-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement RUF028 to detect useless formatter suppression comments (#9899
) <!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> Fixes #6611 ## Summary This lint rule spots comments that are _intended_ to suppress or enable the formatter, but will be ignored by the Ruff formatter. We borrow some functions the formatter uses for determining comment placement / putting them in context within an AST. The analysis function uses an AST visitor to visit each comment and attach it to the AST. It then uses that context to check: 1. Is this comment in an expression? 2. Does this comment have bad placement? (e.g. a `# fmt: skip` above a function instead of at the end of a line) 3. Is this comment redundant? 4. Does this comment actually suppress any code? 5. Does this comment have ambiguous placement? (e.g. a `# fmt: off` above an `else:` block) If any of these are true, a violation is thrown. The reported reason depends on the order of the above check-list: in other words, a `# fmt: skip` comment on its own line within a list expression will be reported as being in an expression, since that reason takes priority. The lint suggests removing the comment as an unsafe fix, regardless of the reason. ## Test Plan A snapshot test has been created.
- Loading branch information
1 parent
36bc725
commit 0293908
Showing
41 changed files
with
1,215 additions
and
438 deletions.
There are no files selected for viewing
This file contains 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,63 @@ | ||
def fmt_off_between_lists(): | ||
test_list = [ | ||
# fmt: off | ||
1, | ||
2, | ||
3, | ||
] | ||
|
||
|
||
# note: the second `fmt: skip`` should be OK | ||
def fmt_skip_on_own_line(): | ||
# fmt: skip | ||
pass # fmt: skip | ||
|
||
|
||
@fmt_skip_on_own_line | ||
# fmt: off | ||
@fmt_off_between_lists | ||
def fmt_off_between_decorators(): | ||
pass | ||
|
||
|
||
@fmt_off_between_decorators | ||
# fmt: off | ||
class FmtOffBetweenClassDecorators: | ||
... | ||
|
||
|
||
def fmt_off_in_else(): | ||
x = [1, 2, 3] | ||
for val in x: | ||
print(x) | ||
# fmt: off | ||
else: | ||
print("done") | ||
while False: | ||
print("while") | ||
# fmt: off | ||
# fmt: off | ||
else: | ||
print("done") | ||
if len(x) > 3: | ||
print("huh?") | ||
# fmt: on | ||
# fmt: off | ||
else: | ||
print("expected") | ||
|
||
|
||
class Test: | ||
@classmethod | ||
# fmt: off | ||
def cls_method_a( | ||
# fmt: off | ||
cls, | ||
) -> None: # noqa: test # fmt: skip | ||
pass | ||
|
||
|
||
def fmt_on_trailing(): | ||
# fmt: off | ||
val = 5 # fmt: on | ||
pass # fmt: on |
This file contains 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 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 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 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 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
Oops, something went wrong.