-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathbool_assert_comparison.fixed
202 lines (175 loc) · 5.26 KB
/
bool_assert_comparison.fixed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#![allow(unused, clippy::assertions_on_constants, clippy::const_is_empty)]
#![warn(clippy::bool_assert_comparison)]
use std::ops::Not;
macro_rules! a {
() => {
true
};
}
macro_rules! b {
() => {
true
};
}
// Implements the Not trait but with an output type
// that's not bool. Should not suggest a rewrite
#[derive(Debug, Clone, Copy)]
enum ImplNotTraitWithoutBool {
VariantX(bool),
VariantY(u32),
}
impl PartialEq<bool> for ImplNotTraitWithoutBool {
fn eq(&self, other: &bool) -> bool {
match *self {
ImplNotTraitWithoutBool::VariantX(b) => b == *other,
_ => false,
}
}
}
impl Not for ImplNotTraitWithoutBool {
type Output = Self;
fn not(self) -> Self::Output {
match self {
ImplNotTraitWithoutBool::VariantX(b) => ImplNotTraitWithoutBool::VariantX(!b),
ImplNotTraitWithoutBool::VariantY(0) => ImplNotTraitWithoutBool::VariantY(1),
ImplNotTraitWithoutBool::VariantY(_) => ImplNotTraitWithoutBool::VariantY(0),
}
}
}
// This type implements the Not trait with an Output of
// type bool. Using assert!(..) must be suggested
#[derive(Debug, Clone, Copy)]
struct ImplNotTraitWithBool;
impl PartialEq<bool> for ImplNotTraitWithBool {
fn eq(&self, other: &bool) -> bool {
false
}
}
impl Not for ImplNotTraitWithBool {
type Output = bool;
fn not(self) -> Self::Output {
true
}
}
#[derive(Debug)]
struct NonCopy;
impl PartialEq<bool> for NonCopy {
fn eq(&self, other: &bool) -> bool {
false
}
}
impl Not for NonCopy {
type Output = bool;
fn not(self) -> Self::Output {
true
}
}
fn main() {
let a = ImplNotTraitWithoutBool::VariantX(true);
let b = ImplNotTraitWithBool;
assert_eq!("a".len(), 1);
assert!(!"a".is_empty());
//~^ bool_assert_comparison
assert!("".is_empty());
//~^ bool_assert_comparison
assert!("".is_empty());
//~^ bool_assert_comparison
assert_eq!(a!(), b!());
assert_eq!(a!(), "".is_empty());
assert_eq!("".is_empty(), b!());
assert_eq!(a, true);
assert!(b);
//~^ bool_assert_comparison
assert_ne!("a".len(), 1);
assert!("a".is_empty());
//~^ bool_assert_comparison
assert!(!"".is_empty());
//~^ bool_assert_comparison
assert!(!"".is_empty());
//~^ bool_assert_comparison
assert_ne!(a!(), b!());
assert_ne!(a!(), "".is_empty());
assert_ne!("".is_empty(), b!());
assert_ne!(a, true);
assert!(!b);
//~^ bool_assert_comparison
debug_assert_eq!("a".len(), 1);
debug_assert!(!"a".is_empty());
//~^ bool_assert_comparison
debug_assert!("".is_empty());
//~^ bool_assert_comparison
debug_assert!("".is_empty());
//~^ bool_assert_comparison
debug_assert_eq!(a!(), b!());
debug_assert_eq!(a!(), "".is_empty());
debug_assert_eq!("".is_empty(), b!());
debug_assert_eq!(a, true);
debug_assert!(b);
//~^ bool_assert_comparison
debug_assert_ne!("a".len(), 1);
debug_assert!("a".is_empty());
//~^ bool_assert_comparison
debug_assert!(!"".is_empty());
//~^ bool_assert_comparison
debug_assert!(!"".is_empty());
//~^ bool_assert_comparison
debug_assert_ne!(a!(), b!());
debug_assert_ne!(a!(), "".is_empty());
debug_assert_ne!("".is_empty(), b!());
debug_assert_ne!(a, true);
debug_assert!(!b);
//~^ bool_assert_comparison
// assert with error messages
assert_eq!("a".len(), 1, "tadam {}", 1);
assert_eq!("a".len(), 1, "tadam {}", true);
assert!(!"a".is_empty(), "tadam {}", 1);
//~^ bool_assert_comparison
assert!(!"a".is_empty(), "tadam {}", true);
//~^ bool_assert_comparison
assert!(!"a".is_empty(), "tadam {}", true);
//~^ bool_assert_comparison
assert_eq!(a, true, "tadam {}", false);
debug_assert_eq!("a".len(), 1, "tadam {}", 1);
debug_assert_eq!("a".len(), 1, "tadam {}", true);
debug_assert!(!"a".is_empty(), "tadam {}", 1);
//~^ bool_assert_comparison
debug_assert!(!"a".is_empty(), "tadam {}", true);
//~^ bool_assert_comparison
debug_assert!(!"a".is_empty(), "tadam {}", true);
//~^ bool_assert_comparison
debug_assert_eq!(a, true, "tadam {}", false);
assert!(a!());
//~^ bool_assert_comparison
assert!(b!());
//~^ bool_assert_comparison
use debug_assert_eq as renamed;
renamed!(a, true);
debug_assert!(b);
//~^ bool_assert_comparison
let non_copy = NonCopy;
assert_eq!(non_copy, true);
// changing the above to `assert!(non_copy)` would cause a `borrow of moved value`
println!("{non_copy:?}");
macro_rules! in_macro {
($v:expr) => {{
assert_eq!($v, true);
}};
}
in_macro!(a);
assert!("".is_empty());
//~^ bool_assert_comparison
assert!("".is_empty());
//~^ bool_assert_comparison
assert!(!"requires negation".is_empty());
//~^ bool_assert_comparison
assert!(!"requires negation".is_empty());
//~^ bool_assert_comparison
debug_assert!("".is_empty());
//~^ bool_assert_comparison
debug_assert!("".is_empty());
//~^ bool_assert_comparison
debug_assert!(!"requires negation".is_empty());
//~^ bool_assert_comparison
debug_assert!(!"requires negation".is_empty());
//~^ bool_assert_comparison
}