Improve SpanlessEq

* Don't consider expansions of different macros to be the same, even if they expand to the same tokens
* Don't consider `cfg!` expansions to be equal if they check different configs.
This commit is contained in:
Jason Newcomb 2023-03-10 10:43:04 -05:00
parent 5351170744
commit 58132cb3b0
12 changed files with 135 additions and 34 deletions

View file

@ -1,5 +1,10 @@
//@run-rustfix
#![allow(clippy::assertions_on_constants, clippy::equatable_if_let)]
#![allow(
clippy::assertions_on_constants,
clippy::equatable_if_let,
clippy::nonminimal_bool,
clippy::eq_op
)]
#[rustfmt::skip]
#[warn(clippy::collapsible_if)]

View file

@ -1,5 +1,10 @@
//@run-rustfix
#![allow(clippy::assertions_on_constants, clippy::equatable_if_let)]
#![allow(
clippy::assertions_on_constants,
clippy::equatable_if_let,
clippy::nonminimal_bool,
clippy::eq_op
)]
#[rustfmt::skip]
#[warn(clippy::collapsible_if)]

View file

@ -1,5 +1,5 @@
error: this `if` statement can be collapsed
--> $DIR/collapsible_if.rs:9:5
--> $DIR/collapsible_if.rs:14:5
|
LL | / if x == "hello" {
LL | | if y == "world" {
@ -17,7 +17,7 @@ LL + }
|
error: this `if` statement can be collapsed
--> $DIR/collapsible_if.rs:15:5
--> $DIR/collapsible_if.rs:20:5
|
LL | / if x == "hello" || x == "world" {
LL | | if y == "world" || y == "hello" {
@ -34,7 +34,7 @@ LL + }
|
error: this `if` statement can be collapsed
--> $DIR/collapsible_if.rs:21:5
--> $DIR/collapsible_if.rs:26:5
|
LL | / if x == "hello" && x == "world" {
LL | | if y == "world" || y == "hello" {
@ -51,7 +51,7 @@ LL + }
|
error: this `if` statement can be collapsed
--> $DIR/collapsible_if.rs:27:5
--> $DIR/collapsible_if.rs:32:5
|
LL | / if x == "hello" || x == "world" {
LL | | if y == "world" && y == "hello" {
@ -68,7 +68,7 @@ LL + }
|
error: this `if` statement can be collapsed
--> $DIR/collapsible_if.rs:33:5
--> $DIR/collapsible_if.rs:38:5
|
LL | / if x == "hello" && x == "world" {
LL | | if y == "world" && y == "hello" {
@ -85,7 +85,7 @@ LL + }
|
error: this `if` statement can be collapsed
--> $DIR/collapsible_if.rs:39:5
--> $DIR/collapsible_if.rs:44:5
|
LL | / if 42 == 1337 {
LL | | if 'a' != 'A' {
@ -102,7 +102,7 @@ LL + }
|
error: this `if` statement can be collapsed
--> $DIR/collapsible_if.rs:95:5
--> $DIR/collapsible_if.rs:100:5
|
LL | / if x == "hello" {
LL | | if y == "world" { // Collapsible
@ -119,7 +119,7 @@ LL + }
|
error: this `if` statement can be collapsed
--> $DIR/collapsible_if.rs:154:5
--> $DIR/collapsible_if.rs:159:5
|
LL | / if matches!(true, true) {
LL | | if matches!(true, true) {}
@ -127,7 +127,7 @@ LL | | }
| |_____^ help: collapse nested if block: `if matches!(true, true) && matches!(true, true) {}`
error: this `if` statement can be collapsed
--> $DIR/collapsible_if.rs:159:5
--> $DIR/collapsible_if.rs:164:5
|
LL | / if matches!(true, true) && truth() {
LL | | if matches!(true, true) {}

View file

@ -57,6 +57,16 @@ macro_rules! m {
(foo) => {};
(bar) => {};
}
macro_rules! foo {
() => {
1
};
}
macro_rules! bar {
() => {
1
};
}
fn main() {
let x = 0;
@ -111,4 +121,16 @@ fn main() {
},
_ => 0,
};
let _ = match 0 {
0 => foo!(),
1 => bar!(),
_ => 1,
};
let _ = match 0 {
0 => cfg!(not_enabled),
1 => cfg!(also_not_enabled),
_ => false,
};
}

View file

@ -239,4 +239,10 @@ fn main() {
3 => core::convert::identity::<u32>(todo!()),
_ => 5,
};
let _ = match 0 {
0 => cfg!(not_enable),
1 => cfg!(not_enable),
_ => false,
};
}

View file

@ -192,5 +192,20 @@ note: other arm here
LL | Some(Bar { x: 0, y: 5, .. }) => 1,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 12 previous errors
error: this match arm has an identical body to another arm
--> $DIR/match_same_arms2.rs:245:9
|
LL | 1 => cfg!(not_enable),
| -^^^^^^^^^^^^^^^^^^^^
| |
| help: try merging the arm patterns: `1 | 0`
|
= help: or try changing either arm body
note: other arm here
--> $DIR/match_same_arms2.rs:244:9
|
LL | 0 => cfg!(not_enable),
| ^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 13 previous errors

View file

@ -1,5 +1,6 @@
//@run-rustfix
#![warn(clippy::partialeq_to_none)]
#![allow(clippy::eq_op)]
struct Foobar;

View file

@ -1,5 +1,6 @@
//@run-rustfix
#![warn(clippy::partialeq_to_none)]
#![allow(clippy::eq_op)]
struct Foobar;

View file

@ -1,5 +1,5 @@
error: binary comparison to literal `Option::None`
--> $DIR/partialeq_to_none.rs:14:8
--> $DIR/partialeq_to_none.rs:15:8
|
LL | if f != None { "yay" } else { "nay" }
| ^^^^^^^^^ help: use `Option::is_some()` instead: `f.is_some()`
@ -7,55 +7,55 @@ LL | if f != None { "yay" } else { "nay" }
= note: `-D clippy::partialeq-to-none` implied by `-D warnings`
error: binary comparison to literal `Option::None`
--> $DIR/partialeq_to_none.rs:44:13
--> $DIR/partialeq_to_none.rs:45:13
|
LL | let _ = x == None;
| ^^^^^^^^^ help: use `Option::is_none()` instead: `x.is_none()`
error: binary comparison to literal `Option::None`
--> $DIR/partialeq_to_none.rs:45:13
--> $DIR/partialeq_to_none.rs:46:13
|
LL | let _ = x != None;
| ^^^^^^^^^ help: use `Option::is_some()` instead: `x.is_some()`
error: binary comparison to literal `Option::None`
--> $DIR/partialeq_to_none.rs:46:13
--> $DIR/partialeq_to_none.rs:47:13
|
LL | let _ = None == x;
| ^^^^^^^^^ help: use `Option::is_none()` instead: `x.is_none()`
error: binary comparison to literal `Option::None`
--> $DIR/partialeq_to_none.rs:47:13
--> $DIR/partialeq_to_none.rs:48:13
|
LL | let _ = None != x;
| ^^^^^^^^^ help: use `Option::is_some()` instead: `x.is_some()`
error: binary comparison to literal `Option::None`
--> $DIR/partialeq_to_none.rs:49:8
--> $DIR/partialeq_to_none.rs:50:8
|
LL | if foobar() == None {}
| ^^^^^^^^^^^^^^^^ help: use `Option::is_none()` instead: `foobar().is_none()`
error: binary comparison to literal `Option::None`
--> $DIR/partialeq_to_none.rs:51:8
--> $DIR/partialeq_to_none.rs:52:8
|
LL | if bar().ok() != None {}
| ^^^^^^^^^^^^^^^^^^ help: use `Option::is_some()` instead: `bar().ok().is_some()`
error: binary comparison to literal `Option::None`
--> $DIR/partialeq_to_none.rs:53:13
--> $DIR/partialeq_to_none.rs:54:13
|
LL | let _ = Some(1 + 2) != None;
| ^^^^^^^^^^^^^^^^^^^ help: use `Option::is_some()` instead: `Some(1 + 2).is_some()`
error: binary comparison to literal `Option::None`
--> $DIR/partialeq_to_none.rs:55:13
--> $DIR/partialeq_to_none.rs:56:13
|
LL | let _ = { Some(0) } == None;
| ^^^^^^^^^^^^^^^^^^^ help: use `Option::is_none()` instead: `{ Some(0) }.is_none()`
error: binary comparison to literal `Option::None`
--> $DIR/partialeq_to_none.rs:57:13
--> $DIR/partialeq_to_none.rs:58:13
|
LL | let _ = {
| _____________^
@ -77,31 +77,31 @@ LL ~ }.is_some();
|
error: binary comparison to literal `Option::None`
--> $DIR/partialeq_to_none.rs:67:13
--> $DIR/partialeq_to_none.rs:68:13
|
LL | let _ = optref() == &&None;
| ^^^^^^^^^^^^^^^^^^ help: use `Option::is_none()` instead: `optref().is_none()`
error: binary comparison to literal `Option::None`
--> $DIR/partialeq_to_none.rs:68:13
--> $DIR/partialeq_to_none.rs:69:13
|
LL | let _ = &&None != optref();
| ^^^^^^^^^^^^^^^^^^ help: use `Option::is_some()` instead: `optref().is_some()`
error: binary comparison to literal `Option::None`
--> $DIR/partialeq_to_none.rs:69:13
--> $DIR/partialeq_to_none.rs:70:13
|
LL | let _ = **optref() == None;
| ^^^^^^^^^^^^^^^^^^ help: use `Option::is_none()` instead: `optref().is_none()`
error: binary comparison to literal `Option::None`
--> $DIR/partialeq_to_none.rs:70:13
--> $DIR/partialeq_to_none.rs:71:13
|
LL | let _ = &None != *optref();
| ^^^^^^^^^^^^^^^^^^ help: use `Option::is_some()` instead: `optref().is_some()`
error: binary comparison to literal `Option::None`
--> $DIR/partialeq_to_none.rs:73:13
--> $DIR/partialeq_to_none.rs:74:13
|
LL | let _ = None != *x;
| ^^^^^^^^^^ help: use `Option::is_some()` instead: `(*x).is_some()`