fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
2.6 KiB110 linesraw
1#![allow(unused, clippy::diverging_sub_expression, clippy::needless_if)]
2#![warn(clippy::nonminimal_bool)]
3
4fn 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_none();
9    let _ = a.is_none();
10    let _ = a.is_some();
11    let _ = b.is_err();
12    let _ = b.is_ok();
13    let _ = b.is_ok();
14    let _ = b.is_err();
15    let c = false;
16    let _ = a.is_none() || c;
17    let _ = a.is_none() && c;
18    let _ = !(!c ^ c) || a.is_none();
19    let _ = (!c ^ c) || a.is_none();
20    let _ = !c ^ c || a.is_none();
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.
26enum CustomResultOk<E> {
27    Ok,
28    Err(E),
29}
30enum CustomResultErr<E> {
31    Ok,
32    Err(E),
33}
34enum CustomSomeSome<T> {
35    Some(T),
36    None,
37}
38enum CustomSomeNone<T> {
39    Some(T),
40    None,
41}
42
43impl<E> CustomResultOk<E> {
44    pub fn is_ok(&self) -> bool {
45        true
46    }
47}
48
49impl<E> CustomResultErr<E> {
50    pub fn is_err(&self) -> bool {
51        true
52    }
53}
54
55impl<T> CustomSomeSome<T> {
56    pub fn is_some(&self) -> bool {
57        true
58    }
59}
60
61impl<T> CustomSomeNone<T> {
62    pub fn is_none(&self) -> bool {
63        true
64    }
65}
66
67fn 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
90fn warn_for_built_in_methods_with_negation() {
91    let res: Result<usize, usize> = Ok(1);
92    if res.is_err() {}
93    if res.is_ok() {}
94
95    let res = Some(1);
96    if res.is_none() {}
97    if res.is_some() {}
98}
99
100#[allow(clippy::neg_cmp_op_on_partial_ord)]
101fn 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
110fn main() {}