Skip to content

Commit e76ef1d

Browse files
committed
Auto merge of #4339 - phansch:rustfix_needless_bool, r=flip1995
Add run-rustfix for needless_bool lint This splits up the needless_bool tests into `fixable.rs` and `simple.rs`. `simple.rs` contains the code that triggers the lint diagnostic without a suggestion. changelog: none cc #3630
2 parents 286d528 + 39c8f84 commit e76ef1d

File tree

5 files changed

+221
-100
lines changed

5 files changed

+221
-100
lines changed

tests/ui/needless_bool/fixable.fixed

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::needless_bool)]
4+
#![allow(
5+
unused,
6+
dead_code,
7+
clippy::no_effect,
8+
clippy::if_same_then_else,
9+
clippy::needless_return
10+
)]
11+
12+
use std::cell::Cell;
13+
14+
macro_rules! bool_comparison_trigger {
15+
($($i:ident: $def:expr, $stb:expr );+ $(;)*) => (
16+
17+
#[derive(Clone)]
18+
pub struct Trigger {
19+
$($i: (Cell<bool>, bool, bool)),+
20+
}
21+
22+
#[allow(dead_code)]
23+
impl Trigger {
24+
pub fn trigger(&self, key: &str) -> bool {
25+
$(
26+
if let stringify!($i) = key {
27+
return self.$i.1 && self.$i.2 == $def;
28+
}
29+
)+
30+
false
31+
}
32+
}
33+
)
34+
}
35+
36+
fn main() {
37+
let x = true;
38+
let y = false;
39+
x;
40+
!x;
41+
!(x && y);
42+
if x {
43+
x
44+
} else {
45+
false
46+
}; // would also be questionable, but we don't catch this yet
47+
bool_ret3(x);
48+
bool_ret4(x);
49+
bool_ret5(x, x);
50+
bool_ret6(x, x);
51+
needless_bool(x);
52+
needless_bool2(x);
53+
needless_bool3(x);
54+
}
55+
56+
fn bool_ret3(x: bool) -> bool {
57+
return x;
58+
}
59+
60+
fn bool_ret4(x: bool) -> bool {
61+
return !x;
62+
}
63+
64+
fn bool_ret5(x: bool, y: bool) -> bool {
65+
return x && y;
66+
}
67+
68+
fn bool_ret6(x: bool, y: bool) -> bool {
69+
return !(x && y);
70+
}
71+
72+
fn needless_bool(x: bool) {
73+
if x {};
74+
}
75+
76+
fn needless_bool2(x: bool) {
77+
if !x {};
78+
}
79+
80+
fn needless_bool3(x: bool) {
81+
bool_comparison_trigger! {
82+
test_one: false, false;
83+
test_three: false, false;
84+
test_two: true, true;
85+
}
86+
87+
if x {};
88+
if !x {};
89+
}
90+
91+
fn needless_bool_in_the_suggestion_wraps_the_predicate_of_if_else_statement_in_brackets() {
92+
let b = false;
93+
let returns_bool = || false;
94+
95+
let x = if b {
96+
true
97+
} else { !returns_bool() };
98+
}

tests/ui/needless_bool.rs renamed to tests/ui/needless_bool/fixable.rs

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
// run-rustfix
2+
13
#![warn(clippy::needless_bool)]
2-
#![allow(unused, dead_code, clippy::no_effect)]
4+
#![allow(
5+
unused,
6+
dead_code,
7+
clippy::no_effect,
8+
clippy::if_same_then_else,
9+
clippy::needless_return
10+
)]
311

412
use std::cell::Cell;
513

@@ -25,20 +33,9 @@ macro_rules! bool_comparison_trigger {
2533
)
2634
}
2735

28-
#[allow(clippy::if_same_then_else)]
2936
fn main() {
3037
let x = true;
3138
let y = false;
32-
if x {
33-
true
34-
} else {
35-
true
36-
};
37-
if x {
38-
false
39-
} else {
40-
false
41-
};
4239
if x {
4340
true
4441
} else {
@@ -59,45 +56,31 @@ fn main() {
5956
} else {
6057
false
6158
}; // would also be questionable, but we don't catch this yet
62-
bool_ret(x);
63-
bool_ret2(x);
6459
bool_ret3(x);
65-
bool_ret5(x, x);
6660
bool_ret4(x);
61+
bool_ret5(x, x);
6762
bool_ret6(x, x);
6863
needless_bool(x);
6964
needless_bool2(x);
7065
needless_bool3(x);
7166
}
7267

73-
#[allow(clippy::if_same_then_else, clippy::needless_return)]
74-
fn bool_ret(x: bool) -> bool {
68+
fn bool_ret3(x: bool) -> bool {
7569
if x {
7670
return true;
7771
} else {
78-
return true;
72+
return false;
7973
};
8074
}
8175

82-
#[allow(clippy::if_same_then_else, clippy::needless_return)]
83-
fn bool_ret2(x: bool) -> bool {
76+
fn bool_ret4(x: bool) -> bool {
8477
if x {
8578
return false;
8679
} else {
87-
return false;
88-
};
89-
}
90-
91-
#[allow(clippy::needless_return)]
92-
fn bool_ret3(x: bool) -> bool {
93-
if x {
9480
return true;
95-
} else {
96-
return false;
9781
};
9882
}
9983

100-
#[allow(clippy::needless_return)]
10184
fn bool_ret5(x: bool, y: bool) -> bool {
10285
if x && y {
10386
return true;
@@ -106,16 +89,6 @@ fn bool_ret5(x: bool, y: bool) -> bool {
10689
};
10790
}
10891

109-
#[allow(clippy::needless_return)]
110-
fn bool_ret4(x: bool) -> bool {
111-
if x {
112-
return false;
113-
} else {
114-
return true;
115-
};
116-
}
117-
118-
#[allow(clippy::needless_return)]
11992
fn bool_ret6(x: bool, y: bool) -> bool {
12093
if x && y {
12194
return false;
Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,17 @@
1-
error: this if-then-else expression will always return true
2-
--> $DIR/needless_bool.rs:32:5
3-
|
4-
LL | / if x {
5-
LL | | true
6-
LL | | } else {
7-
LL | | true
8-
LL | | };
9-
| |_____^
10-
|
11-
= note: `-D clippy::needless-bool` implied by `-D warnings`
12-
13-
error: this if-then-else expression will always return false
14-
--> $DIR/needless_bool.rs:37:5
15-
|
16-
LL | / if x {
17-
LL | | false
18-
LL | | } else {
19-
LL | | false
20-
LL | | };
21-
| |_____^
22-
231
error: this if-then-else expression returns a bool literal
24-
--> $DIR/needless_bool.rs:42:5
2+
--> $DIR/fixable.rs:39:5
253
|
264
LL | / if x {
275
LL | | true
286
LL | | } else {
297
LL | | false
308
LL | | };
319
| |_____^ help: you can reduce it to: `x`
10+
|
11+
= note: `-D clippy::needless-bool` implied by `-D warnings`
3212

3313
error: this if-then-else expression returns a bool literal
34-
--> $DIR/needless_bool.rs:47:5
14+
--> $DIR/fixable.rs:44:5
3515
|
3616
LL | / if x {
3717
LL | | false
@@ -41,7 +21,7 @@ LL | | };
4121
| |_____^ help: you can reduce it to: `!x`
4222

4323
error: this if-then-else expression returns a bool literal
44-
--> $DIR/needless_bool.rs:52:5
24+
--> $DIR/fixable.rs:49:5
4525
|
4626
LL | / if x && y {
4727
LL | | false
@@ -50,38 +30,28 @@ LL | | true
5030
LL | | };
5131
| |_____^ help: you can reduce it to: `!(x && y)`
5232

53-
error: this if-then-else expression will always return true
54-
--> $DIR/needless_bool.rs:75:5
33+
error: this if-then-else expression returns a bool literal
34+
--> $DIR/fixable.rs:69:5
5535
|
5636
LL | / if x {
5737
LL | | return true;
5838
LL | | } else {
59-
LL | | return true;
60-
LL | | };
61-
| |_____^
62-
63-
error: this if-then-else expression will always return false
64-
--> $DIR/needless_bool.rs:84:5
65-
|
66-
LL | / if x {
67-
LL | | return false;
68-
LL | | } else {
6939
LL | | return false;
7040
LL | | };
71-
| |_____^
41+
| |_____^ help: you can reduce it to: `return x`
7242

7343
error: this if-then-else expression returns a bool literal
74-
--> $DIR/needless_bool.rs:93:5
44+
--> $DIR/fixable.rs:77:5
7545
|
7646
LL | / if x {
77-
LL | | return true;
78-
LL | | } else {
7947
LL | | return false;
48+
LL | | } else {
49+
LL | | return true;
8050
LL | | };
81-
| |_____^ help: you can reduce it to: `return x`
51+
| |_____^ help: you can reduce it to: `return !x`
8252

8353
error: this if-then-else expression returns a bool literal
84-
--> $DIR/needless_bool.rs:102:5
54+
--> $DIR/fixable.rs:85:5
8555
|
8656
LL | / if x && y {
8757
LL | | return true;
@@ -91,17 +61,7 @@ LL | | };
9161
| |_____^ help: you can reduce it to: `return x && y`
9262

9363
error: this if-then-else expression returns a bool literal
94-
--> $DIR/needless_bool.rs:111:5
95-
|
96-
LL | / if x {
97-
LL | | return false;
98-
LL | | } else {
99-
LL | | return true;
100-
LL | | };
101-
| |_____^ help: you can reduce it to: `return !x`
102-
103-
error: this if-then-else expression returns a bool literal
104-
--> $DIR/needless_bool.rs:120:5
64+
--> $DIR/fixable.rs:93:5
10565
|
10666
LL | / if x && y {
10767
LL | | return false;
@@ -111,33 +71,33 @@ LL | | };
11171
| |_____^ help: you can reduce it to: `return !(x && y)`
11272

11373
error: equality checks against true are unnecessary
114-
--> $DIR/needless_bool.rs:128:8
74+
--> $DIR/fixable.rs:101:8
11575
|
11676
LL | if x == true {};
11777
| ^^^^^^^^^ help: try simplifying it as shown: `x`
11878
|
11979
= note: `-D clippy::bool-comparison` implied by `-D warnings`
12080

12181
error: equality checks against false can be replaced by a negation
122-
--> $DIR/needless_bool.rs:132:8
82+
--> $DIR/fixable.rs:105:8
12383
|
12484
LL | if x == false {};
12585
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
12686

12787
error: equality checks against true are unnecessary
128-
--> $DIR/needless_bool.rs:142:8
88+
--> $DIR/fixable.rs:115:8
12989
|
13090
LL | if x == true {};
13191
| ^^^^^^^^^ help: try simplifying it as shown: `x`
13292

13393
error: equality checks against false can be replaced by a negation
134-
--> $DIR/needless_bool.rs:143:8
94+
--> $DIR/fixable.rs:116:8
13595
|
13696
LL | if x == false {};
13797
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
13898

13999
error: this if-then-else expression returns a bool literal
140-
--> $DIR/needless_bool.rs:152:12
100+
--> $DIR/fixable.rs:125:12
141101
|
142102
LL | } else if returns_bool() {
143103
| ____________^
@@ -147,5 +107,5 @@ LL | | true
147107
LL | | };
148108
| |_____^ help: you can reduce it to: `{ !returns_bool() }`
149109

150-
error: aborting due to 16 previous errors
110+
error: aborting due to 12 previous errors
151111

0 commit comments

Comments
 (0)