Fix B015 false positive on comparison deep inside expression statement (#616)

This commit is contained in:
Anders Kaseorg 2022-11-05 17:13:22 -07:00 committed by GitHub
parent 22cfd03b13
commit b067b665ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 10 deletions

View File

@ -22,3 +22,6 @@ data = [x for x in [1, 2, 3] if x in (1, 2)]
class TestClass:
1 == 1
print(1 == 1)

View File

@ -809,6 +809,11 @@ where
}
}
StmtKind::Delete { .. } => {}
StmtKind::Expr { value, .. } => {
if self.settings.enabled.contains(&CheckCode::B015) {
flake8_bugbear::plugins::useless_comparison(self, value)
}
}
_ => {}
}
@ -1334,12 +1339,6 @@ where
self.locate_check(Range::from_located(expr)),
));
}
if self.settings.enabled.contains(&CheckCode::B015) {
if let Some(parent) = self.parents.last() {
flake8_bugbear::plugins::useless_comparison(self, left, parent);
}
}
}
ExprKind::Constant {
value: Constant::Str(value),

View File

@ -1,14 +1,14 @@
use rustpython_ast::{Expr, Stmt, StmtKind};
use rustpython_ast::{Expr, ExprKind};
use crate::ast::types::{CheckLocator, Range};
use crate::check_ast::Checker;
use crate::checks::{Check, CheckKind};
pub fn useless_comparison(checker: &mut Checker, expr: &Expr, parent: &Stmt) {
if let StmtKind::Expr { .. } = &parent.node {
pub fn useless_comparison(checker: &mut Checker, expr: &Expr) {
if let ExprKind::Compare { left, .. } = &expr.node {
checker.add_check(Check::new(
CheckKind::UselessComparison,
checker.locate_check(Range::from_located(expr)),
checker.locate_check(Range::from_located(left)),
));
}
}