Modify Parameter
ID |
go.modify_parameter |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Go |
Tags |
code-style, readability |
Description
Reports an assignment whose left-hand side is a function parameter, for example
func f(x int) { x = 1 }.
Rationale
Go passes arguments by value, so reassigning a parameter mutates only the function’s private
copy — the caller never sees the change. Reusing the parameter as a scratch variable also
hides the original argument and makes the function harder to follow. A short variable
declaration (:=) introduces a new binding and is not reported; writing through a pointer
parameter (p.field = x) is visible to the caller and is also left alone.
// Bad
func f(x int) { x = 1 } // FLAW
// Good
func f(x int) { y := x; y = 1 } // OK — local copy
func g(p *T) { p.field = 1 } // OK — mutates the pointee