Skip to content

Commit 3f1a571

Browse files
committed
Better help message for comparison_chain lint
1 parent af1f78a commit 3f1a571

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

clippy_lints/src/comparison_chain.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
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};
45
use rustc_hir::{BinOpKind, Expr, ExprKind};
@@ -120,13 +121,18 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
120121
return;
121122
}
122123
}
124+
let ExprKind::Binary(_, lhs, rhs) = conds[0].kind else {
125+
unreachable!();
126+
};
127+
let lhs = Sugg::hir(cx, lhs, "..").maybe_par();
128+
let rhs = Sugg::hir(cx, rhs, "..").addr();
123129
span_lint_and_help(
124130
cx,
125131
COMPARISON_CHAIN,
126132
expr.span,
127133
"`if` chain can be rewritten with `match`",
128134
None,
129-
"consider rewriting the `if` chain to use `cmp` and `match`",
135+
format!("consider rewriting the `if` chain with: `match {lhs}.cmp({rhs}) {{...}}`"),
130136
);
131137
}
132138
}

tests/ui/comparison_chain.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,16 @@ const fn sign_i8(n: i8) -> Sign {
238238
}
239239
}
240240

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

tests/ui/comparison_chain.stderr

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | | b()
99
LL | | }
1010
| |_____^
1111
|
12-
= help: consider rewriting the `if` chain to use `cmp` and `match`
12+
= help: consider rewriting the `if` chain with: `match x.cmp(&y) {...}`
1313
= note: `-D clippy::comparison-chain` implied by `-D warnings`
1414
= help: to override `-D warnings` add `#[allow(clippy::comparison_chain)]`
1515

@@ -25,7 +25,7 @@ LL | | c()
2525
LL | | }
2626
| |_____^
2727
|
28-
= help: consider rewriting the `if` chain to use `cmp` and `match`
28+
= help: consider rewriting the `if` chain with: `match x.cmp(&y) {...}`
2929

3030
error: `if` chain can be rewritten with `match`
3131
--> tests/ui/comparison_chain.rs:37:5
@@ -39,7 +39,7 @@ LL | | c()
3939
LL | | }
4040
| |_____^
4141
|
42-
= help: consider rewriting the `if` chain to use `cmp` and `match`
42+
= help: consider rewriting the `if` chain with: `match x.cmp(&y) {...}`
4343

4444
error: `if` chain can be rewritten with `match`
4545
--> tests/ui/comparison_chain.rs:46:5
@@ -53,7 +53,7 @@ LL | | c()
5353
LL | | }
5454
| |_____^
5555
|
56-
= help: consider rewriting the `if` chain to use `cmp` and `match`
56+
= help: consider rewriting the `if` chain with: `match x.cmp(&1) {...}`
5757

5858
error: `if` chain can be rewritten with `match`
5959
--> tests/ui/comparison_chain.rs:121:5
@@ -66,7 +66,7 @@ LL | | b()
6666
LL | | }
6767
| |_____^
6868
|
69-
= help: consider rewriting the `if` chain to use `cmp` and `match`
69+
= help: consider rewriting the `if` chain with: `match x.cmp(&y) {...}`
7070

7171
error: `if` chain can be rewritten with `match`
7272
--> tests/ui/comparison_chain.rs:128:5
@@ -80,7 +80,7 @@ LL | | c()
8080
LL | | }
8181
| |_____^
8282
|
83-
= help: consider rewriting the `if` chain to use `cmp` and `match`
83+
= help: consider rewriting the `if` chain with: `match x.cmp(&y) {...}`
8484

8585
error: `if` chain can be rewritten with `match`
8686
--> tests/ui/comparison_chain.rs:137:5
@@ -94,7 +94,21 @@ LL | | c()
9494
LL | | }
9595
| |_____^
9696
|
97-
= help: consider rewriting the `if` chain to use `cmp` and `match`
97+
= help: consider rewriting the `if` chain with: `match x.cmp(&y) {...}`
9898

99-
error: aborting due to 7 previous errors
99+
error: `if` chain can be rewritten with `match`
100+
--> tests/ui/comparison_chain.rs:243:5
101+
|
102+
LL | / if x + 1 > y * 2 {
103+
LL | |
104+
LL | | "aa"
105+
LL | | } else if x + 1 < y * 2 {
106+
... |
107+
LL | | "cc"
108+
LL | | }
109+
| |_____^
110+
|
111+
= help: consider rewriting the `if` chain with: `match (x + 1).cmp(&(y * 2)) {...}`
112+
113+
error: aborting due to 8 previous errors
100114

0 commit comments

Comments
 (0)