Ignore multi-comparisons in `repeated-equality-comparison-target` (#6705)

Given `foo == "a" == "b" or foo == "c"`, we were suggesting `foo in
{"a", "b", "c"}`.
This commit is contained in:
Charlie Marsh 2023-08-20 10:41:10 -04:00 committed by GitHub
parent 129b19050a
commit a742a562fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 15 deletions

View File

@ -32,3 +32,7 @@ foo not in {"a", "b", "c"} # Uses membership test already.
foo == "a" # Single comparison. foo == "a" # Single comparison.
foo != "a" # Single comparison. foo != "a" # Single comparison.
foo == "a" == "b" or foo == "c" # Multiple comparisons.
foo == bar == "b" or foo == "c" # Multiple comparisons.

View File

@ -111,7 +111,11 @@ fn is_allowed_value(bool_op: BoolOp, value: &Expr) -> bool {
return false; return false;
}; };
ops.iter().all(|op| { // Ignore, e.g., `foo == bar == baz`.
let [op] = ops.as_slice() else {
return false;
};
if match bool_op { if match bool_op {
BoolOp::Or => !matches!(op, CmpOp::Eq), BoolOp::Or => !matches!(op, CmpOp::Eq),
BoolOp::And => !matches!(op, CmpOp::NotEq), BoolOp::And => !matches!(op, CmpOp::NotEq),
@ -128,7 +132,6 @@ fn is_allowed_value(bool_op: BoolOp, value: &Expr) -> bool {
} }
true true
})
} }
/// Generate a string like `obj in (a, b, c)` or `obj not in (a, b, c)`. /// Generate a string like `obj in (a, b, c)` or `obj not in (a, b, c)`.