Skip to content

Commit ea25f04

Browse files
committed
Auto merge of #3977 - phansch:add_rustfix_bool_comparison, r=flip1995
Add run-rustfix for bool_comparison lint cc #3630
2 parents 1936368 + 90ddf0d commit ea25f04

File tree

3 files changed

+129
-14
lines changed

3 files changed

+129
-14
lines changed

tests/ui/bool_comparison.fixed

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// run-rustfix
2+
3+
#[warn(clippy::bool_comparison)]
4+
fn main() {
5+
let x = true;
6+
if x {
7+
"yes"
8+
} else {
9+
"no"
10+
};
11+
if !x {
12+
"yes"
13+
} else {
14+
"no"
15+
};
16+
if x {
17+
"yes"
18+
} else {
19+
"no"
20+
};
21+
if !x {
22+
"yes"
23+
} else {
24+
"no"
25+
};
26+
if !x {
27+
"yes"
28+
} else {
29+
"no"
30+
};
31+
if x {
32+
"yes"
33+
} else {
34+
"no"
35+
};
36+
if !x {
37+
"yes"
38+
} else {
39+
"no"
40+
};
41+
if x {
42+
"yes"
43+
} else {
44+
"no"
45+
};
46+
if !x {
47+
"yes"
48+
} else {
49+
"no"
50+
};
51+
if x {
52+
"yes"
53+
} else {
54+
"no"
55+
};
56+
if x {
57+
"yes"
58+
} else {
59+
"no"
60+
};
61+
if !x {
62+
"yes"
63+
} else {
64+
"no"
65+
};
66+
let y = true;
67+
if !x & y {
68+
"yes"
69+
} else {
70+
"no"
71+
};
72+
if x & !y {
73+
"yes"
74+
} else {
75+
"no"
76+
};
77+
}
78+
79+
#[allow(dead_code)]
80+
fn issue3703() {
81+
struct Foo;
82+
impl PartialEq<bool> for Foo {
83+
fn eq(&self, _: &bool) -> bool {
84+
true
85+
}
86+
}
87+
impl PartialEq<Foo> for bool {
88+
fn eq(&self, _: &Foo) -> bool {
89+
true
90+
}
91+
}
92+
impl PartialOrd<bool> for Foo {
93+
fn partial_cmp(&self, _: &bool) -> Option<std::cmp::Ordering> {
94+
None
95+
}
96+
}
97+
impl PartialOrd<Foo> for bool {
98+
fn partial_cmp(&self, _: &Foo) -> Option<std::cmp::Ordering> {
99+
None
100+
}
101+
}
102+
103+
if Foo == true {}
104+
if true == Foo {}
105+
if Foo != true {}
106+
if true != Foo {}
107+
if Foo == false {}
108+
if false == Foo {}
109+
if Foo != false {}
110+
if false != Foo {}
111+
if Foo < false {}
112+
if false < Foo {}
113+
}

tests/ui/bool_comparison.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// run-rustfix
2+
13
#[warn(clippy::bool_comparison)]
24
fn main() {
35
let x = true;

tests/ui/bool_comparison.stderr

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,85 @@
11
error: equality checks against true are unnecessary
2-
--> $DIR/bool_comparison.rs:4:8
2+
--> $DIR/bool_comparison.rs:6:8
33
|
44
LL | if x == true {
55
| ^^^^^^^^^ help: try simplifying it as shown: `x`
66
|
77
= note: `-D clippy::bool-comparison` implied by `-D warnings`
88

99
error: equality checks against false can be replaced by a negation
10-
--> $DIR/bool_comparison.rs:9:8
10+
--> $DIR/bool_comparison.rs:11:8
1111
|
1212
LL | if x == false {
1313
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
1414

1515
error: equality checks against true are unnecessary
16-
--> $DIR/bool_comparison.rs:14:8
16+
--> $DIR/bool_comparison.rs:16:8
1717
|
1818
LL | if true == x {
1919
| ^^^^^^^^^ help: try simplifying it as shown: `x`
2020

2121
error: equality checks against false can be replaced by a negation
22-
--> $DIR/bool_comparison.rs:19:8
22+
--> $DIR/bool_comparison.rs:21:8
2323
|
2424
LL | if false == x {
2525
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
2626

2727
error: inequality checks against true can be replaced by a negation
28-
--> $DIR/bool_comparison.rs:24:8
28+
--> $DIR/bool_comparison.rs:26:8
2929
|
3030
LL | if x != true {
3131
| ^^^^^^^^^ help: try simplifying it as shown: `!x`
3232

3333
error: inequality checks against false are unnecessary
34-
--> $DIR/bool_comparison.rs:29:8
34+
--> $DIR/bool_comparison.rs:31:8
3535
|
3636
LL | if x != false {
3737
| ^^^^^^^^^^ help: try simplifying it as shown: `x`
3838

3939
error: inequality checks against true can be replaced by a negation
40-
--> $DIR/bool_comparison.rs:34:8
40+
--> $DIR/bool_comparison.rs:36:8
4141
|
4242
LL | if true != x {
4343
| ^^^^^^^^^ help: try simplifying it as shown: `!x`
4444

4545
error: inequality checks against false are unnecessary
46-
--> $DIR/bool_comparison.rs:39:8
46+
--> $DIR/bool_comparison.rs:41:8
4747
|
4848
LL | if false != x {
4949
| ^^^^^^^^^^ help: try simplifying it as shown: `x`
5050

5151
error: less than comparison against true can be replaced by a negation
52-
--> $DIR/bool_comparison.rs:44:8
52+
--> $DIR/bool_comparison.rs:46:8
5353
|
5454
LL | if x < true {
5555
| ^^^^^^^^ help: try simplifying it as shown: `!x`
5656

5757
error: greater than checks against false are unnecessary
58-
--> $DIR/bool_comparison.rs:49:8
58+
--> $DIR/bool_comparison.rs:51:8
5959
|
6060
LL | if false < x {
6161
| ^^^^^^^^^ help: try simplifying it as shown: `x`
6262

6363
error: greater than checks against false are unnecessary
64-
--> $DIR/bool_comparison.rs:54:8
64+
--> $DIR/bool_comparison.rs:56:8
6565
|
6666
LL | if x > false {
6767
| ^^^^^^^^^ help: try simplifying it as shown: `x`
6868

6969
error: less than comparison against true can be replaced by a negation
70-
--> $DIR/bool_comparison.rs:59:8
70+
--> $DIR/bool_comparison.rs:61:8
7171
|
7272
LL | if true > x {
7373
| ^^^^^^^^ help: try simplifying it as shown: `!x`
7474

7575
error: order comparisons between booleans can be simplified
76-
--> $DIR/bool_comparison.rs:65:8
76+
--> $DIR/bool_comparison.rs:67:8
7777
|
7878
LL | if x < y {
7979
| ^^^^^ help: try simplifying it as shown: `!x & y`
8080

8181
error: order comparisons between booleans can be simplified
82-
--> $DIR/bool_comparison.rs:70:8
82+
--> $DIR/bool_comparison.rs:72:8
8383
|
8484
LL | if x > y {
8585
| ^^^^^ help: try simplifying it as shown: `x & !y`

0 commit comments

Comments
 (0)