-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[pylint
] Implement empty-comment
(PLR2044
)
#9174
Merged
charliermarsh
merged 7 commits into
astral-sh:main
from
mdbernard:PLR2044-empty-comment
Dec 29, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3e15b26
[pylint] add PLR2044 empty_comment
mdbernard 454f8ef
Merge branch 'main' into PLR2044-empty-comment
charliermarsh 19d9f3b
Move to preview
charliermarsh 72223bc
Move to token-based rule; delete entire line
charliermarsh 32119dd
Add empty check
charliermarsh 20e8827
Handle blank lines
charliermarsh 6d78bd6
Fix ranges
charliermarsh 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
crates/ruff_linter/resources/test/fixtures/pylint/empty_comment.py
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,53 @@ | ||
# this line has a non-empty comment and is OK | ||
# this line is also OK, but the three following lines are not | ||
# | ||
# | ||
# | ||
|
||
# this non-empty comment has trailing whitespace and is OK | ||
|
||
# Many codebases use multiple `#` characters on a single line to visually | ||
# separate sections of code, so we don't consider these empty comments. | ||
|
||
########################################## | ||
|
||
# trailing `#` characters are not considered empty comments ### | ||
|
||
|
||
def foo(): # this comment is OK, the one below is not | ||
pass # | ||
|
||
|
||
# the lines below have no comments and are OK | ||
def bar(): | ||
pass | ||
|
||
|
||
# "Empty comments" are common in block comments | ||
# to add legibility. For example: | ||
# | ||
# The above line's "empty comment" is likely | ||
# intentional and is considered OK. | ||
|
||
|
||
# lines in multi-line strings/comments whose last non-whitespace character is a `#` | ||
# do not count as empty comments | ||
""" | ||
The following lines are all fine: | ||
# | ||
# | ||
# | ||
""" | ||
|
||
# These should be removed, despite being an empty "block comment". | ||
|
||
# | ||
# | ||
|
||
# These should also be removed. | ||
|
||
x = 1 | ||
|
||
# | ||
## | ||
# |
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
106 changes: 106 additions & 0 deletions
106
crates/ruff_linter/src/rules/pylint/rules/empty_comment.rs
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,106 @@ | ||
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_index::Indexer; | ||
use ruff_python_trivia::is_python_whitespace; | ||
|
||
use ruff_source_file::Locator; | ||
use ruff_text_size::{TextRange, TextSize}; | ||
use rustc_hash::FxHashSet; | ||
|
||
/// ## What it does | ||
/// Checks for a # symbol appearing on a line not followed by an actual comment. | ||
/// | ||
/// ## Why is this bad? | ||
/// Empty comments don't provide any clarity to the code, and just add clutter. | ||
/// Either add a comment or delete the empty comment. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// class Foo: # | ||
/// pass | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// class Foo: | ||
/// pass | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Pylint documentation](https://pylint.pycqa.org/en/latest/user_guide/messages/refactor/empty-comment.html) | ||
#[violation] | ||
pub struct EmptyComment; | ||
|
||
impl Violation for EmptyComment { | ||
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Always; | ||
|
||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Line with empty comment") | ||
} | ||
|
||
fn fix_title(&self) -> Option<String> { | ||
Some(format!("Delete the empty comment")) | ||
} | ||
} | ||
|
||
/// PLR2044 | ||
pub(crate) fn empty_comments( | ||
diagnostics: &mut Vec<Diagnostic>, | ||
indexer: &Indexer, | ||
locator: &Locator, | ||
) { | ||
let block_comments = FxHashSet::from_iter(indexer.comment_ranges().block_comments(locator)); | ||
|
||
for range in indexer.comment_ranges() { | ||
// Ignore comments that are part of multi-line "comment blocks". | ||
if block_comments.contains(&range.start()) { | ||
continue; | ||
} | ||
|
||
// If the line contains an empty comment, add a diagnostic. | ||
if let Some(diagnostic) = empty_comment(*range, locator) { | ||
diagnostics.push(diagnostic); | ||
} | ||
} | ||
} | ||
|
||
/// Return a [`Diagnostic`] if the comment at the given [`TextRange`] is empty. | ||
fn empty_comment(range: TextRange, locator: &Locator) -> Option<Diagnostic> { | ||
// Check: is the comment empty? | ||
if !locator | ||
.slice(range) | ||
.chars() | ||
.skip(1) | ||
.all(is_python_whitespace) | ||
{ | ||
return None; | ||
} | ||
|
||
// Find the location of the `#`. | ||
let first_hash_col = range.start(); | ||
|
||
// Find the start of the line. | ||
let line = locator.line_range(first_hash_col); | ||
|
||
// Find the last character in the line that precedes the comment, if any. | ||
let deletion_start_col = locator | ||
.slice(TextRange::new(line.start(), first_hash_col)) | ||
.char_indices() | ||
.rev() | ||
.find(|&(_, c)| !is_python_whitespace(c) && c != '#') | ||
.map(|(last_non_whitespace_non_comment_col, _)| { | ||
// SAFETY: (last_non_whitespace_non_comment_col + 1) <= first_hash_col | ||
TextSize::try_from(last_non_whitespace_non_comment_col).unwrap() + TextSize::new(1) | ||
}); | ||
|
||
Some( | ||
Diagnostic::new(EmptyComment, TextRange::new(first_hash_col, line.end())).with_fix( | ||
Fix::safe_edit(if let Some(deletion_start_col) = deletion_start_col { | ||
Edit::deletion(line.start() + deletion_start_col, line.end()) | ||
} else { | ||
Edit::range_deletion(locator.full_line_range(first_hash_col)) | ||
}), | ||
), | ||
) | ||
} |
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
118 changes: 118 additions & 0 deletions
118
...c/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR2044_empty_comment.py.snap
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,118 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
empty_comment.py:3:1: PLR2044 [*] Line with empty comment | ||
| | ||
1 | # this line has a non-empty comment and is OK | ||
2 | # this line is also OK, but the three following lines are not | ||
3 | # | ||
| ^ PLR2044 | ||
4 | # | ||
5 | # | ||
| | ||
= help: Delete the empty comment | ||
|
||
ℹ Safe fix | ||
1 1 | # this line has a non-empty comment and is OK | ||
2 2 | # this line is also OK, but the three following lines are not | ||
3 |-# | ||
4 3 | # | ||
5 4 | # | ||
6 5 | | ||
|
||
empty_comment.py:4:5: PLR2044 [*] Line with empty comment | ||
| | ||
2 | # this line is also OK, but the three following lines are not | ||
3 | # | ||
4 | # | ||
| ^ PLR2044 | ||
5 | # | ||
| | ||
= help: Delete the empty comment | ||
|
||
ℹ Safe fix | ||
1 1 | # this line has a non-empty comment and is OK | ||
2 2 | # this line is also OK, but the three following lines are not | ||
3 3 | # | ||
4 |- # | ||
5 4 | # | ||
6 5 | | ||
7 6 | # this non-empty comment has trailing whitespace and is OK | ||
|
||
empty_comment.py:5:9: PLR2044 [*] Line with empty comment | ||
| | ||
3 | # | ||
4 | # | ||
5 | # | ||
| ^ PLR2044 | ||
6 | | ||
7 | # this non-empty comment has trailing whitespace and is OK | ||
| | ||
= help: Delete the empty comment | ||
|
||
ℹ Safe fix | ||
2 2 | # this line is also OK, but the three following lines are not | ||
3 3 | # | ||
4 4 | # | ||
5 |- # | ||
6 5 | | ||
7 6 | # this non-empty comment has trailing whitespace and is OK | ||
8 7 | | ||
|
||
empty_comment.py:18:11: PLR2044 [*] Line with empty comment | ||
| | ||
17 | def foo(): # this comment is OK, the one below is not | ||
18 | pass # | ||
| ^ PLR2044 | ||
| | ||
= help: Delete the empty comment | ||
|
||
ℹ Safe fix | ||
15 15 | | ||
16 16 | | ||
17 17 | def foo(): # this comment is OK, the one below is not | ||
18 |- pass # | ||
18 |+ pass | ||
19 19 | | ||
20 20 | | ||
21 21 | # the lines below have no comments and are OK | ||
|
||
empty_comment.py:44:1: PLR2044 [*] Line with empty comment | ||
| | ||
42 | # These should be removed, despite being an empty "block comment". | ||
43 | | ||
44 | # | ||
| ^ PLR2044 | ||
45 | # | ||
| | ||
= help: Delete the empty comment | ||
|
||
ℹ Safe fix | ||
42 42 | # These should be removed, despite being an empty "block comment". | ||
43 43 | | ||
44 44 | # | ||
45 |-# | ||
46 45 | | ||
47 46 | # These should also be removed. | ||
48 47 | | ||
|
||
empty_comment.py:45:1: PLR2044 [*] Line with empty comment | ||
| | ||
44 | # | ||
45 | # | ||
| ^ PLR2044 | ||
46 | | ||
47 | # These should also be removed. | ||
| | ||
= help: Delete the empty comment | ||
|
||
ℹ Safe fix | ||
42 42 | # These should be removed, despite being an empty "block comment". | ||
43 43 | | ||
44 44 | # | ||
45 |-# | ||
46 45 | | ||
47 46 | # These should also be removed. | ||
48 47 | | ||
|
||
|
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.
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.