-
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.
- Loading branch information
Showing
12 changed files
with
492 additions
and
1 deletion.
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.
40 changes: 40 additions & 0 deletions
40
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,40 @@ | ||
# 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: | ||
# | ||
# | ||
# | ||
""" |
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
88 changes: 88 additions & 0 deletions
88
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,88 @@ | ||
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_trivia::is_python_whitespace; | ||
use ruff_source_file::newlines::Line; | ||
use ruff_text_size::{TextRange, TextSize}; | ||
|
||
/// ## 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_comment(line: &Line) -> Option<Diagnostic> { | ||
let first_hash_col = line | ||
.as_str() | ||
.char_indices() | ||
.find(|&(_, char)| char == '#')? // indicates the line does not contain a comment | ||
.0; | ||
|
||
let last_non_whitespace_col = line | ||
.as_str() | ||
.char_indices() | ||
.rev() | ||
.find(|&(_, char)| !is_python_whitespace(char)) | ||
.unwrap() // already verified at least one '#' char is in the line | ||
.0; | ||
|
||
if last_non_whitespace_col != first_hash_col { | ||
return None; // the comment is not empty | ||
} | ||
|
||
let last = match line | ||
.as_str() | ||
.char_indices() | ||
.rev() | ||
.find(|&(_, c)| !is_python_whitespace(c) && c != '#') | ||
{ | ||
#[allow(clippy::cast_possible_truncation)] // TODO: justify safety | ||
Some((i, _second_to_last_non_whitespace_char)) => { | ||
TextSize::new((line.start().to_usize() + i + 1) as u32) | ||
} | ||
None => line.start(), | ||
}; | ||
|
||
Some( | ||
Diagnostic::new( | ||
EmptyComment, | ||
#[allow(clippy::cast_possible_truncation)] // TODO: justify safety | ||
TextRange::new( | ||
line.start() + TextSize::new(first_hash_col as u32), | ||
line.end(), | ||
), | ||
) | ||
.with_fix(Fix::safe_edit(Edit::deletion(last, line.end()))), | ||
) | ||
} |
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
84 changes: 84 additions & 0 deletions
84
...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,84 @@ | ||
--- | ||
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 |-# | ||
3 |+ | ||
4 4 | # | ||
5 5 | # | ||
6 6 | | ||
|
||
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 |- # | ||
4 |+ | ||
5 5 | # | ||
6 6 | | ||
7 7 | # 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 | | ||
6 |+ | ||
7 7 | # this non-empty comment has trailing whitespace and is OK | ||
8 8 | | ||
9 9 | # Many codebases use multiple `#` characters on a single line to visually | ||
|
||
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 | ||
|
||
|
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.