double_comparison: add missing cases

Add checks for expressions such as `x != y && x >= y` and `x != y && x <= y`.
This commit is contained in:
Ian D. Bollinger 2025-11-04 13:00:02 -05:00
parent ad7fc4f468
commit d160cb7313
4 changed files with 69 additions and 1 deletions

View file

@ -39,6 +39,18 @@ pub(super) fn check(cx: &LateContext<'_>, op: BinOpKind, lhs: &Expr<'_>, rhs: &E
| (BinOpKind::And, BinOpKind::Ge, BinOpKind::Le) => {
"=="
},
// x != y && x >= y => x > y
(BinOpKind::And, BinOpKind::Ne, BinOpKind::Ge)
// x >= y && x != y => x > y
| (BinOpKind::And, BinOpKind::Ge, BinOpKind::Ne) => {
">"
},
// x != y && x <= y => x < y
(BinOpKind::And, BinOpKind::Ne, BinOpKind::Le)
// x <= y && x != y => x < y
| (BinOpKind::And, BinOpKind::Le, BinOpKind::Ne) => {
"<"
},
_ => return,
};

View file

@ -35,4 +35,20 @@ fn main() {
//~^ double_comparisons
// do something
}
if x < y {
//~^ double_comparisons
// do something
}
if x < y {
//~^ double_comparisons
// do something
}
if x > y {
//~^ double_comparisons
// do something
}
if x > y {
//~^ double_comparisons
// do something
}
}

View file

@ -35,4 +35,20 @@ fn main() {
//~^ double_comparisons
// do something
}
if x != y && x <= y {
//~^ double_comparisons
// do something
}
if x <= y && x != y {
//~^ double_comparisons
// do something
}
if x != y && x >= y {
//~^ double_comparisons
// do something
}
if x >= y && x != y {
//~^ double_comparisons
// do something
}
}

View file

@ -49,5 +49,29 @@ error: this binary expression can be simplified
LL | if x >= y && x <= y {
| ^^^^^^^^^^^^^^^^ help: try: `x == y`
error: aborting due to 8 previous errors
error: this binary expression can be simplified
--> tests/ui/double_comparison.rs:38:8
|
LL | if x != y && x <= y {
| ^^^^^^^^^^^^^^^^ help: try: `x < y`
error: this binary expression can be simplified
--> tests/ui/double_comparison.rs:42:8
|
LL | if x <= y && x != y {
| ^^^^^^^^^^^^^^^^ help: try: `x < y`
error: this binary expression can be simplified
--> tests/ui/double_comparison.rs:46:8
|
LL | if x != y && x >= y {
| ^^^^^^^^^^^^^^^^ help: try: `x > y`
error: this binary expression can be simplified
--> tests/ui/double_comparison.rs:50:8
|
LL | if x >= y && x != y {
| ^^^^^^^^^^^^^^^^ help: try: `x > y`
error: aborting due to 12 previous errors