Skip to content

[flake8-bugbear] Fix panic on match subjects (B031) - #27781

Merged
ntBre merged 3 commits into
astral-sh:mainfrom
pikammmmm:fix-b031-match-subject-panic
Aug 28, 2026
Merged

ntBre merged 3 commits into
astral-sh:mainfrom
pikammmmm:fix-b031-match-subject-panic

Conversation

@pikammmmm

@pikammmmm pikammmmm commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #26624

@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre August 15, 2026 12:42
…ect (B031)

The `Stmt::Match` arm pushed an empty branch-counter frame before
visiting the subject, so a group usage in the subject unwrapped
`last_mut()` on the empty frame and panicked. Visit the subject first:
it is evaluated unconditionally, so it belongs to the enclosing count,
and a subsequent usage in a case body is still flagged.

Fixes astral-sh#26624
@pikammmmm
pikammmmm force-pushed the fix-b031-match-subject-panic branch from cc25d72 to b546bb4 Compare August 15, 2026 19:07
@astral-sh-bot

astral-sh-bot Bot commented Aug 17, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@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.

Thanks, this looks good to me overall. I just had one suggestion to treat If statements similarly and a small nit about a comment.

Comment thread crates/ruff_linter/src/rules/flake8_bugbear/rules/reuse_of_groupby_generator.rs Outdated
@ntBre ntBre added the bug An issue describing something that isn't working, or a PR that fixes a bug label Aug 17, 2026
The comment on the `match` subject fix referenced a panic that no
longer exists after the fix; trim it to the first sentence per
review feedback.

Also extend the same "visit unconditionally-evaluated expression
before pushing branch counters" pattern to `if` statement tests,
which are evaluated unconditionally just like a `match` subject.
Previously the test's usage was attributed to the `if` branch's
counter, causing a false negative when the group was reused in the
`else` branch (or another elif branch).
@pikammmmm

Copy link
Copy Markdown
Contributor Author

Trimmed the comment to the first sentence, and extended the unconditional-visit-then-track-counters pattern to if tests too — added a test for the else-branch reuse case. PTAL.

@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 changed the title [flake8-bugbear] Fix B031 panic on match subjects [flake8-bugbear] Fix panic on match subjects (B031) Aug 28, 2026
@ntBre
ntBre enabled auto-merge (squash) August 28, 2026 20:56
@ntBre
ntBre merged commit e837e8b into astral-sh:main Aug 28, 2026
48 checks passed
@jherbel

jherbel commented Aug 31, 2026

Copy link
Copy Markdown

Thanks!

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.

Panic: reuse-of-groupby-generator (B031) + match list(group)

3 participants