Unread Value
ID |
go.unread_value |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Go |
Tags |
code-style, suspicious |
Description
Reports a dead store: a value assigned to a local variable that is overwritten by the very next
statement without being read in between (x = a; x = b).
Rationale
The first assignment computes a value that nothing ever observes — it is wasted work and almost
always a bug: a forgotten read, the wrong variable on the left, or a leftover line from edited
code. When the second write reads the variable on its right-hand side (x = x + 1), the first
value is observed and the store is live, so that case is not reported.
// Bad
x = 1 // FLAW — overwritten before being read
x = 2
// Good
x = 1
fmt.Println(x) // OK — read before the next write
x = 2
x = x + 1 // OK — the second write reads x first
x = x * 2
Remediation
Remove the dead store, or insert the read that was intended between the two assignments.