]> git.lizzy.rs Git - rust.git/blob - tests/ui/bool_assert_comparison.rs
Auto merge of #10094 - EricWu2003:increment-visitor-fix, r=xFrednet
[rust.git] / tests / ui / bool_assert_comparison.rs
1 #![warn(clippy::bool_assert_comparison)]
2
3 use std::ops::Not;
4
5 macro_rules! a {
6     () => {
7         true
8     };
9 }
10 macro_rules! b {
11     () => {
12         true
13     };
14 }
15
16 // Implements the Not trait but with an output type
17 // that's not bool. Should not suggest a rewrite
18 #[derive(Debug)]
19 enum ImplNotTraitWithoutBool {
20     VariantX(bool),
21     VariantY(u32),
22 }
23
24 impl PartialEq<bool> for ImplNotTraitWithoutBool {
25     fn eq(&self, other: &bool) -> bool {
26         match *self {
27             ImplNotTraitWithoutBool::VariantX(b) => b == *other,
28             _ => false,
29         }
30     }
31 }
32
33 impl Not for ImplNotTraitWithoutBool {
34     type Output = Self;
35
36     fn not(self) -> Self::Output {
37         match self {
38             ImplNotTraitWithoutBool::VariantX(b) => ImplNotTraitWithoutBool::VariantX(!b),
39             ImplNotTraitWithoutBool::VariantY(0) => ImplNotTraitWithoutBool::VariantY(1),
40             ImplNotTraitWithoutBool::VariantY(_) => ImplNotTraitWithoutBool::VariantY(0),
41         }
42     }
43 }
44
45 // This type implements the Not trait with an Output of
46 // type bool. Using assert!(..) must be suggested
47 #[derive(Debug)]
48 struct ImplNotTraitWithBool;
49
50 impl PartialEq<bool> for ImplNotTraitWithBool {
51     fn eq(&self, other: &bool) -> bool {
52         false
53     }
54 }
55
56 impl Not for ImplNotTraitWithBool {
57     type Output = bool;
58
59     fn not(self) -> Self::Output {
60         true
61     }
62 }
63
64 fn main() {
65     let a = ImplNotTraitWithoutBool::VariantX(true);
66     let b = ImplNotTraitWithBool;
67
68     assert_eq!("a".len(), 1);
69     assert_eq!("a".is_empty(), false);
70     assert_eq!("".is_empty(), true);
71     assert_eq!(true, "".is_empty());
72     assert_eq!(a!(), b!());
73     assert_eq!(a!(), "".is_empty());
74     assert_eq!("".is_empty(), b!());
75     assert_eq!(a, true);
76     assert_eq!(b, true);
77
78     assert_ne!("a".len(), 1);
79     assert_ne!("a".is_empty(), false);
80     assert_ne!("".is_empty(), true);
81     assert_ne!(true, "".is_empty());
82     assert_ne!(a!(), b!());
83     assert_ne!(a!(), "".is_empty());
84     assert_ne!("".is_empty(), b!());
85     assert_ne!(a, true);
86     assert_ne!(b, true);
87
88     debug_assert_eq!("a".len(), 1);
89     debug_assert_eq!("a".is_empty(), false);
90     debug_assert_eq!("".is_empty(), true);
91     debug_assert_eq!(true, "".is_empty());
92     debug_assert_eq!(a!(), b!());
93     debug_assert_eq!(a!(), "".is_empty());
94     debug_assert_eq!("".is_empty(), b!());
95     debug_assert_eq!(a, true);
96     debug_assert_eq!(b, true);
97
98     debug_assert_ne!("a".len(), 1);
99     debug_assert_ne!("a".is_empty(), false);
100     debug_assert_ne!("".is_empty(), true);
101     debug_assert_ne!(true, "".is_empty());
102     debug_assert_ne!(a!(), b!());
103     debug_assert_ne!(a!(), "".is_empty());
104     debug_assert_ne!("".is_empty(), b!());
105     debug_assert_ne!(a, true);
106     debug_assert_ne!(b, true);
107
108     // assert with error messages
109     assert_eq!("a".len(), 1, "tadam {}", 1);
110     assert_eq!("a".len(), 1, "tadam {}", true);
111     assert_eq!("a".is_empty(), false, "tadam {}", 1);
112     assert_eq!("a".is_empty(), false, "tadam {}", true);
113     assert_eq!(false, "a".is_empty(), "tadam {}", true);
114     assert_eq!(a, true, "tadam {}", false);
115
116     debug_assert_eq!("a".len(), 1, "tadam {}", 1);
117     debug_assert_eq!("a".len(), 1, "tadam {}", true);
118     debug_assert_eq!("a".is_empty(), false, "tadam {}", 1);
119     debug_assert_eq!("a".is_empty(), false, "tadam {}", true);
120     debug_assert_eq!(false, "a".is_empty(), "tadam {}", true);
121     debug_assert_eq!(a, true, "tadam {}", false);
122 }