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