-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Expand the scope of useless-expression (B018) #3455
Merged
Merged
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
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 { .. }) { | ||
Comment on lines
+58
to
+60
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. Should we use a different diagnostic in that case? |
||
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
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.
Adding a small code snipped here of a case where a docstring causes a false positive would be helpful.
Is there a way to determine whether a string is a docstring?