Better help message for comparison_chain lint (#13762)

changelog: [`comparison_chain`]: give explicit help message showing a
clear suggestion

Close #13739
This commit is contained in:
Timo 2024-12-10 21:11:42 +00:00 committed by GitHub
commit 59740a8eb1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 51 additions and 31 deletions

View file

@ -1,3 +1,4 @@
//@no-rustfix
#![allow(dead_code)]
#![warn(clippy::comparison_chain)]
@ -238,4 +239,16 @@ const fn sign_i8(n: i8) -> Sign {
}
}
fn needs_parens() -> &'static str {
let (x, y) = (1, 2);
if x + 1 > y * 2 {
//~^ ERROR: `if` chain can be rewritten with `match`
"aa"
} else if x + 1 < y * 2 {
"bb"
} else {
"cc"
}
}
fn main() {}

View file

@ -1,5 +1,5 @@
error: `if` chain can be rewritten with `match`
--> tests/ui/comparison_chain.rs:14:5
--> tests/ui/comparison_chain.rs:15:5
|
LL | / if x > y {
LL | |
@ -7,14 +7,13 @@ LL | | a()
LL | | } else if x < y {
LL | | b()
LL | | }
| |_____^
| |_____^ help: consider rewriting the `if` chain with `match`: `match x.cmp(&y) {...}`
|
= help: consider rewriting the `if` chain to use `cmp` and `match`
= 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:28:5
--> tests/ui/comparison_chain.rs:29:5
|
LL | / if x > y {
LL | |
@ -23,12 +22,10 @@ LL | | } else if x < y {
... |
LL | | c()
LL | | }
| |_____^
|
= help: consider rewriting the `if` chain to use `cmp` and `match`
| |_____^ 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:37:5
--> tests/ui/comparison_chain.rs:38:5
|
LL | / if x > y {
LL | |
@ -37,12 +34,10 @@ LL | | } else if y > x {
... |
LL | | c()
LL | | }
| |_____^
|
= help: consider rewriting the `if` chain to use `cmp` and `match`
| |_____^ 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:46:5
--> tests/ui/comparison_chain.rs:47:5
|
LL | / if x > 1 {
LL | |
@ -51,12 +46,10 @@ LL | | } else if x < 1 {
... |
LL | | c()
LL | | }
| |_____^
|
= help: consider rewriting the `if` chain to use `cmp` and `match`
| |_____^ 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:121:5
--> tests/ui/comparison_chain.rs:122:5
|
LL | / if x > y {
LL | |
@ -64,12 +57,10 @@ LL | | a()
LL | | } else if x < y {
LL | | b()
LL | | }
| |_____^
|
= help: consider rewriting the `if` chain to use `cmp` and `match`
| |_____^ 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:128:5
--> tests/ui/comparison_chain.rs:129:5
|
LL | / if x > y {
LL | |
@ -78,12 +69,10 @@ LL | | } else if x < y {
... |
LL | | c()
LL | | }
| |_____^
|
= help: consider rewriting the `if` chain to use `cmp` and `match`
| |_____^ 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:137:5
--> tests/ui/comparison_chain.rs:138:5
|
LL | / if x > y {
LL | |
@ -92,9 +81,19 @@ LL | | } else if y > x {
... |
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:244:5
|
= help: consider rewriting the `if` chain to use `cmp` and `match`
LL | / if x + 1 > y * 2 {
LL | |
LL | | "aa"
LL | | } else if x + 1 < y * 2 {
... |
LL | | "cc"
LL | | }
| |_____^ help: consider rewriting the `if` chain with `match`: `match (x + 1).cmp(&(y * 2)) {...}`
error: aborting due to 7 previous errors
error: aborting due to 8 previous errors