-
Notifications
You must be signed in to change notification settings - Fork 927
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…8641) ## Summary This PR extends `unnecessary-pass` (`PIE790`) to flag unnecessary ellipsis expressions in addition to `pass` statements. A `pass` is equivalent to a standalone `...`, so it feels correct to me that a single rule should cover both cases. When we look to v0.2.0, we should also consider deprecating `PYI013`, which flags ellipses only for classes. Closes #8602.
- Loading branch information
1 parent
df9ade7
commit 534fc34
Showing
9 changed files
with
836 additions
and
81 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
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
75 changes: 0 additions & 75 deletions
75
crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_pass.rs
This file was deleted.
Oops, something went wrong.
127 changes: 127 additions & 0 deletions
127
crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_placeholder.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,127 @@ | ||
use ruff_diagnostics::AlwaysFixableViolation; | ||
use ruff_diagnostics::{Diagnostic, Edit, Fix}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::whitespace::trailing_comment_start_offset; | ||
use ruff_python_ast::Stmt; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
use crate::fix; | ||
|
||
/// ## What it does | ||
/// Checks for unnecessary `pass` statements in functions, classes, and other | ||
/// blocks. | ||
/// | ||
/// In [preview], this rule also checks for unnecessary ellipsis (`...`) | ||
/// literals. | ||
/// | ||
/// ## Why is this bad? | ||
/// In Python, the `pass` statement and ellipsis (`...`) literal serve as | ||
/// placeholders, allowing for syntactically correct empty code blocks. The | ||
/// primary purpose of these nodes is to avoid syntax errors in situations | ||
/// where a statement or expression is syntactically required, but no code | ||
/// needs to be executed. | ||
/// | ||
/// If a `pass` or ellipsis is present in a code block that includes at least | ||
/// one other statement (even, e.g., a docstring), it is unnecessary and should | ||
/// be removed. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// def func(): | ||
/// """Placeholder docstring.""" | ||
/// pass | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// def func(): | ||
/// """Placeholder docstring.""" | ||
/// ``` | ||
/// | ||
/// In [preview]: | ||
/// ```python | ||
/// def func(): | ||
/// """Placeholder docstring.""" | ||
/// ... | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// def func(): | ||
/// """Placeholder docstring.""" | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: The `pass` statement](https://docs.python.org/3/reference/simple_stmts.html#the-pass-statement) | ||
/// | ||
/// [preview]: https://docs.astral.sh/ruff/preview/ | ||
#[violation] | ||
pub struct UnnecessaryPlaceholder { | ||
kind: Placeholder, | ||
} | ||
|
||
impl AlwaysFixableViolation for UnnecessaryPlaceholder { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let Self { kind } = self; | ||
match kind { | ||
Placeholder::Pass => format!("Unnecessary `pass` statement"), | ||
Placeholder::Ellipsis => format!("Unnecessary `...` literal"), | ||
} | ||
} | ||
|
||
fn fix_title(&self) -> String { | ||
let Self { kind } = self; | ||
match kind { | ||
Placeholder::Pass => format!("Remove unnecessary `pass`"), | ||
Placeholder::Ellipsis => format!("Remove unnecessary `...`"), | ||
} | ||
} | ||
} | ||
|
||
/// PIE790 | ||
pub(crate) fn unnecessary_placeholder(checker: &mut Checker, body: &[Stmt]) { | ||
if body.len() < 2 { | ||
return; | ||
} | ||
|
||
for stmt in body { | ||
let kind = match stmt { | ||
Stmt::Pass(_) => Placeholder::Pass, | ||
Stmt::Expr(expr) | ||
if expr.value.is_ellipsis_literal_expr() | ||
&& checker.settings.preview.is_enabled() => | ||
{ | ||
Placeholder::Ellipsis | ||
} | ||
_ => continue, | ||
}; | ||
|
||
let mut diagnostic = Diagnostic::new(UnnecessaryPlaceholder { kind }, stmt.range()); | ||
let edit = if let Some(index) = trailing_comment_start_offset(stmt, checker.locator()) { | ||
Edit::range_deletion(stmt.range().add_end(index)) | ||
} else { | ||
fix::edits::delete_stmt(stmt, None, checker.locator(), checker.indexer()) | ||
}; | ||
diagnostic.set_fix(Fix::safe_edit(edit).isolate(Checker::isolation( | ||
checker.semantic().current_statement_id(), | ||
))); | ||
checker.diagnostics.push(diagnostic); | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
enum Placeholder { | ||
Pass, | ||
Ellipsis, | ||
} | ||
|
||
impl std::fmt::Display for Placeholder { | ||
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
match self { | ||
Self::Pass => fmt.write_str("pass"), | ||
Self::Ellipsis => fmt.write_str("..."), | ||
} | ||
} | ||
} |
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.