Skip to content

Commit 34c6ba7

Browse files
committed
comparison_chain: do not lint on 2 blocks expression
Clippy should not lint 2 blocks expression for comparison_chain.
1 parent 40bead0 commit 34c6ba7

File tree

3 files changed

+19
-40
lines changed

3 files changed

+19
-40
lines changed

clippy_lints/src/comparison_chain.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,15 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
7575
}
7676

7777
// Check that there exists at least one explicit else condition
78-
let (conds, _) = if_sequence(expr);
78+
let (conds, blocks) = if_sequence(expr);
7979
if conds.len() < 2 {
8080
return;
8181
}
8282

83+
if blocks.len() < 3 {
84+
return;
85+
}
86+
8387
for cond in conds.windows(2) {
8488
if let (&ExprKind::Binary(ref kind1, lhs1, rhs1), &ExprKind::Binary(ref kind2, lhs2, rhs2)) =
8589
(&cond[0].kind, &cond[1].kind)
@@ -125,6 +129,7 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
125129
let ExprKind::Binary(_, lhs, rhs) = conds[0].kind else {
126130
unreachable!();
127131
};
132+
128133
let lhs = Sugg::hir(cx, lhs, "..").maybe_paren();
129134
let rhs = Sugg::hir(cx, rhs, "..").addr();
130135
span_lint_and_sugg(

tests/ui/comparison_chain.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ fn f(x: u8, y: u8, z: u8) {
1212
a()
1313
}
1414

15-
if x > y {
16-
//~^ comparison_chain
17-
15+
// Ignored: Not all cases are covered
16+
if x < y {
1817
a()
19-
} else if x < y {
18+
} else if x > y {
2019
b()
2120
}
2221

@@ -123,9 +122,8 @@ fn g(x: f64, y: f64, z: f64) {
123122
}
124123

125124
fn h<T: Ord>(x: T, y: T, z: T) {
125+
// Ignored: Not all cases are covered
126126
if x > y {
127-
//~^ comparison_chain
128-
129127
a()
130128
} else if x < y {
131129
b()

tests/ui/comparison_chain.stderr

+9-33
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,20 @@
11
error: `if` chain can be rewritten with `match`
2-
--> tests/ui/comparison_chain.rs:15:5
2+
--> tests/ui/comparison_chain.rs:29:5
33
|
44
LL | / if x > y {
55
LL | |
66
LL | |
77
LL | | a()
8-
LL | | } else if x < y {
9-
LL | | b()
8+
... |
9+
LL | | c()
1010
LL | | }
1111
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`
1212
|
1313
= note: `-D clippy::comparison-chain` implied by `-D warnings`
1414
= help: to override `-D warnings` add `#[allow(clippy::comparison_chain)]`
1515

1616
error: `if` chain can be rewritten with `match`
17-
--> tests/ui/comparison_chain.rs:30:5
18-
|
19-
LL | / if x > y {
20-
LL | |
21-
LL | |
22-
LL | | a()
23-
... |
24-
LL | | c()
25-
LL | | }
26-
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`
27-
28-
error: `if` chain can be rewritten with `match`
29-
--> tests/ui/comparison_chain.rs:40:5
17+
--> tests/ui/comparison_chain.rs:39:5
3018
|
3119
LL | / if x > y {
3220
LL | |
@@ -38,7 +26,7 @@ LL | | }
3826
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`
3927

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

5240
error: `if` chain can be rewritten with `match`
53-
--> tests/ui/comparison_chain.rs:126:5
54-
|
55-
LL | / if x > y {
56-
LL | |
57-
LL | |
58-
LL | | a()
59-
LL | | } else if x < y {
60-
LL | | b()
61-
LL | | }
62-
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`
63-
64-
error: `if` chain can be rewritten with `match`
65-
--> tests/ui/comparison_chain.rs:134:5
41+
--> tests/ui/comparison_chain.rs:132:5
6642
|
6743
LL | / if x > y {
6844
LL | |
@@ -74,7 +50,7 @@ LL | | }
7450
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`
7551

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

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

100-
error: aborting due to 8 previous errors
76+
error: aborting due to 6 previous errors
10177

0 commit comments

Comments
 (0)