Nothing in a loop changes its condition and nothing leaves it, so it never ends once entered
What it finds
``go for i := 0; i < len(items); { process(items[i]) } `` Nothing in the loop changes what its condition reads, and nothing leaves the loop, so once the condition holds it holds forever and the loop never ends. Usually the step was forgotten, or a different variable is stepped.
The loop must have a condition and no post statement. The condition must compare local variables, constants and len or cap of local variables, with no other calls, no fields, elements, pointers or receives. No variable of the same name may be assigned, stepped or have its address taken anywhere in the function, its own declaration aside, and the body has no break, return, goto, or call of panic or of a function named like Exit or Fatal.
From the rule's source, loop_condition_unchanged.rs.
package p func f(items []int) int { sum := 0 for i := 0; i < len(items); {tx0 reports: nothing in the loop changes i < len(items) sum += items[i] } for j := 0; j < len(items); { sum += items[j] j++ } k := 0 for k < 10 { k += 2 } for n := 0; n < 3; { if sum > 3 { break } } for m := 0; m < 4; {tx0 reports: nothing in the loop changes m < 4 print(m) } return sum} func g(done *bool) { for i := 0; i < 1; { if *done { return } }} 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-condition-unchanged" = "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-condition-unchanged] reason="..."tx0 explain tx0/go:loop-condition-unchanged shows the same in the terminal. Found a false positive? Report it.