Expand the scope of useless-expression (B018) (#3455)

This commit is contained in:
Charlie Marsh 2023-03-23 18:33:58 -04:00 committed by GitHub
parent aea925a898
commit e8d17d23cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 146 additions and 56 deletions

View file

@ -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,