mirror of https://github.com/astral-sh/ruff
Fix `SIM222` and `SIM223` false positive (#3832)
This commit is contained in:
parent
f3f9a9f297
commit
fe38597279
|
|
@ -29,3 +29,16 @@ if True or f() or a or g() or b: # SIM222
|
||||||
|
|
||||||
if a or True or f() or b or g(): # SIM222
|
if a or True or f() or b or g(): # SIM222
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if a and f() and b and g() and False: # OK
|
||||||
|
pass
|
||||||
|
|
||||||
|
if a and f() and False and g() and b: # OK
|
||||||
|
pass
|
||||||
|
|
||||||
|
if False and f() and a and g() and b: # OK
|
||||||
|
pass
|
||||||
|
|
||||||
|
if a and False and f() and b and g(): # OK
|
||||||
|
pass
|
||||||
|
|
|
||||||
|
|
@ -16,11 +16,24 @@ if False:
|
||||||
if a and f() and b and g() and False: # OK
|
if a and f() and b and g() and False: # OK
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if a and f() and False and g() and b: # SIM222
|
if a and f() and False and g() and b: # SIM223
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if False and f() and a and g() and b: # SIM222
|
if False and f() and a and g() and b: # SIM223
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if a and False and f() and b and g(): # SIM222
|
if a and False and f() and b and g(): # SIM223
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if a or f() or b or g() or True: # OK
|
||||||
|
pass
|
||||||
|
|
||||||
|
if a or f() or True or g() or b: # OK
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True or f() or a or g() or b: # OK
|
||||||
|
pass
|
||||||
|
|
||||||
|
if a or True or f() or b or g(): # OK
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
|
|
@ -503,10 +503,17 @@ pub fn expr_or_not_expr(checker: &mut Checker, expr: &Expr) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_short_circuit(ctx: &Context, expr: &Expr) -> Option<(Location, Location)> {
|
pub fn is_short_circuit(
|
||||||
|
ctx: &Context,
|
||||||
|
expr: &Expr,
|
||||||
|
expected_op: &Boolop,
|
||||||
|
) -> Option<(Location, Location)> {
|
||||||
let ExprKind::BoolOp { op, values, } = &expr.node else {
|
let ExprKind::BoolOp { op, values, } = &expr.node else {
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
|
if op != expected_op {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
let short_circuit_value = match op {
|
let short_circuit_value = match op {
|
||||||
Boolop::And => false,
|
Boolop::And => false,
|
||||||
Boolop::Or => true,
|
Boolop::Or => true,
|
||||||
|
|
@ -551,7 +558,7 @@ pub fn is_short_circuit(ctx: &Context, expr: &Expr) -> Option<(Location, Locatio
|
||||||
|
|
||||||
/// SIM222
|
/// SIM222
|
||||||
pub fn expr_or_true(checker: &mut Checker, expr: &Expr) {
|
pub fn expr_or_true(checker: &mut Checker, expr: &Expr) {
|
||||||
let Some((location, end_location)) = is_short_circuit(&checker.ctx, expr) else {
|
let Some((location, end_location)) = is_short_circuit(&checker.ctx, expr, &Boolop::Or) else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let mut diagnostic = Diagnostic::new(
|
let mut diagnostic = Diagnostic::new(
|
||||||
|
|
@ -573,7 +580,7 @@ pub fn expr_or_true(checker: &mut Checker, expr: &Expr) {
|
||||||
|
|
||||||
/// SIM223
|
/// SIM223
|
||||||
pub fn expr_and_false(checker: &mut Checker, expr: &Expr) {
|
pub fn expr_and_false(checker: &mut Checker, expr: &Expr) {
|
||||||
let Some((location, end_location)) = is_short_circuit(&checker.ctx, expr) else {
|
let Some((location, end_location)) = is_short_circuit(&checker.ctx, expr, &Boolop::And) else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let mut diagnostic = Diagnostic::new(
|
let mut diagnostic = Diagnostic::new(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue