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