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