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:
parent
ad7fc4f468
commit
d160cb7313
4 changed files with 69 additions and 1 deletions
|
|
@ -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,
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue