Skip to content
Merged
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
7 changes: 6 additions & 1 deletion clippy_lints/src/comparison_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
}

// Check that there exists at least one explicit else condition
let (conds, _) = if_sequence(expr);
let (conds, blocks) = if_sequence(expr);
if conds.len() < 2 {
return;
}

if blocks.len() < 3 {
return;
}

for cond in conds.windows(2) {
if let (&ExprKind::Binary(ref kind1, lhs1, rhs1), &ExprKind::Binary(ref kind2, lhs2, rhs2)) =
(&cond[0].kind, &cond[1].kind)
Expand Down Expand Up @@ -125,6 +129,7 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
let ExprKind::Binary(_, lhs, rhs) = conds[0].kind else {
unreachable!();
};

let lhs = Sugg::hir(cx, lhs, "..").maybe_paren();
let rhs = Sugg::hir(cx, rhs, "..").addr();
span_lint_and_sugg(
Expand Down
10 changes: 4 additions & 6 deletions tests/ui/comparison_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ fn f(x: u8, y: u8, z: u8) {
a()
}

if x > y {
//~^ comparison_chain

// Ignored: Not all cases are covered
if x < y {
a()
} else if x < y {
} else if x > y {
b()
}

Expand Down Expand Up @@ -123,9 +122,8 @@ fn g(x: f64, y: f64, z: f64) {
}

fn h<T: Ord>(x: T, y: T, z: T) {
// Ignored: Not all cases are covered
if x > y {
//~^ comparison_chain

a()
} else if x < y {
b()
Expand Down
42 changes: 9 additions & 33 deletions tests/ui/comparison_chain.stderr
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
error: `if` chain can be rewritten with `match`
--> tests/ui/comparison_chain.rs:15:5
--> tests/ui/comparison_chain.rs:29:5
|
LL | / if x > y {
LL | |
LL | |
LL | | a()
LL | | } else if x < y {
LL | | b()
... |
LL | | c()
LL | | }
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`
|
= note: `-D clippy::comparison-chain` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::comparison_chain)]`

error: `if` chain can be rewritten with `match`
--> tests/ui/comparison_chain.rs:30:5
|
LL | / if x > y {
LL | |
LL | |
LL | | a()
... |
LL | | c()
LL | | }
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`

error: `if` chain can be rewritten with `match`
--> tests/ui/comparison_chain.rs:40:5
--> tests/ui/comparison_chain.rs:39:5
|
LL | / if x > y {
LL | |
Expand All @@ -38,7 +26,7 @@ LL | | }
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`

error: `if` chain can be rewritten with `match`
--> tests/ui/comparison_chain.rs:50:5
--> tests/ui/comparison_chain.rs:49:5
|
LL | / if x > 1 {
LL | |
Expand All @@ -50,19 +38,7 @@ LL | | }
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&1) {...}`

error: `if` chain can be rewritten with `match`
--> tests/ui/comparison_chain.rs:126:5
|
LL | / if x > y {
LL | |
LL | |
LL | | a()
LL | | } else if x < y {
LL | | b()
LL | | }
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`

error: `if` chain can be rewritten with `match`
--> tests/ui/comparison_chain.rs:134:5
--> tests/ui/comparison_chain.rs:132:5
|
LL | / if x > y {
LL | |
Expand All @@ -74,7 +50,7 @@ LL | | }
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`

error: `if` chain can be rewritten with `match`
--> tests/ui/comparison_chain.rs:144:5
--> tests/ui/comparison_chain.rs:142:5
|
LL | / if x > y {
LL | |
Expand All @@ -86,7 +62,7 @@ LL | | }
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`

error: `if` chain can be rewritten with `match`
--> tests/ui/comparison_chain.rs:251:5
--> tests/ui/comparison_chain.rs:249:5
|
LL | / if x + 1 > y * 2 {
LL | |
Expand All @@ -97,5 +73,5 @@ LL | | "cc"
LL | | }
| |_____^ help: consider rewriting the `if` chain with `match`: `match (x + 1).cmp(&(y * 2)) {...}`

error: aborting due to 8 previous errors
error: aborting due to 6 previous errors