diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B909.py b/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B909.py index c9be4f036bd88..08de7b2f5be9d 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B909.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B909.py @@ -179,3 +179,17 @@ def func(): for elem in some_list: if some_list.pop() == 2: return + +# should not error - direct return with mutation (Issue #18399) +def fail_map(mapping): + for key in mapping: + return mapping.pop(key) + +def success_map(mapping): + for key in mapping: + ret = mapping.pop(key) # should not error + return ret + +def fail_list(seq): + for val in seq: + return seq.pop(4) diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/rules/loop_iterator_mutation.rs b/crates/ruff_linter/src/rules/flake8_bugbear/rules/loop_iterator_mutation.rs index fe3ff0870045d..9473466a32a01 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/rules/loop_iterator_mutation.rs +++ b/crates/ruff_linter/src/rules/flake8_bugbear/rules/loop_iterator_mutation.rs @@ -285,7 +285,6 @@ impl<'a> Visitor<'a> for LoopMutationsVisitor<'a> { if let Some(mutations) = self.mutations.get_mut(&self.branch) { mutations.clear(); } - visitor::walk_stmt(self, stmt); } // Avoid recursion for class and function definitions.