]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_bool.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / needless_bool.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![warn(clippy::needless_bool)]
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 #[allow(clippy::if_same_then_else)]
37 fn main() {
38     let x = true;
39     let y = false;
40     if x {
41         true
42     } else {
43         true
44     };
45     if x {
46         false
47     } else {
48         false
49     };
50     if x {
51         true
52     } else {
53         false
54     };
55     if x {
56         false
57     } else {
58         true
59     };
60     if x && y {
61         false
62     } else {
63         true
64     };
65     if x {
66         x
67     } else {
68         false
69     }; // would also be questionable, but we don't catch this yet
70     bool_ret(x);
71     bool_ret2(x);
72     bool_ret3(x);
73     bool_ret5(x, x);
74     bool_ret4(x);
75     bool_ret6(x, x);
76     needless_bool(x);
77     needless_bool2(x);
78     needless_bool3(x);
79 }
80
81 #[allow(clippy::if_same_then_else, clippy::needless_return)]
82 fn bool_ret(x: bool) -> bool {
83     if x {
84         return true;
85     } else {
86         return true;
87     };
88 }
89
90 #[allow(clippy::if_same_then_else, clippy::needless_return)]
91 fn bool_ret2(x: bool) -> bool {
92     if x {
93         return false;
94     } else {
95         return false;
96     };
97 }
98
99 #[allow(clippy::needless_return)]
100 fn bool_ret3(x: bool) -> bool {
101     if x {
102         return true;
103     } else {
104         return false;
105     };
106 }
107
108 #[allow(clippy::needless_return)]
109 fn bool_ret5(x: bool, y: bool) -> bool {
110     if x && y {
111         return true;
112     } else {
113         return false;
114     };
115 }
116
117 #[allow(clippy::needless_return)]
118 fn bool_ret4(x: bool) -> bool {
119     if x {
120         return false;
121     } else {
122         return true;
123     };
124 }
125
126 #[allow(clippy::needless_return)]
127 fn bool_ret6(x: bool, y: bool) -> bool {
128     if x && y {
129         return false;
130     } else {
131         return true;
132     };
133 }
134
135 fn needless_bool(x: bool) {
136     if x == true {};
137 }
138
139 fn needless_bool2(x: bool) {
140     if x == false {};
141 }
142
143 fn needless_bool3(x: bool) {
144     bool_comparison_trigger! {
145         test_one:   false, false;
146         test_three: false, false;
147         test_two:   true, true;
148     }
149
150     if x == true {};
151     if x == false {};
152 }