Skip to content

Conversation

@amyreese
Copy link
Member

@amyreese amyreese commented Dec 10, 2025

Summary

  • Adds new RUF103 and RUF104 diagnostics for invalid and unmatched suppression comments
  • Reports RUF104 diagnostics for any unmatched suppression comment

Test Plan

Updated snapshots from test cases with unmatched suppression comments

Issue #3711
Fixes #21878
Fixes #21875

@amyreese amyreese force-pushed the amy/unmatched-suppressions branch from 282450e to 7e9def9 Compare December 10, 2025 23:29
@astral-sh-bot
Copy link

astral-sh-bot bot commented Dec 10, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@amyreese amyreese changed the title Report diagnostics for unmatched range suppression comments Report diagnostics for invalid/unmatched range suppression comments Dec 11, 2025
@MichaReiser
Copy link
Member

Uff, ruff: isort 😆

amyreese added a commit that referenced this pull request Dec 11, 2025
## Summary

Ignores `#ruff:isort` when parsing suppressions similar to `#ruff:noqa`.
Should clear up ecosystem issues in #21908

## Test Plan

cargo tests
@amyreese amyreese force-pushed the amy/unmatched-suppressions branch from 100ed3f to 999a481 Compare December 12, 2025 01:25
@amyreese amyreese marked this pull request as ready for review December 12, 2025 01:26
@MichaReiser MichaReiser added rule Implementing or modifying a lint rule preview Related to preview mode features labels Dec 12, 2025
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Overall this looks good. A few more smaller suggestions

},
range,
);
diagnostic.set_fix(Fix::safe_edit(edit));
Copy link
Member

Choose a reason for hiding this comment

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

Does noqa offer a fix if the rule code is invalid? Removing is one possible solution but the user might also have misspelled the rule name and the proper fix is to fix the rule name instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, the noqa implementation does a range deletion of either the entire comment or the individual invalid code, and also marks those as safe edits.

Comment on lines 275 to 276
suppression.comments.len() == 1
&& suppression.comments[0].action == SuppressionAction::Disable
Copy link
Member

Choose a reason for hiding this comment

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

I think this is another case where I'd prefer to have dedicated enable and disable fields on Suppression. It would remove the need for code like this to "poke" into the internals.

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! Just a couple of small things from me beyond Micha's comments.

This is a bit off topic, and I'm not sure if y'all have discussed this (or already implemented it?), but I was thinking it would be nice to have a code action to wrap the selected region with these comments, like we have for adding a noqa comment.

/// ## References
/// - [Ruff error suppression](https://docs.astral.sh/ruff/linter/#error-suppression)
#[derive(ViolationMetadata)]
#[violation_metadata(preview_since = "0.14.9")]
Copy link
Contributor

Choose a reason for hiding this comment

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

We'll have to bump these after the release this week.

@MichaReiser
Copy link
Member

This is a bit off topic, and I'm not sure if y'all have discussed this (or already implemented it?), but I was thinking it would be nice to have a code action to wrap the selected region with these comments, like we have for adding a noqa comment.

I think this is trickier because the main use case is to suppress multiple statements. It still recommended to use noqa for local suppressions.

Showing too many code actions is also noisy, which is why I wouldn't implement this for now

@amyreese
Copy link
Member Author

Checkpointing the feedback I got to today.

@amyreese amyreese force-pushed the amy/unmatched-suppressions branch from 8a54e01 to 5bdf049 Compare December 18, 2025 01:12
@amyreese
Copy link
Member Author

Reorganized the logic to reduce repeat iterations and only consider each suppression for one type of violation at a time (I think that's what Micha was getting at?). I'd like to leave consolidating the diagnostics and any other refactors/structural changes to follow-up PRs.

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

A few more nits about which diagnostics we emit but this otherwise looks good.

94 | # ruff: disable[F841, RQW320]
95 | value = 0
|
help: Remove the rule code
Copy link
Member

Choose a reason for hiding this comment

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

Maybe: Remove the suppression comment if all codes are invalid

Copy link
Member Author

Choose a reason for hiding this comment

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

Changing this would also change the help text for all #noqa directives as well. Should that happen in a separate PR?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, maybe

@amyreese amyreese merged commit 3d334a3 into main Dec 18, 2025
37 checks passed
@amyreese amyreese deleted the amy/unmatched-suppressions branch December 18, 2025 20:58
amyreese added a commit that referenced this pull request Dec 18, 2025
Missed including this in the follow-up on #21908
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Report unmatched range suppression comments Report invalid range suppressions and parse errors

4 participants