A loop tests only local variables that the loop never changes, so once entered it never ends
What it finds
while (i < n) { total += i; } never ends once it starts: its condition reads only local variables, and nothing in the loop changes them. The step was forgotten, or the update of a for loop changes another variable than the one the condition tests, as in for (int i = 0; i < n; j++).
The condition must consist of local variables and parameters of primitive types, literals and operators, without calls, fields or array elements, which code elsewhere could change. The loop must not assign, increment or decrement any of the variables, and must not be left by break, return, throw, yield, a continue with a label, or a call of exit. Only an exception thrown from a call could still end it.
From the rule's source, loop_condition_never_changes.rs.
class Counter { int sum(int n, int[] values) { int total = 0; int i = 0; while (i < n) {tx0 reports: reads only i, n, which this loop never changes total += values[0]; } for (int j = 0; j < n; total++) {tx0 reports: reads only j, n total += j; } for (int k = 0; k < n; k++) { total += k; } int m = 0; while (m < n) { if (values[m] > 0) { break; } } while (total < n) { total = total + values.length; } while (values.length > n) { total++; } return total; }} 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/java:frozen-loop-condition" = "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/java:frozen-loop-condition] reason="..."tx0 explain tx0/java:frozen-loop-condition shows the same in the terminal. Found a false positive? Report it.