]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
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, | |
c295e0f8 | 9 | clippy::equatable_if_let, |
136023e0 XL |
10 | clippy::needless_return, |
11 | clippy::self_named_constructors | |
f20569fa XL |
12 | )] |
13 | ||
14 | use std::cell::Cell; | |
15 | ||
16 | macro_rules! bool_comparison_trigger { | |
17 | ($($i:ident: $def:expr, $stb:expr );+ $(;)*) => ( | |
18 | ||
19 | #[derive(Clone)] | |
20 | pub struct Trigger { | |
21 | $($i: (Cell<bool>, bool, bool)),+ | |
22 | } | |
23 | ||
24 | #[allow(dead_code)] | |
25 | impl Trigger { | |
26 | pub fn trigger(&self, key: &str) -> bool { | |
27 | $( | |
28 | if let stringify!($i) = key { | |
29 | return self.$i.1 && self.$i.2 == $def; | |
30 | } | |
31 | )+ | |
32 | false | |
33 | } | |
34 | } | |
35 | ) | |
36 | } | |
37 | ||
38 | fn main() { | |
39 | let x = true; | |
40 | let y = false; | |
41 | if x { | |
42 | true | |
43 | } else { | |
44 | false | |
45 | }; | |
46 | if x { | |
47 | false | |
48 | } else { | |
49 | true | |
50 | }; | |
51 | if x && y { | |
52 | false | |
53 | } else { | |
54 | true | |
55 | }; | |
56 | if x { | |
57 | x | |
58 | } else { | |
59 | false | |
60 | }; // would also be questionable, but we don't catch this yet | |
61 | bool_ret3(x); | |
62 | bool_ret4(x); | |
63 | bool_ret5(x, x); | |
64 | bool_ret6(x, x); | |
65 | needless_bool(x); | |
66 | needless_bool2(x); | |
67 | needless_bool3(x); | |
68 | } | |
69 | ||
70 | fn bool_ret3(x: bool) -> bool { | |
71 | if x { | |
72 | return true; | |
73 | } else { | |
74 | return false; | |
75 | }; | |
76 | } | |
77 | ||
78 | fn bool_ret4(x: bool) -> bool { | |
79 | if x { | |
80 | return false; | |
81 | } else { | |
82 | return true; | |
83 | }; | |
84 | } | |
85 | ||
86 | fn bool_ret5(x: bool, y: bool) -> bool { | |
87 | if x && y { | |
88 | return true; | |
89 | } else { | |
90 | return false; | |
91 | }; | |
92 | } | |
93 | ||
94 | fn bool_ret6(x: bool, y: bool) -> bool { | |
95 | if x && y { | |
96 | return false; | |
97 | } else { | |
98 | return true; | |
99 | }; | |
100 | } | |
101 | ||
102 | fn needless_bool(x: bool) { | |
103 | if x == true {}; | |
104 | } | |
105 | ||
106 | fn needless_bool2(x: bool) { | |
107 | if x == false {}; | |
108 | } | |
109 | ||
110 | fn needless_bool3(x: bool) { | |
111 | bool_comparison_trigger! { | |
112 | test_one: false, false; | |
113 | test_three: false, false; | |
114 | test_two: true, true; | |
115 | } | |
116 | ||
117 | if x == true {}; | |
118 | if x == false {}; | |
119 | } | |
120 | ||
121 | fn needless_bool_in_the_suggestion_wraps_the_predicate_of_if_else_statement_in_brackets() { | |
122 | let b = false; | |
123 | let returns_bool = || false; | |
124 | ||
125 | let x = if b { | |
126 | true | |
127 | } else if returns_bool() { | |
128 | false | |
129 | } else { | |
130 | true | |
131 | }; | |
132 | } |