-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[ruff] Add rule to use human-readable names in ruff:ignore comments (RUF106)
#26682
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
308 changes: 308 additions & 0 deletions
308
crates/ruff_linter/resources/mdtest/ruff/rule-codes-in-suppression-comments.md
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,308 @@ | ||
| # `rule-codes-in-suppression-comments` (`RUF106`) | ||
|
|
||
| ```toml | ||
| [lint] | ||
| preview = true | ||
| select = ["RUF106"] | ||
| external = ["EXT"] | ||
| ``` | ||
|
|
||
| ## `ruff:ignore` | ||
|
|
||
| Each Ruff rule code receives a separate diagnostic. Rule names and external or unknown codes are | ||
| preserved: | ||
|
|
||
| ```py | ||
| # snapshot: rule-codes-in-suppression-comments | ||
| # snapshot: rule-codes-in-suppression-comments | ||
| # ruff:ignore[F401, undefined-name, EXT001, UNKNOWN, F841] | ||
| value = 1 | ||
| ``` | ||
|
|
||
| ```snapshot | ||
| error[RUF106]: Rule code used instead of name in suppression comment | ||
| --> src/mdtest_snippet.py:3:15 | ||
| | | ||
| 3 | # ruff:ignore[F401, undefined-name, EXT001, UNKNOWN, F841] | ||
| | ^^^^ | ||
| | | ||
| help: Replace rule code with name | ||
| | | ||
| 2 | # snapshot: rule-codes-in-suppression-comments | ||
| - # ruff:ignore[F401, undefined-name, EXT001, UNKNOWN, F841] | ||
| 3 + # ruff:ignore[unused-import, undefined-name, EXT001, UNKNOWN, F841] | ||
| 4 | value = 1 | ||
| | | ||
|
|
||
|
|
||
| error[RUF106]: Rule code used instead of name in suppression comment | ||
| --> src/mdtest_snippet.py:3:54 | ||
| | | ||
| 3 | # ruff:ignore[F401, undefined-name, EXT001, UNKNOWN, F841] | ||
| | ^^^^ | ||
| | | ||
| help: Replace rule code with name | ||
| | | ||
| 2 | # snapshot: rule-codes-in-suppression-comments | ||
| - # ruff:ignore[F401, undefined-name, EXT001, UNKNOWN, F841] | ||
| 3 + # ruff:ignore[F401, undefined-name, EXT001, UNKNOWN, unused-variable] | ||
| 4 | value = 1 | ||
| | | ||
| ``` | ||
|
|
||
| Valid human-readable names are unaffected: | ||
|
|
||
| ```py | ||
| # snapshot: rule-codes-in-suppression-comments | ||
| # snapshot: rule-codes-in-suppression-comments | ||
| # ruff:ignore[F401, undefined-name, F841] | ||
| value = 1 | ||
| ``` | ||
|
|
||
| ```snapshot | ||
| error[RUF106]: Rule code used instead of name in suppression comment | ||
| --> src/mdtest_snippet.py:7:15 | ||
| | | ||
| 7 | # ruff:ignore[F401, undefined-name, F841] | ||
| | ^^^^ | ||
| | | ||
| help: Replace rule code with name | ||
| | | ||
| 6 | # snapshot: rule-codes-in-suppression-comments | ||
| - # ruff:ignore[F401, undefined-name, F841] | ||
| 7 + # ruff:ignore[unused-import, undefined-name, F841] | ||
| 8 | value = 1 | ||
| | | ||
|
|
||
|
|
||
| error[RUF106]: Rule code used instead of name in suppression comment | ||
| --> src/mdtest_snippet.py:7:37 | ||
| | | ||
| 7 | # ruff:ignore[F401, undefined-name, F841] | ||
| | ^^^^ | ||
| | | ||
| help: Replace rule code with name | ||
| | | ||
| 6 | # snapshot: rule-codes-in-suppression-comments | ||
| - # ruff:ignore[F401, undefined-name, F841] | ||
| 7 + # ruff:ignore[F401, undefined-name, unused-variable] | ||
| 8 | value = 1 | ||
| | | ||
| ``` | ||
|
|
||
| ## `ruff:file-ignore` | ||
|
|
||
| ```py | ||
| # snapshot: rule-codes-in-suppression-comments | ||
| # snapshot: rule-codes-in-suppression-comments | ||
| # ruff:file-ignore[F401, F841] | ||
| ``` | ||
|
|
||
| ```snapshot | ||
| error[RUF106]: Rule code used instead of name in suppression comment | ||
| --> src/mdtest_snippet.py:3:20 | ||
| | | ||
| 3 | # ruff:file-ignore[F401, F841] | ||
| | ^^^^ | ||
| | | ||
| help: Replace rule code with name | ||
| | | ||
| 2 | # snapshot: rule-codes-in-suppression-comments | ||
| - # ruff:file-ignore[F401, F841] | ||
| 3 + # ruff:file-ignore[unused-import, F841] | ||
| | | ||
|
|
||
|
|
||
| error[RUF106]: Rule code used instead of name in suppression comment | ||
| --> src/mdtest_snippet.py:3:26 | ||
| | | ||
| 3 | # ruff:file-ignore[F401, F841] | ||
| | ^^^^ | ||
| | | ||
| help: Replace rule code with name | ||
| | | ||
| 2 | # snapshot: rule-codes-in-suppression-comments | ||
| - # ruff:file-ignore[F401, F841] | ||
| 3 + # ruff:file-ignore[F401, unused-variable] | ||
| | | ||
| ``` | ||
|
|
||
| ## Matched `ruff:disable` and `ruff:enable` | ||
|
|
||
| Matching comments are reported and fixed together: | ||
|
|
||
| ```py | ||
| # snapshot: rule-codes-in-suppression-comments | ||
| # snapshot: rule-codes-in-suppression-comments | ||
| # ruff:disable[F401, undefined-name, F841] | ||
| value = 1 | ||
| # ruff:enable[F401, undefined-name, F841] | ||
| ``` | ||
|
|
||
| ```snapshot | ||
| error[RUF106]: Rule code used instead of name in suppression comment | ||
| --> src/mdtest_snippet.py:3:16 | ||
| | | ||
| 3 | # ruff:disable[F401, undefined-name, F841] | ||
| | ^^^^ | ||
| 4 | value = 1 | ||
| 5 | # ruff:enable[F401, undefined-name, F841] | ||
| | ---- | ||
| | | ||
| help: Replace rule code with name | ||
| | | ||
| 2 | # snapshot: rule-codes-in-suppression-comments | ||
| - # ruff:disable[F401, undefined-name, F841] | ||
| 3 + # ruff:disable[unused-import, undefined-name, F841] | ||
| 4 | value = 1 | ||
| - # ruff:enable[F401, undefined-name, F841] | ||
| 5 + # ruff:enable[unused-import, undefined-name, F841] | ||
| | | ||
|
|
||
|
|
||
| error[RUF106]: Rule code used instead of name in suppression comment | ||
| --> src/mdtest_snippet.py:3:38 | ||
| | | ||
| 3 | # ruff:disable[F401, undefined-name, F841] | ||
| | ^^^^ | ||
| 4 | value = 1 | ||
| 5 | # ruff:enable[F401, undefined-name, F841] | ||
| | ---- | ||
| | | ||
| help: Replace rule code with name | ||
| | | ||
| 2 | # snapshot: rule-codes-in-suppression-comments | ||
| - # ruff:disable[F401, undefined-name, F841] | ||
| 3 + # ruff:disable[F401, undefined-name, unused-variable] | ||
| 4 | value = 1 | ||
| - # ruff:enable[F401, undefined-name, F841] | ||
| 5 + # ruff:enable[F401, undefined-name, unused-variable] | ||
| | | ||
| ``` | ||
|
|
||
| ## Unmatched `ruff:disable` | ||
|
|
||
| An unmatched disable comment is still an effective suppression through the end of its indentation | ||
| level: | ||
|
|
||
| ```py | ||
| # snapshot: rule-codes-in-suppression-comments | ||
| # ruff:disable[F401] | ||
| ``` | ||
|
|
||
| ```snapshot | ||
| error[RUF106]: Rule code used instead of name in suppression comment | ||
| --> src/mdtest_snippet.py:2:16 | ||
| | | ||
| 2 | # ruff:disable[F401] | ||
| | ^^^^ | ||
| | | ||
| help: Replace rule code with name | ||
| | | ||
| 1 | # snapshot: rule-codes-in-suppression-comments | ||
| - # ruff:disable[F401] | ||
| 2 + # ruff:disable[unused-import] | ||
| | | ||
| ``` | ||
|
|
||
| ## Unmatched `ruff:enable` | ||
|
|
||
| An unmatched enable comment is invalid and is left to `invalid-suppression-comment`: | ||
|
|
||
| ```py | ||
| # ruff:enable[F401] | ||
| ``` | ||
|
|
||
| ## Redirected codes | ||
|
|
||
| Redirected codes are replaced with the name of their canonical rule: | ||
|
|
||
| ```py | ||
| # snapshot: rule-codes-in-suppression-comments | ||
| # ruff:ignore[PGH001] | ||
| value = 1 | ||
| ``` | ||
|
|
||
| ```snapshot | ||
| error[RUF106]: Rule code used instead of name in suppression comment | ||
| --> src/mdtest_snippet.py:2:15 | ||
| | | ||
| 2 | # ruff:ignore[PGH001] | ||
| | ^^^^^^ | ||
| | | ||
| help: Replace rule code with name | ||
| | | ||
| 1 | # snapshot: rule-codes-in-suppression-comments | ||
| - # ruff:ignore[PGH001] | ||
| 2 + # ruff:ignore[suspicious-eval-usage] | ||
| 3 | value = 1 | ||
| | | ||
| ``` | ||
|
|
||
| ## Nested suppression comments | ||
|
|
||
| Only the rule codes within a nested suppression comment are replaced: | ||
|
|
||
| ```py | ||
| # snapshot: rule-codes-in-suppression-comments | ||
| # snapshot: rule-codes-in-suppression-comments | ||
| value = 1 # explanation # ruff:ignore[F401, F841] reason # another | ||
| ``` | ||
|
|
||
| ```snapshot | ||
| error[RUF106]: Rule code used instead of name in suppression comment | ||
| --> src/mdtest_snippet.py:3:40 | ||
| | | ||
| 3 | value = 1 # explanation # ruff:ignore[F401, F841] reason # another | ||
| | ^^^^ | ||
| | | ||
| help: Replace rule code with name | ||
| | | ||
| 2 | # snapshot: rule-codes-in-suppression-comments | ||
| - value = 1 # explanation # ruff:ignore[F401, F841] reason # another | ||
| 3 + value = 1 # explanation # ruff:ignore[unused-import, F841] reason # another | ||
| | | ||
|
|
||
|
|
||
| error[RUF106]: Rule code used instead of name in suppression comment | ||
| --> src/mdtest_snippet.py:3:46 | ||
| | | ||
| 3 | value = 1 # explanation # ruff:ignore[F401, F841] reason # another | ||
| | ^^^^ | ||
| | | ||
| help: Replace rule code with name | ||
| | | ||
| 2 | # snapshot: rule-codes-in-suppression-comments | ||
| - value = 1 # explanation # ruff:ignore[F401, F841] reason # another | ||
| 3 + value = 1 # explanation # ruff:ignore[F401, unused-variable] reason # another | ||
| | | ||
| ``` | ||
|
|
||
| ## Comments without Ruff rule codes | ||
|
|
||
| Comments containing only names and external or unknown codes are unchanged: | ||
|
|
||
| ```py | ||
| # ruff:ignore[unused-import, EXT001, UNKNOWN] | ||
| value = 1 | ||
| ``` | ||
|
|
||
| ## Self-suppression | ||
|
|
||
| The rule can be suppressed by its code or name: | ||
|
|
||
| ```py | ||
| # ruff:ignore[F401, RUF106] | ||
| value = 1 | ||
| ``` | ||
|
|
||
| ```py | ||
| # ruff:ignore[F401, rule-codes-in-suppression-comments] | ||
| value = 1 | ||
| ``` | ||
|
|
||
| The diagnostic can also be suppressed with a `noqa` comment: | ||
|
|
||
| ```py | ||
| value = 1 # ruff:ignore[F401] # noqa: RUF106 | ||
| ``` | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test for an ignore that uses human readable names and codes? I don't think this is covered yet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is covered by:
ruff/crates/ruff_linter/resources/mdtest/ruff/rule-codes-in-suppression-comments.md
Lines 10 to 20 in 68ce969
if I understand your suggestion. Or do you mean just human-readable names and valid codes, without the external codes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I mean one with valid human readable names and valid codes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a version without the external codes:
ruff/crates/ruff_linter/resources/mdtest/ruff/rule-codes-in-suppression-comments.md
Lines 53 to 60 in 20a6dd9