Overwritten Argument
ID |
go.overwritten_argument |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Go |
Tags |
code-style, suspicious |
Description
Reports a function parameter whose first use in the body is an assignment to it, with the
incoming argument value never read first, for example func f(x int) { x = 5; use(x) }.
Rationale
When the first thing a function does with a parameter is overwrite it, the value the caller
passed is discarded before it is ever observed. The parameter is effectively a fresh local
variable — either the argument is pointless and should be removed, or (more commonly) a read of
the argument was forgotten. Reading the parameter on the right-hand side (x = x + 1) is a
normal read-before-write and is not reported.
// Bad
func f(x int) { x = 5; use(x) } // FLAW — argument overwritten before any read
// Good
func g(x int) { use(x); x = 5 } // OK — read first
func h(x int) { x = x + 1 } // OK — x is read on the RHS