A for loop's update changes a variable that its condition does not read

tx0/ts:loop-update-mismatchdefaultwarningbug

What it finds

for (let i = 0; i < rows; j++): the update of a for loop changes a variable that the condition does not read, so the condition never changes because of it and the loop runs forever or not at all. Usually a nested loop's counter was copied into the wrong header.

Reported are loops whose update is a single ++, -- or compound assignment of a plain name and whose condition compares, without calls or other effects, a variable that the loop's head declares and its body never changes, when the updated name is not read by the condition. Loops whose condition depends on state the body changes, as for (let i = 0; queue.length < n; i++), are left alone.

From the rule's source, loop_update_mismatch.rs.

Example from the rule's tests
export function grid(rows: number, cols: number) {  let n = 0;  for (let i = 0; i < rows; i++) {    for (let j = 0; j < cols; i++) {tx0 reports: the update changes i, which the loop's condition does not read      n++;    }  }  return n;}
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 in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:loop-update-mismatch" = "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/ts:loop-update-mismatch] reason="..."

tx0 explain tx0/ts:loop-update-mismatch shows the same in the terminal. Found a false positive? Report it.