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