The address of a range variable is kept beyond its iteration, before Go 1.22, so all copies point to one variable
What it finds
``go for _, item := range items { ptrs = append(ptrs, &item) } ` Before Go 1.22, the variables a range clause declares are shared by all iterations, so &item is the same pointer every time, and after the loop every element of ptrs points to the last item. Taking the address of the element, &items[i]`, or copying the variable first gives distinct pointers. Since Go 1.22 each iteration has its own variables.
The rule applies to files whose nearest go.mod names a version before 1.22. Reported is &v of a variable that a range clause declares with :=, when the pointer is appended to a slice or stored by an assignment whose target is not a plain variable declared in the loop, such as an element, a field or a variable declared before it. An assignment followed by break or return is left alone, since the loop ends while the variable still holds the value.
From the rule's source, loop_variable_address.rs.
Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/go:loop-variable-address" = "off"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:loop-variable-address] reason="..."tx0 explain tx0/go:loop-variable-address shows the same in the terminal. Found a false positive? Report it.