Skip to content

[flake8-simplify] Report collapsible elif clauses after a multi-statement if branch (SIM102) - #29814

Merged
ntBre merged 1 commit into
astral-sh:mainfrom
fixfon:fix/29422-sim102-elif
Oct 29, 2026
Merged

ntBre merged 1 commit into
astral-sh:mainfrom
fixfon:fix/29422-sim102-elif

Conversation

@fixfon

@fixfon fixfon commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Part of #29422.

SIM102 missed a nested if inside an elif whenever the first branch had more than one statement. In nested_if_body, when the last branch is an elif we pick that clause as the nested if, but the "only one statement" check still looked at the outer if body. So the length check was done on the wrong body.

The fix checks nested_if.body().len() instead, so an elif uses its own body. The plain if path 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: an elif that only contains a nested if after a multi-statement first branch (now flagged), and one where the elif has another statement next to the nested if (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_linter and clippy both pass.

I used Claude (via T3 Code) to help with this change, and I reviewed and tested it myself.

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@ntBre ntBre added the bug An issue describing something that isn't working, or a PR that fixes a bug label Oct 29, 2026
@ntBre ntBre changed the title [flake8-simplify] Check the elif body length for collapsible elif (SIM102) [flake8-simplify] Report collapsible elif clauses after a multi-statement if branch (SIM102) Oct 29, 2026
@ntBre
ntBre merged commit 7885d24 into astral-sh:main Oct 29, 2026
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue describing something that isn't working, or a PR that fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants