Repository navigation
[flake8-simplify] Report collapsible elif clauses after a multi-statement if branch (SIM102) - #29814
Merged
Merged
Conversation
flake8-simplify] Check the elif body length for collapsible elif (SIM102)flake8-simplify] Report collapsible elif clauses after a multi-statement if branch (SIM102)
This was referenced Nov 9, 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
Part of #29422.
SIM102 missed a nested
ifinside anelifwhenever the first branch had more than one statement. Innested_if_body, when the last branch is anelifwe pick that clause as the nestedif, but the "only one statement" check still looked at the outerifbody. So the length check was done on the wrong body.The fix checks
nested_if.body().len()instead, so anelifuses its own body. The plainifpath behaves the same as before.This only covers the SIM102 item from the issue. I didn't touch the other rules there.
Test Plan
I added two cases to
SIM102.py: anelifthat only contains a nestedifafter a multi-statement first branch (now flagged), and one where theelifhas another statement next to the nestedif(still not flagged). The example from the issue is now reported at 5:5. The snapshot also gets one extra context line on the previous last diagnostic, because the file doesn't end there anymore.cargo test -p ruff_linterand clippy both pass.I used Claude (via T3 Code) to help with this change, and I reviewed and tested it myself.