Return Value Ignored
ID |
go.return_value_ignored |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Go |
Tags |
dead-code, reliability |
Description
Reports a statement that is just a call to a known pure function whose result is then discarded. A pure function has no observable side effect, so dropping its return value means the call does nothing.
Rationale
strings.ToUpper(s) on its own line looks like it transforms s, but strings are immutable in Go:
the upper-cased copy is computed and thrown away while s is unchanged. This is almost always a
typo for s = strings.ToUpper(s). The rule fires only when the call is the whole statement; a pure
call whose result is assigned, returned or passed as an argument is fine. The set of functions
treated as pure is conservative and configurable.
strings.ToUpper(s) // FLAW — the upper-cased string is thrown away
s = strings.ToUpper(s) // OK — result is assigned
fmt.Println(strings.ToUpper(s)) // OK — result is consumed as an argument
Remediation
Assign or otherwise use the result (s = strings.ToUpper(s)). If the call really is unnecessary,
delete it. Tune the pureFunctions property to match the functions your project considers pure.