Skip to content

Commit

Permalink
Expand the scope of useless-expression (B018)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 12, 2023
1 parent 12a6fc7 commit 031b428
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
11 changes: 3 additions & 8 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,6 @@ where
pyupgrade::rules::functools_cache(self, decorator_list);
}

if self.settings.rules.enabled(&Rule::UselessExpression) {
flake8_bugbear::rules::useless_expression(self, body);
}

if self.settings.rules.enabled(&Rule::CachedInstanceMethod) {
flake8_bugbear::rules::cached_instance_method(self, decorator_list);
}
Expand Down Expand Up @@ -756,10 +752,6 @@ where
}
}

if self.settings.rules.enabled(&Rule::UselessExpression) {
flake8_bugbear::rules::useless_expression(self, body);
}

if !self.is_stub {
if self
.settings
Expand Down Expand Up @@ -1886,6 +1878,9 @@ where
if self.settings.rules.enabled(&Rule::UselessComparison) {
flake8_bugbear::rules::useless_comparison(self, value);
}
if self.settings.rules.enabled(&Rule::UselessExpression) {
flake8_bugbear::rules::useless_expression(self, value);
}
if self
.settings
.rules
Expand Down
41 changes: 21 additions & 20 deletions crates/ruff/src/rules/flake8_bugbear/rules/useless_expression.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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;
Expand All @@ -17,25 +18,25 @@ impl Violation for UselessExpression {
}

/// 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 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) {
return;
}

checker
.diagnostics
.push(Diagnostic::new(UselessExpression, Range::from(value)));
}
7 changes: 6 additions & 1 deletion crates/ruff_python_ast/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use regex::Regex;
use rustc_hash::{FxHashMap, FxHashSet};
use rustpython_parser::ast::{
Arguments, Constant, Excepthandler, ExcepthandlerKind, Expr, ExprKind, Keyword, KeywordData,
Located, Location, MatchCase, Pattern, PatternKind, Stmt, StmtKind,
Located, Location, MatchCase, Operator, Pattern, PatternKind, Stmt, StmtKind,
};
use rustpython_parser::{lexer, Mode, StringKind, Tok};
use smallvec::{smallvec, SmallVec};
Expand Down Expand Up @@ -142,6 +142,11 @@ pub fn contains_effect(ctx: &Context, expr: &Expr) -> bool {
| ExprKind::Subscript { .. }
| ExprKind::Yield { .. }
| ExprKind::YieldFrom { .. }
| ExprKind::BinOp {
// These are often used as side-effects, e.g., `foo >> bar`.
op: Operator::LShift | Operator::RShift,
..
}
)
})
}
Expand Down

0 comments on commit 031b428

Please sign in to comment.