Skip to content

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

Closed

Description

@jherbel

Summary

The reuse-of-groupby-generator rule results in a panic when running on a match statement in a specific constellation. Minimal repro:

from itertools import groupby


for key, group in groupby([]):
    match list(group):
        case _:
            pass

Ruff invocation:

uvx --python 3.14.6 ruff check --isolated --select B031
panic: Panicked at crates/ruff_linter/src/rules/flake8_bugbear/rules/reuse_of_groupby_generator.rs:99:30 when checking `/home/joergherbel/tmp/B031_ruff_crash/repro.py`: `called `Option::unwrap()` on a `None` value`
--> repro.py:1:1
info: This indicates a bug in Ruff.
info: If you could open an issue at https://github.com/astral-sh/ruff/issues/new?title=%5Bpanic%5D, we'd be very appreciative!
info: run with `RUST_BACKTRACE=1` environment variable to show the full backtrace information

Found 1 error.
error: Panic during linting indicates a bug in Ruff. If you could open an issue at:

https://github.com/astral-sh/ruff/issues/new?title=%5BLinter%20panic%5D

...with the relevant file contents, the `pyproject.toml` settings, and the stack trace above, we'd be very appreciative!

I don't have a pyproject.toml in the cwd. Note that this workaround passes:

from itertools import groupby


for key, group in groupby([]):
    group_list = list(group)
    match group_list:
        case _:
            pass

Playground link: https://play.ruff.rs/0e4ad728-f221-4db1-abce-0d91e7b56dd8

Version

ruff 0.15.20

Activity

  1. ntBre commented on Jul 8, 2026

    @ntBre
    Contributor

    Thank you for the report!

  2. added
    bugAn issue describing something that isn't working, or a PR that fixes a bug
    on Jul 8, 2026
  3. SuryanshSS1011 commented on Jul 16, 2026

    @SuryanshSS1011
    Contributor

    Looking into this, the panic is in the Stmt::Match arm of the visitor. This pushes an empty counter frame and then visits the subject, so increment_usage_count unwraps a None on the still-empty inner vec.

    Visiting subject before pushing the frame should fix it, rather than pre-pushing a 0 the way the If arm does. The subject is evaluated only once and unconditionally, so it isn't part of any branch. Counting it inside the frame would make match list(group) with a case that also uses group come out as max(1, 1) instead of 2.

    @ntBre does this sound good? Happy to open a PR with that plus a regression test if so.

  4. added a commit that references this issue on Aug 15, 2026
    b546bb4
  5. added a commit that references this issue on Aug 28, 2026
    e837e8b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions