A value is assigned to a local variable and replaced or dropped before anything reads it

tx0/go:dead-storerecommendedwarningbug, maintainability

What it finds

``go n := len(items) n = 0 for _, item := range items { `` A value is assigned to a variable and replaced, or the function ends, before anything reads it. The compiler accepts this as long as the variable is read somewhere, but the assignment does nothing: either it is left over, or a later read was meant to see it and reads something else.

The rule follows each function through the shared IR, where every assignment makes a new value, and asks the dataflow analysis which values are never needed on any path. Reported is an assignment, declaration with a value, or increment of a local variable whose value is not needed. Left alone are zero values such as 0, "", false, nil and T{}, which are often written to make the initial state explicit, variables named like errors, which unread-error covers, variables that a function literal captures and assigns, which live in memory rather than in values, variables whose name is also written as a key of a composite literal, which may be a map key that reads them, variables the function discards with _ = x, results of append, which may be called for its writes into the array, and functions with constructs the lowering does not understand.

From the rule's source, dead_store.rs.

Example from the rule's tests
package p func f(items []int) int {	n := len(items)tx0 reports: the value assigned to n here is never read	n = 0	for _, item := range items {		n += item	}	total := 0	total = n * 2	count := 1	if n > 10 {		count = 2	}	return total + count}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/go:dead-store" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/go:dead-store] reason="..."

tx0 explain tx0/go:dead-store shows the same in the terminal. Found a false positive? Report it.