-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expand the scope of useless-expression (B018) (#3455)
- Loading branch information
1 parent
aea925a
commit e8d17d2
Showing
8 changed files
with
142 additions
and
52 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
78 changes: 57 additions & 21 deletions
78
crates/ruff/src/rules/flake8_bugbear/rules/useless_expression.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 |
---|---|---|
@@ -1,41 +1,77 @@ | ||
use rustpython_parser::ast::{Constant, ExprKind, Stmt, StmtKind}; | ||
use rustpython_parser::ast::{Constant, Expr, ExprKind}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::helpers::contains_effect; | ||
use ruff_python_ast::types::Range; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub enum Kind { | ||
Expression, | ||
Attribute, | ||
} | ||
|
||
#[violation] | ||
pub struct UselessExpression; | ||
pub struct UselessExpression { | ||
kind: Kind, | ||
} | ||
|
||
impl Violation for UselessExpression { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Found useless expression. Either assign it to a variable or remove it.") | ||
match self.kind { | ||
Kind::Expression => { | ||
format!("Found useless expression. Either assign it to a variable or remove it.") | ||
} | ||
Kind::Attribute => { | ||
format!( | ||
"Found useless attribute access. Either assign it to a variable or remove it." | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// B018 | ||
pub fn useless_expression(checker: &mut Checker, body: &[Stmt]) { | ||
for stmt in body { | ||
if let StmtKind::Expr { value } = &stmt.node { | ||
match &value.node { | ||
ExprKind::List { .. } | ExprKind::Dict { .. } | ExprKind::Set { .. } => { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(UselessExpression, Range::from(value))); | ||
} | ||
ExprKind::Constant { value: val, .. } => match &val { | ||
Constant::Str { .. } | Constant::Ellipsis => {} | ||
_ => { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(UselessExpression, Range::from(value))); | ||
} | ||
}, | ||
_ => {} | ||
pub fn useless_expression(checker: &mut Checker, value: &Expr) { | ||
// Ignore comparisons, as they're handled by `useless_comparison`. | ||
if matches!(value.node, ExprKind::Compare { .. }) { | ||
return; | ||
} | ||
|
||
// Ignore strings, to avoid false positives with docstrings. | ||
if matches!( | ||
value.node, | ||
ExprKind::JoinedStr { .. } | ||
| ExprKind::Constant { | ||
value: Constant::Str(..) | Constant::Ellipsis, | ||
.. | ||
} | ||
) { | ||
return; | ||
} | ||
|
||
// Ignore statements that have side effects. | ||
if contains_effect(&checker.ctx, value) { | ||
// Flag attributes as useless expressions, even if they're attached to calls or other | ||
// expressions. | ||
if matches!(value.node, ExprKind::Attribute { .. }) { | ||
checker.diagnostics.push(Diagnostic::new( | ||
UselessExpression { | ||
kind: Kind::Attribute, | ||
}, | ||
Range::from(value), | ||
)); | ||
} | ||
return; | ||
} | ||
|
||
checker.diagnostics.push(Diagnostic::new( | ||
UselessExpression { | ||
kind: Kind::Expression, | ||
}, | ||
Range::from(value), | ||
)); | ||
} |
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