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