Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions clippy_lints/src/operators/double_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
16 changes: 16 additions & 0 deletions tests/ui/double_comparison.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
16 changes: 16 additions & 0 deletions tests/ui/double_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
26 changes: 25 additions & 1 deletion tests/ui/double_comparison.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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