1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
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.
13 #![warn(clippy::nonminimal_bool, clippy::logic_bug)]
15 #[allow(unused, clippy::many_single_char_names)]
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!();
30 let _ = cfg!(you_shall_not_not_pass) && a;
31 let _ = a || !b || !c || !d || !e;
32 let _ = !(a && b || c);
36 #[allow(unused, clippy::many_single_char_names)]
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);
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!();
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();
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 }
79 impl<E> CustomResultOk<E> {
80 pub fn is_ok(&self) -> bool { true }
83 impl<E> CustomResultErr<E> {
84 pub fn is_err(&self) -> bool { true }
87 impl<T> CustomSomeSome<T> {
88 pub fn is_some(&self) -> bool { true }
91 impl<T> CustomSomeNone<T> {
92 pub fn is_none(&self) -> bool { true }
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().
101 let res = CustomResultErr::Err("Error");
102 // Should not warn and suggest 'is_ok()' because the type does not
103 // implement is_ok().
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() { }
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() { }
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);
124 if !res.is_some() { }
125 if !res.is_none() { }
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!();