[flake8-bugbear] Fix break/continue handling in loop-iterator-mutation (B909)#24440
Merged
MichaReiser merged 5 commits intoastral-sh:mainfrom Apr 21, 2026
Merged
Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| B909 | 2 | 0 | 2 | 0 | 0 |
MichaReiser
reviewed
Apr 8, 2026
Contributor
Author
|
@MichaReiser Thank you so much for the review. Addressed both comments, nested break/continue is now gated by loop depth, and branch tracking covers for/else, while/else, and try/except/else/finally. Ready for re-review whenever you have time! Thank you |
break/continue handling in loop-iterator-mutation (B909)
MichaReiser
reviewed
Apr 14, 2026
MichaReiser
approved these changes
Apr 16, 2026
Member
MichaReiser
left a comment
There was a problem hiding this comment.
Thank you. I found one more false positive which we should fix if it's easy to do so. If not, that's fine also.
This PR is certainly a big improvement!
a7d4d02 to
e947692
Compare
MichaReiser
approved these changes
Apr 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #12402.
B909's branch tracking had a few issues: the branch counter wasn't restored after if blocks, continue wasn't handled as a terminator, nested loop break/continue was incorrectly treated as exiting the outer loop, and there was no branch isolation for for/else, while/else, or try/except/else/finally.
Fixed by tracking loop_depth so nested break only affects the inner loop, giving each arm of forking statements its own branch (merged back into the parent on exit), and pre-scanning loop bodies to determine whether else is unconditional.
Test Plan
Added 8 fixture cases covering nested break/continue, for/else, while/else, and try/except/else/finally.