No Unmodified Loop
ID |
go.no_unmodified_loop |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Go |
Tags |
reliability, suspicious |
Description
Reports a condition-only for cond { … } loop whose condition variables are never modified
anywhere in the body and that has no other way to exit.
Rationale
The loop never makes progress towards its termination test: if the condition is true on entry it
spins forever, and if false it never runs once. This is almost always a forgotten increment, a
missing reassignment, or a guard placed on the wrong variable. A loop that modifies a condition
variable, contains a break/return/goto, or passes a condition variable to a callee (which
could mutate it through a pointer) is not reported.
// Bad
for i < n { fmt.Println(i) } // FLAW — neither i nor n changes; never terminates
// Good
for i < n { i++ } // OK — i is modified
for !done { done = next() } // OK — done is reassigned
Remediation
Update a condition variable inside the loop body, or add an explicit exit (break/return) so
the loop can terminate.