-
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
9 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
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,17 @@ | ||
# 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 | ||
|
||
|
||
def foo(): # this comment is OK, the one below is not | ||
pass # | ||
|
||
|
||
# the lines below have no comments and are OK | ||
def bar(): | ||
pass |
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
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
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 are unnecessary and can make code harder to read. | ||
/// Consider using a blank line instead. | ||
#[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> { | ||
if let Some((column, last_non_whitespace_char)) = line | ||
.as_str() | ||
.char_indices() | ||
.rev() | ||
.find(|&(_, c)| !c.is_whitespace()) | ||
{ | ||
if last_non_whitespace_char == '#' { | ||
#[allow(clippy::cast_possible_truncation)] | ||
let start = TextSize::new((line.start().to_usize() + column) as u32); | ||
let last = match line | ||
.as_str() | ||
.char_indices() | ||
.rev() | ||
.find(|&(_, c)| !c.is_whitespace() && c != '#') | ||
{ | ||
Some((i, _second_to_last_non_whitespace_char)) => | ||
{ | ||
#[allow(clippy::cast_possible_truncation)] | ||
TextSize::new((line.start().to_usize() + i + 1) as u32) | ||
} | ||
None => line.start(), | ||
}; | ||
|
||
return Some( | ||
Diagnostic::new(EmptyComment, TextRange::new(start, line.end())) | ||
.with_fix(Fix::safe_edit(Edit::deletion(last, line.end()))), | ||
); | ||
} | ||
} | ||
None | ||
} |
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
83 changes: 83 additions & 0 deletions
83
...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,83 @@ | ||
--- | ||
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 | | ||
|
||
empty_comment.py:5:5: PLR2044 [*] Line with empty comment | ||
| | ||
3 | # | ||
4 | # | ||
5 | # | ||
| ^^^^^ PLR2044 | ||
| | ||
= 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 | | ||
7 |+ | ||
8 8 | # this non-empty comment has trailing whitespace and is OK | ||
9 9 | | ||
10 10 | | ||
|
||
empty_comment.py:12:11: PLR2044 [*] Line with empty comment | ||
| | ||
11 | def foo(): # this comment is OK, the one below is not | ||
12 | pass # | ||
| ^^^^^ PLR2044 | ||
| | ||
= help: Delete the empty comment | ||
|
||
ℹ Safe fix | ||
9 9 | | ||
10 10 | | ||
11 11 | def foo(): # this comment is OK, the one below is not | ||
12 |- pass # | ||
12 |+ pass | ||
13 13 | | ||
14 14 | | ||
15 15 | # the lines below have no comments and are OK | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.