Skip to content

B909 false positive when directly returning mutation #18399

@TeamSpen210

Description

@TeamSpen210

Summary

The B909 rule checks for mutations to loop iterables, but permits cases where return/break would prevent further iterations. However if the return and the mutation are the same statement it flags the value.

Example:

def fail_map(mapping):
    for key in mapping:
        return mapping.pop(key)  # false positive

def success_map(mapping):
    for key in mapping:
        ret = mapping.pop(key)  # passes
        return ret

def fail_list(seq):
    for val in seq:
        return seq.pop(4)  # also false positive

Used ruff check --preview --isolated --select B909 iter_mut_bug.py.

Version

ruff 0.11.11 (0397682 2025-05-22)

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedContributions especially welcomeruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions