Skip to content

Combine suppression code diagnostics#22613

Merged
amyreese merged 5 commits intomainfrom
amy/suppression-grouping
Jan 16, 2026
Merged

Combine suppression code diagnostics#22613
amyreese merged 5 commits intomainfrom
amy/suppression-grouping

Conversation

@amyreese
Copy link
Member

@amyreese amyreese commented Jan 16, 2026

Summary

  • Report a single combined UnusedNOQA diagnostic when any range suppression
    has one or more unused, disabled, or duplicate lint codes.
  • Report a single combined InvalidRuleCode diagnostic for any range suppression
    with one or more invalid lint codes.

Test Plan

Updated fixtures and snapshots

Fixes #22429, #21873

@astral-sh-bot
Copy link

astral-sh-bot bot commented Jan 16, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@amyreese amyreese added rule Implementing or modifying a lint rule suppression Related to supression of violations e.g. noqa labels Jan 16, 2026
@amyreese amyreese requested review from MichaReiser and ntBre January 16, 2026 00:50

RUF100 [*] Unused suppression (unused: `E741`)
--> suppressions.py:87:21
RUF100 [*] Unused suppression (unused: `E741`, `F841`; non-enabled: `F401`)
Copy link
Member

Choose a reason for hiding this comment

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

Nice

|
73 | def f():
74 | # Multiple codes but only one is used
75 | # ruff: disable[E741, F401, F841]
Copy link
Member

Choose a reason for hiding this comment

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

The suppression range here is a bit misleading because it suggests that all codes are unused. I think I would either:

  • Highlight the entire suppression diagnostic. That makes it clear that something's up with the suppression and not with an individual code
  • Keep underlining the codes, but only group adjacent codes (in which case we'd emit two diagnostics in this case). I slightly prefer this as it gives more precise ranges.

Copy link
Member

Choose a reason for hiding this comment

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

Don't bother with this if this matches what we do for noqa today.

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like noqa highlights the entire noqa comment:

RUF100 [*] Unused `noqa` directive (unused: `F401`, `E741`)
 --> foo.py:2:12
  |
1 | def f():
2 |     x = 1  # noqa: F401, F841, E741
  |            ^^^^^^^^^^^^^^^^^^^^^^^^
  |
help: Remove unused `noqa` directive
1 | def f():
  -     x = 1  # noqa: F401, F841, E741
2 +     x = 1  # noqa: F841

Copy link
Member

Choose a reason for hiding this comment

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

I would then suggest aligning with noqa (which I think should be relatively easy?)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I liked the idea in #21873 (comment) of extending the range to only the adjacent codes (if I understood correctly), but aligning with noqa makes sense too.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've updated the highlighting logic so that:

  • if only one code exists or only one is being removed, it highlights that one code
  • otherwise, it highlights the entire suppression

I think this matches noqa now.

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Nice!

Comment on lines +317 to +321
disabled: group
.disabled_codes
.iter()
.map(ToString::to_string)
.collect_vec(),
Copy link
Contributor

Choose a reason for hiding this comment

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

This is probably better suited for a follow-up, and I thought I saw some previous discussion of this, but do these need to be Strings? I tried making UnusedCodes::disabled a Vec<&'a str> and it seems to be working locally. Combining that with into_values makes this a little nicer:

Suggested change
disabled: group
.disabled_codes
.iter()
.map(ToString::to_string)
.collect_vec(),
disabled: group.disabled_codes

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't like this either, but since it would also affect the noqa system I'd prefer to put that change in a different PR

|
73 | def f():
74 | # Multiple codes but only one is used
75 | # ruff: disable[E741, F401, F841]
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I liked the idea in #21873 (comment) of extending the range to only the adjacent codes (if I understood correctly), but aligning with noqa makes sense too.

@amyreese amyreese force-pushed the amy/suppression-grouping branch from 9bc09d8 to 7791cc9 Compare January 16, 2026 18:35
@amyreese amyreese linked an issue Jan 16, 2026 that may be closed by this pull request
@amyreese amyreese merged commit 337e3eb into main Jan 16, 2026
41 checks passed
@amyreese amyreese deleted the amy/suppression-grouping branch January 16, 2026 19:08
@MichaReiser
Copy link
Member

Congrats! I think we can now close the block level suppression tracking issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rule Implementing or modifying a lint rule suppression Related to supression of violations e.g. noqa

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle suppression comments with duplicated codes Group unused range suppression codes into single diagnostic

3 participants