]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/bool_comparison.rs
Rollup merge of #84221 - ABouttefeux:generic-arg-elision, r=estebank
[rust.git] / src / tools / clippy / tests / ui / bool_comparison.rs
1 // run-rustfix
2
3 #![warn(clippy::bool_comparison)]
4
5 fn main() {
6     let x = true;
7     if x == true {
8         "yes"
9     } else {
10         "no"
11     };
12     if x == false {
13         "yes"
14     } else {
15         "no"
16     };
17     if true == x {
18         "yes"
19     } else {
20         "no"
21     };
22     if false == x {
23         "yes"
24     } else {
25         "no"
26     };
27     if x != true {
28         "yes"
29     } else {
30         "no"
31     };
32     if x != false {
33         "yes"
34     } else {
35         "no"
36     };
37     if true != x {
38         "yes"
39     } else {
40         "no"
41     };
42     if false != x {
43         "yes"
44     } else {
45         "no"
46     };
47     if x < true {
48         "yes"
49     } else {
50         "no"
51     };
52     if false < x {
53         "yes"
54     } else {
55         "no"
56     };
57     if x > false {
58         "yes"
59     } else {
60         "no"
61     };
62     if true > x {
63         "yes"
64     } else {
65         "no"
66     };
67     let y = true;
68     if x < y {
69         "yes"
70     } else {
71         "no"
72     };
73     if x > y {
74         "yes"
75     } else {
76         "no"
77     };
78 }
79
80 #[allow(dead_code)]
81 fn issue3703() {
82     struct Foo;
83     impl PartialEq<bool> for Foo {
84         fn eq(&self, _: &bool) -> bool {
85             true
86         }
87     }
88     impl PartialEq<Foo> for bool {
89         fn eq(&self, _: &Foo) -> bool {
90             true
91         }
92     }
93     impl PartialOrd<bool> for Foo {
94         fn partial_cmp(&self, _: &bool) -> Option<std::cmp::Ordering> {
95             None
96         }
97     }
98     impl PartialOrd<Foo> for bool {
99         fn partial_cmp(&self, _: &Foo) -> Option<std::cmp::Ordering> {
100             None
101         }
102     }
103
104     if Foo == true {}
105     if true == Foo {}
106     if Foo != true {}
107     if true != Foo {}
108     if Foo == false {}
109     if false == Foo {}
110     if Foo != false {}
111     if false != Foo {}
112     if Foo < false {}
113     if false < Foo {}
114 }
115
116 #[allow(dead_code)]
117 fn issue4983() {
118     let a = true;
119     let b = false;
120
121     if a == !b {};
122     if !a == b {};
123     if a == b {};
124     if !a == !b {};
125
126     if b == !a {};
127     if !b == a {};
128     if b == a {};
129     if !b == !a {};
130 }
131
132 macro_rules! m {
133     ($func:ident) => {
134         $func()
135     };
136 }
137
138 fn func() -> bool {
139     true
140 }
141
142 #[allow(dead_code)]
143 fn issue3973() {
144     // ok, don't lint on `cfg` invocation
145     if false == cfg!(feature = "debugging") {}
146     if cfg!(feature = "debugging") == false {}
147     if true == cfg!(feature = "debugging") {}
148     if cfg!(feature = "debugging") == true {}
149
150     // lint, could be simplified
151     if false == m!(func) {}
152     if m!(func) == false {}
153     if true == m!(func) {}
154     if m!(func) == true {}
155
156     // no lint with a variable
157     let is_debug = false;
158     if is_debug == cfg!(feature = "debugging") {}
159     if cfg!(feature = "debugging") == is_debug {}
160     if is_debug == m!(func) {}
161     if m!(func) == is_debug {}
162     let is_debug = true;
163     if is_debug == cfg!(feature = "debugging") {}
164     if cfg!(feature = "debugging") == is_debug {}
165     if is_debug == m!(func) {}
166     if m!(func) == is_debug {}
167 }