-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Report diagnostics for invalid/unmatched range suppression comments #21908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 23 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
90f82c1
Define rule codes RUF103/104 for invalid/unmatched suppression comments
amyreese 976a52c
Report RUF104 unmatched suppression diagnostics
amyreese 05f63c9
Report invalid suppression diagnostics
amyreese aa91353
Todos
amyreese ebf0121
Fix test cases causing panics
amyreese 2b6dc03
Move rule-enabled checks to local logic, extract some bits
amyreese bcd9374
Generate invalid rule code diagnostics
amyreese 58f5734
Target the diagnostic at the code even if only one code in comment
amyreese 4793f52
clippy
amyreese c50026e
Document that implicit ranges will produce RUF104
amyreese a6e9ca8
Add test case covering external rules
amyreese 6cee82d
Correct pub usage
amyreese 5b7d82b
Improve documentation for unmatched diagnostic
amyreese b659b5b
Mark invalid comment diagnostics as unsafe fixes
amyreese 83706a9
Update test snapshots
amyreese 5c112af
update wording in invalid rule code diagnostics
amyreese 0f3c74f
Revert "Target the diagnostic at the code even if only one code in co…
amyreese f5532ff
Avoid unnecessary assignments
amyreese ea17270
Use itertools unique
amyreese b90f418
Handle external codes and redirects
amyreese 5bdf049
Reorganize check_suppressions to iterate over valid/errors only once
amyreese 0548374
clippy
amyreese bafc629
Fix docs
amyreese a6c73ae
Only target the invalid rule code
amyreese 0e8b8aa
Treat missing codes as invalid suppression comment, update error message
amyreese 5f5bd05
Better implementation of highlighting only code for invalid rule codes
amyreese b14a6ac
More test cases, improve error/fix wording
amyreese 78e8594
Update comment to clarify unused external codes
amyreese File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
crates/ruff_linter/src/rules/ruff/rules/invalid_suppression_comment.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| use ruff_macros::{ViolationMetadata, derive_message_formats}; | ||
|
|
||
| use crate::AlwaysFixableViolation; | ||
| use crate::suppression::{InvalidSuppressionKind, ParseErrorKind}; | ||
|
|
||
| /// ## What it does | ||
| /// Checks for invalid suppression comments | ||
| /// | ||
| /// ## Why is this bad? | ||
| /// Invalid suppression comments are ignored by Ruff, and should either | ||
| /// be fixed or removed to avoid confusion. | ||
| /// | ||
| /// ## Example | ||
| /// ```python | ||
| /// ruff: disable # missing codes | ||
| /// ``` | ||
| /// | ||
| /// Use instead: | ||
| /// ```python | ||
| /// # ruff: disable[E501] | ||
| /// ``` | ||
| /// | ||
| /// Or delete the invalid suppression comment. | ||
| /// | ||
amyreese marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// ## References | ||
| /// - [Ruff error suppression](https://docs.astral.sh/ruff/linter/#error-suppression) | ||
| #[derive(ViolationMetadata)] | ||
| #[violation_metadata(preview_since = "0.14.9")] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll have to bump these after the release this week. |
||
| pub(crate) struct InvalidSuppressionComment { | ||
| pub(crate) kind: InvalidSuppressionCommentKind, | ||
| } | ||
|
|
||
| impl AlwaysFixableViolation for InvalidSuppressionComment { | ||
| #[derive_message_formats] | ||
| fn message(&self) -> String { | ||
| let msg = match self.kind { | ||
| InvalidSuppressionCommentKind::Invalid(InvalidSuppressionKind::Indentation) => { | ||
| "unexpected indentation".to_string() | ||
| } | ||
| InvalidSuppressionCommentKind::Invalid(InvalidSuppressionKind::Trailing) => { | ||
| "trailing comments are not supported".to_string() | ||
| } | ||
| InvalidSuppressionCommentKind::Invalid(InvalidSuppressionKind::Unmatched) => { | ||
| "no matching 'disable' comment".to_string() | ||
| } | ||
| InvalidSuppressionCommentKind::Error(error) => format!("{error}"), | ||
| }; | ||
| format!("Invalid suppression comment: {msg}") | ||
| } | ||
|
|
||
| fn fix_title(&self) -> String { | ||
| "Remove invalid suppression comment".to_string() | ||
| } | ||
| } | ||
|
|
||
| pub(crate) enum InvalidSuppressionCommentKind { | ||
| Invalid(InvalidSuppressionKind), | ||
| Error(ParseErrorKind), | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
crates/ruff_linter/src/rules/ruff/rules/unmatched_suppression_comment.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| use ruff_macros::{ViolationMetadata, derive_message_formats}; | ||
|
|
||
| use crate::Violation; | ||
|
|
||
| /// ## What it does | ||
| /// Checks for unmatched range suppression comments | ||
| /// | ||
| /// ## Why is this bad? | ||
| /// Unmatched range suppression comments can inadvertently suppress violations | ||
| /// over larger sections of code than intended, particularly at module scope. | ||
| /// | ||
| /// ## Example | ||
| /// ```python | ||
| /// def foo(): | ||
| /// # ruff: disable[E501] # unmatched | ||
| /// REALLY_LONG_VALUES = [...] | ||
| /// | ||
| /// print(REALLY_LONG_VALUES) | ||
| /// ``` | ||
| /// | ||
| /// Use instead: | ||
| /// ```python | ||
| /// def foo(): | ||
| /// # ruff: disable[E501] | ||
| /// REALLY_LONG_VALUES = [...] | ||
| /// # ruff: enable[E501] | ||
| /// | ||
| /// print(REALLY_LONG_VALUES) | ||
| /// ``` | ||
| /// | ||
| /// ## References | ||
| /// - [Ruff error suppression](https://docs.astral.sh/ruff/linter/#error-suppression) | ||
| #[derive(ViolationMetadata)] | ||
| #[violation_metadata(preview_since = "0.14.9")] | ||
| pub(crate) struct UnmatchedSuppressionComment; | ||
|
|
||
| impl Violation for UnmatchedSuppressionComment { | ||
| #[derive_message_formats] | ||
| fn message(&self) -> String { | ||
| "Suppression comment without matching `#ruff:enable` comment".to_string() | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.