An inner for loop assigns the variable of an outer one

tx0/python:nested-loop-reuses-variablerecommendedwarningbug

What it finds

for i in rows: with for i in cols: inside: the inner loop overwrites the outer loop's variable, so code after the inner loop, in the same pass of the outer one, sees the inner value. Usually the second name was meant to be different.

From the rule's source, nested_loop_reuses_variable.rs.

Example from the rule's tests
def grid(rows, cols):    out = []    for i in rows:        for i in cols:tx0 reports: this loop assigns i, the variable of an enclosing loop            out.append(i)    for r in rows:        for c in cols:            out.append((r, c))    for _ in rows:        for _ in cols:            pass    return out
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 from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:nested-loop-reuses-variable" = "warning"

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/python:nested-loop-reuses-variable] reason="..."

tx0 explain tx0/python:nested-loop-reuses-variable shows the same in the terminal. Found a false positive? Report it.