]> git.lizzy.rs Git - rust.git/blob - tests/ui/booleans.rs
Auto merge of #4478 - tsurai:master, r=flip1995
[rust.git] / tests / ui / booleans.rs
1 #![warn(clippy::nonminimal_bool, clippy::logic_bug)]
2
3 #[allow(unused, clippy::many_single_char_names)]
4 fn main() {
5     let a: bool = unimplemented!();
6     let b: bool = unimplemented!();
7     let c: bool = unimplemented!();
8     let d: bool = unimplemented!();
9     let e: bool = unimplemented!();
10     let _ = a && b || a;
11     let _ = !(a && b);
12     let _ = !true;
13     let _ = !false;
14     let _ = !!a;
15     let _ = false && a;
16     let _ = false || a;
17     // don't lint on cfgs
18     let _ = cfg!(you_shall_not_not_pass) && a;
19     let _ = a || !b || !c || !d || !e;
20     let _ = !(a && b || c);
21     let _ = !(!a && b);
22 }
23
24 #[allow(unused, clippy::many_single_char_names)]
25 fn equality_stuff() {
26     let a: i32 = unimplemented!();
27     let b: i32 = unimplemented!();
28     let c: i32 = unimplemented!();
29     let d: i32 = unimplemented!();
30     let e: i32 = unimplemented!();
31     let _ = a == b && a != b;
32     let _ = a == b && c == 5 && a == b;
33     let _ = a == b && c == 5 && b == a;
34     let _ = a < b && a >= b;
35     let _ = a > b && a <= b;
36     let _ = a > b && a == b;
37     let _ = a != b || !(a != b || c == d);
38 }
39
40 #[allow(unused, clippy::many_single_char_names)]
41 fn methods_with_negation() {
42     let a: Option<i32> = unimplemented!();
43     let b: Result<i32, i32> = unimplemented!();
44     let _ = a.is_some();
45     let _ = !a.is_some();
46     let _ = a.is_none();
47     let _ = !a.is_none();
48     let _ = b.is_err();
49     let _ = !b.is_err();
50     let _ = b.is_ok();
51     let _ = !b.is_ok();
52     let c = false;
53     let _ = !(a.is_some() && !c);
54     let _ = !(!c ^ c) || !a.is_some();
55     let _ = (!c ^ c) || !a.is_some();
56     let _ = !c ^ c || !a.is_some();
57 }
58
59 // Simplified versions of https://github.com/rust-lang/rust-clippy/issues/2638
60 // clippy::nonminimal_bool should only check the built-in Result and Some type, not
61 // any other types like the following.
62 enum CustomResultOk<E> {
63     Ok,
64     Err(E),
65 }
66 enum CustomResultErr<E> {
67     Ok,
68     Err(E),
69 }
70 enum CustomSomeSome<T> {
71     Some(T),
72     None,
73 }
74 enum CustomSomeNone<T> {
75     Some(T),
76     None,
77 }
78
79 impl<E> CustomResultOk<E> {
80     pub fn is_ok(&self) -> bool {
81         true
82     }
83 }
84
85 impl<E> CustomResultErr<E> {
86     pub fn is_err(&self) -> bool {
87         true
88     }
89 }
90
91 impl<T> CustomSomeSome<T> {
92     pub fn is_some(&self) -> bool {
93         true
94     }
95 }
96
97 impl<T> CustomSomeNone<T> {
98     pub fn is_none(&self) -> bool {
99         true
100     }
101 }
102
103 fn dont_warn_for_custom_methods_with_negation() {
104     let res = CustomResultOk::Err("Error");
105     // Should not warn and suggest 'is_err()' because the type does not
106     // implement is_err().
107     if !res.is_ok() {}
108
109     let res = CustomResultErr::Err("Error");
110     // Should not warn and suggest 'is_ok()' because the type does not
111     // implement is_ok().
112     if !res.is_err() {}
113
114     let res = CustomSomeSome::Some("thing");
115     // Should not warn and suggest 'is_none()' because the type does not
116     // implement is_none().
117     if !res.is_some() {}
118
119     let res = CustomSomeNone::Some("thing");
120     // Should not warn and suggest 'is_some()' because the type does not
121     // implement is_some().
122     if !res.is_none() {}
123 }
124
125 // Only Built-in Result and Some types should suggest the negated alternative
126 fn warn_for_built_in_methods_with_negation() {
127     let res: Result<usize, usize> = Ok(1);
128     if !res.is_ok() {}
129     if !res.is_err() {}
130
131     let res = Some(1);
132     if !res.is_some() {}
133     if !res.is_none() {}
134 }
135
136 #[allow(clippy::neg_cmp_op_on_partial_ord)]
137 fn dont_warn_for_negated_partial_ord_comparison() {
138     let a: f64 = unimplemented!();
139     let b: f64 = unimplemented!();
140     let _ = !(a < b);
141     let _ = !(a <= b);
142     let _ = !(a > b);
143     let _ = !(a >= b);
144 }