mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 21:43:52 +00:00
Expand the scope of useless-expression (B018) (#3455)
This commit is contained in:
parent
aea925a898
commit
e8d17d23cb
8 changed files with 146 additions and 56 deletions
|
@ -131,6 +131,39 @@ pub fn contains_effect(ctx: &Context, expr: &Expr) -> bool {
|
|||
}
|
||||
}
|
||||
|
||||
// Avoid false positive for overloaded operators.
|
||||
if let ExprKind::BinOp { left, right, .. } = &expr.node {
|
||||
if !matches!(
|
||||
left.node,
|
||||
ExprKind::Constant { .. }
|
||||
| ExprKind::JoinedStr { .. }
|
||||
| ExprKind::List { .. }
|
||||
| ExprKind::Tuple { .. }
|
||||
| ExprKind::Set { .. }
|
||||
| ExprKind::Dict { .. }
|
||||
| ExprKind::ListComp { .. }
|
||||
| ExprKind::SetComp { .. }
|
||||
| ExprKind::DictComp { .. }
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
if !matches!(
|
||||
right.node,
|
||||
ExprKind::Constant { .. }
|
||||
| ExprKind::JoinedStr { .. }
|
||||
| ExprKind::List { .. }
|
||||
| ExprKind::Tuple { .. }
|
||||
| ExprKind::Set { .. }
|
||||
| ExprKind::Dict { .. }
|
||||
| ExprKind::ListComp { .. }
|
||||
| ExprKind::SetComp { .. }
|
||||
| ExprKind::DictComp { .. }
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Otherwise, avoid all complex expressions.
|
||||
matches!(
|
||||
expr.node,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue