Skip to content

Commit 59740a8

Browse files
authored
Better help message for comparison_chain lint (rust-lang#13762)
changelog: [`comparison_chain`]: give explicit help message showing a clear suggestion Close rust-lang#13739
2 parents 2a28347 + 69411e0 commit 59740a8

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

clippy_lints/src/comparison_chain.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::sugg::Sugg;
23
use clippy_utils::ty::implements_trait;
34
use clippy_utils::{SpanlessEq, if_sequence, is_else_clause, is_in_const_context};
5+
use rustc_errors::Applicability;
46
use rustc_hir::{BinOpKind, Expr, ExprKind};
57
use rustc_lint::{LateContext, LateLintPass};
68
use rustc_session::declare_lint_pass;
@@ -120,13 +122,19 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
120122
return;
121123
}
122124
}
123-
span_lint_and_help(
125+
let ExprKind::Binary(_, lhs, rhs) = conds[0].kind else {
126+
unreachable!();
127+
};
128+
let lhs = Sugg::hir(cx, lhs, "..").maybe_par();
129+
let rhs = Sugg::hir(cx, rhs, "..").addr();
130+
span_lint_and_sugg(
124131
cx,
125132
COMPARISON_CHAIN,
126133
expr.span,
127134
"`if` chain can be rewritten with `match`",
128-
None,
129-
"consider rewriting the `if` chain to use `cmp` and `match`",
135+
"consider rewriting the `if` chain with `match`",
136+
format!("match {lhs}.cmp({rhs}) {{...}}"),
137+
Applicability::HasPlaceholders,
130138
);
131139
}
132140
}

tests/ui/comparison_chain.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@no-rustfix
12
#![allow(dead_code)]
23
#![warn(clippy::comparison_chain)]
34

@@ -238,4 +239,16 @@ const fn sign_i8(n: i8) -> Sign {
238239
}
239240
}
240241

242+
fn needs_parens() -> &'static str {
243+
let (x, y) = (1, 2);
244+
if x + 1 > y * 2 {
245+
//~^ ERROR: `if` chain can be rewritten with `match`
246+
"aa"
247+
} else if x + 1 < y * 2 {
248+
"bb"
249+
} else {
250+
"cc"
251+
}
252+
}
253+
241254
fn main() {}

tests/ui/comparison_chain.stderr

+26-27
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
error: `if` chain can be rewritten with `match`
2-
--> tests/ui/comparison_chain.rs:14:5
2+
--> tests/ui/comparison_chain.rs:15:5
33
|
44
LL | / if x > y {
55
LL | |
66
LL | | a()
77
LL | | } else if x < y {
88
LL | | b()
99
LL | | }
10-
| |_____^
10+
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`
1111
|
12-
= help: consider rewriting the `if` chain to use `cmp` and `match`
1312
= note: `-D clippy::comparison-chain` implied by `-D warnings`
1413
= help: to override `-D warnings` add `#[allow(clippy::comparison_chain)]`
1514

1615
error: `if` chain can be rewritten with `match`
17-
--> tests/ui/comparison_chain.rs:28:5
16+
--> tests/ui/comparison_chain.rs:29:5
1817
|
1918
LL | / if x > y {
2019
LL | |
@@ -23,12 +22,10 @@ LL | | } else if x < y {
2322
... |
2423
LL | | c()
2524
LL | | }
26-
| |_____^
27-
|
28-
= help: consider rewriting the `if` chain to use `cmp` and `match`
25+
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`
2926

3027
error: `if` chain can be rewritten with `match`
31-
--> tests/ui/comparison_chain.rs:37:5
28+
--> tests/ui/comparison_chain.rs:38:5
3229
|
3330
LL | / if x > y {
3431
LL | |
@@ -37,12 +34,10 @@ LL | | } else if y > x {
3734
... |
3835
LL | | c()
3936
LL | | }
40-
| |_____^
41-
|
42-
= help: consider rewriting the `if` chain to use `cmp` and `match`
37+
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`
4338

4439
error: `if` chain can be rewritten with `match`
45-
--> tests/ui/comparison_chain.rs:46:5
40+
--> tests/ui/comparison_chain.rs:47:5
4641
|
4742
LL | / if x > 1 {
4843
LL | |
@@ -51,25 +46,21 @@ LL | | } else if x < 1 {
5146
... |
5247
LL | | c()
5348
LL | | }
54-
| |_____^
55-
|
56-
= help: consider rewriting the `if` chain to use `cmp` and `match`
49+
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&1) {...}`
5750

5851
error: `if` chain can be rewritten with `match`
59-
--> tests/ui/comparison_chain.rs:121:5
52+
--> tests/ui/comparison_chain.rs:122:5
6053
|
6154
LL | / if x > y {
6255
LL | |
6356
LL | | a()
6457
LL | | } else if x < y {
6558
LL | | b()
6659
LL | | }
67-
| |_____^
68-
|
69-
= help: consider rewriting the `if` chain to use `cmp` and `match`
60+
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`
7061

7162
error: `if` chain can be rewritten with `match`
72-
--> tests/ui/comparison_chain.rs:128:5
63+
--> tests/ui/comparison_chain.rs:129:5
7364
|
7465
LL | / if x > y {
7566
LL | |
@@ -78,12 +69,10 @@ LL | | } else if x < y {
7869
... |
7970
LL | | c()
8071
LL | | }
81-
| |_____^
82-
|
83-
= help: consider rewriting the `if` chain to use `cmp` and `match`
72+
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`
8473

8574
error: `if` chain can be rewritten with `match`
86-
--> tests/ui/comparison_chain.rs:137:5
75+
--> tests/ui/comparison_chain.rs:138:5
8776
|
8877
LL | / if x > y {
8978
LL | |
@@ -92,9 +81,19 @@ LL | | } else if y > x {
9281
... |
9382
LL | | c()
9483
LL | | }
95-
| |_____^
84+
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`
85+
86+
error: `if` chain can be rewritten with `match`
87+
--> tests/ui/comparison_chain.rs:244:5
9688
|
97-
= help: consider rewriting the `if` chain to use `cmp` and `match`
89+
LL | / if x + 1 > y * 2 {
90+
LL | |
91+
LL | | "aa"
92+
LL | | } else if x + 1 < y * 2 {
93+
... |
94+
LL | | "cc"
95+
LL | | }
96+
| |_____^ help: consider rewriting the `if` chain with `match`: `match (x + 1).cmp(&(y * 2)) {...}`
9897

99-
error: aborting due to 7 previous errors
98+
error: aborting due to 8 previous errors
10099

0 commit comments

Comments
 (0)