Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-simplify] Avoid some more enumerate-for-loop false positives (SIM113) #9515

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,30 @@ def func():
for y in range(5):
g(x, idx)
idx += 1


def func():
# OK (index used within inner)
idx = 0

def inner():
print(idx)

for x in range(5):
for y in range(5):
g(x, idx)
idx += 1


def func():
# OK (index used as nonlocal inner)
idx = 0

def inner():
nonlocal idx
idx = 3

for x in range(5):
for y in range(5):
g(x, idx)
idx += 1
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,10 @@ pub(crate) fn enumerate_for_loop(checker: &mut Checker, for_stmt: &ast::StmtFor)
binding
};

// If the variable is used _after_ the loop, ignore it.
// Find the binding for the augmented assignment.
// If the variable is used outside the loop, ignore it.
if binding.references.iter().any(|id| {
let reference = checker.semantic().reference(*id);
reference.start() > for_stmt.end()
!for_stmt.range().contains_range(reference.range())
}) {
continue;
}
Expand Down
Loading