A loop has an else block but no break, so the block runs like code after the loop

tx0/python:loop-else-without-breakrecommendedwarningbug

What it finds

for ... else or while ... else without a break in the loop: the else block runs whenever the loop ends without break, so without one it runs whenever the loop ends, and its code might as well follow the loop. The else was likely meant to run when nothing was found, which needs a break when something is.

From the rule's source, loop_else_without_break.rs.

Example from the rule's tests
def find(items, key):    for item in items:        if item == key:            return item    else:tx0 reports: the loop has no break, so this else block runs whenever the loop ends        return None  def search(items, key):    for item in items:        if item == key:            break    else:        return None    while items:        for other in items:            break        items.pop()    else:tx0 reports: the loop has no break        return key
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:loop-else-without-break" = "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:loop-else-without-break] reason="..."

tx0 explain tx0/python:loop-else-without-break shows the same in the terminal. Found a false positive? Report it.