diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM113.py b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM113.py index 67ef392337b3c..44cb92b3a3db2 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM113.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM113.py @@ -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 diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/enumerate_for_loop.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/enumerate_for_loop.rs index 95f2237b01952..37244eeb2f982 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/enumerate_for_loop.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/enumerate_for_loop.rs @@ -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; }