-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
RUF010: New rule for removing blank comments #4269
Closed
andreyfedoseev
wants to merge
1
commit into
astral-sh:main
from
andreyfedoseev:RUF010-blank-comments
Closed
Changes from all commits
Commits
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 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,19 @@ | ||
# Multi-line comment | ||
# with a blank line | ||
# | ||
# is valid | ||
|
||
print("Hello, world!") # | ||
|
||
|
||
def func_1(): | ||
# This is a valid comment | ||
print("Hello, world!") # | ||
|
||
|
||
def func_2(): | ||
# Indented multi-line comment | ||
# with a blank line | ||
# | ||
# is valid | ||
print("Hello, world!") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use crate::registry::Rule; | ||
use crate::settings::{flags, Settings}; | ||
use once_cell::sync::Lazy; | ||
use regex::Regex; | ||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::newlines::Line; | ||
use ruff_text_size::{TextLen, TextRange, TextSize}; | ||
|
||
/// ## What it does | ||
/// Check for blank comments. | ||
/// | ||
/// ## Why is this bad? | ||
/// Blank comments are useless and should be removed. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// print("Hello, World!") # | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// print("Hello, World!") | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Ruff documentation](https://beta.ruff.rs/docs/configuration/#error-suppression) | ||
#[violation] | ||
pub struct BlankComment; | ||
|
||
impl AlwaysAutofixableViolation for BlankComment { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Blank comments are useless and should be removed") | ||
} | ||
|
||
fn autofix_title(&self) -> String { | ||
"Remove blank comment".to_string() | ||
} | ||
} | ||
|
||
static BLACK_COMMENT_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\S(\s*#\s*)$").unwrap()); | ||
|
||
/// RUF010 | ||
pub fn blank_comment( | ||
diagnostics: &mut Vec<Diagnostic>, | ||
line: &Line, | ||
settings: &Settings, | ||
autofix: flags::Autofix, | ||
) { | ||
if let Some(captures) = BLACK_COMMENT_REGEX.captures(line.as_str()) { | ||
let match_ = captures.get(1).unwrap(); | ||
let range = TextRange::at( | ||
line.start() + TextSize::try_from(match_.start()).unwrap(), | ||
match_.as_str().text_len(), | ||
); | ||
let mut diagnostic = Diagnostic::new(BlankComment, range); | ||
if autofix.into() && settings.rules.should_fix(Rule::BlankComment) { | ||
diagnostic.set_fix(Edit::deletion(range.start(), range.end())); | ||
} | ||
diagnostics.push(diagnostic); | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
crates/ruff/src/rules/ruff/snapshots/ruff__rules__ruff__tests__RUF010_RUF010.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,42 @@ | ||
--- | ||
source: crates/ruff/src/rules/ruff/mod.rs | ||
--- | ||
RUF010.py:6:23: RUF010 [*] Blank comments are useless and should be removed | ||
| | ||
6 | # is valid | ||
7 | | ||
8 | print("Hello, world!") # | ||
| ^^^ RUF010 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Can we change the range to only highlight the comment |
||
| | ||
= help: Remove blank comment | ||
|
||
ℹ Suggested fix | ||
3 3 | # | ||
4 4 | # is valid | ||
5 5 | | ||
6 |-print("Hello, world!") # | ||
6 |+print("Hello, world!") | ||
7 7 | | ||
8 8 | | ||
9 9 | def func_1(): | ||
|
||
RUF010.py:11:27: RUF010 [*] Blank comments are useless and should be removed | ||
| | ||
11 | def func_1(): | ||
12 | # This is a valid comment | ||
13 | print("Hello, world!") # | ||
| ^^^ RUF010 | ||
| | ||
= help: Remove blank comment | ||
|
||
ℹ Suggested fix | ||
8 8 | | ||
9 9 | def func_1(): | ||
10 10 | # This is a valid comment | ||
11 |- print("Hello, world!") # | ||
11 |+ print("Hello, world!") | ||
12 12 | | ||
13 13 | | ||
14 14 | def func_2(): | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Ruff has the
Indexer
data structure that exposes the range of all comments (Indexer::comment_ranges
). You can use this withLocator.slice(range)
to retrieve all comments and call the rule at the top level (module) once. This should be faster than running a regex on every line.I think we should also be able to replace the regex all together by manually skipping the
#
and then testing iftext.trim().is_empty()
.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.
Yeah I guess we could do something like this