]>
Commit | Line | Data |
---|---|---|
cdc7bbd5 XL |
1 | // edition:2018 |
2 | ||
f20569fa XL |
3 | #![warn(clippy::len_without_is_empty)] |
4 | #![allow(dead_code, unused)] | |
5 | ||
6 | pub struct PubOne; | |
7 | ||
8 | impl PubOne { | |
9 | pub fn len(&self) -> isize { | |
10 | 1 | |
11 | } | |
12 | } | |
13 | ||
14 | impl PubOne { | |
15 | // A second impl for this struct -- the error span shouldn't mention this. | |
16 | pub fn irrelevant(&self) -> bool { | |
17 | false | |
18 | } | |
19 | } | |
20 | ||
21 | // Identical to `PubOne`, but with an `allow` attribute on the impl complaining `len`. | |
22 | pub struct PubAllowed; | |
23 | ||
24 | #[allow(clippy::len_without_is_empty)] | |
25 | impl PubAllowed { | |
26 | pub fn len(&self) -> isize { | |
27 | 1 | |
28 | } | |
29 | } | |
30 | ||
31 | // No `allow` attribute on this impl block, but that doesn't matter -- we only require one on the | |
32 | // impl containing `len`. | |
33 | impl PubAllowed { | |
34 | pub fn irrelevant(&self) -> bool { | |
35 | false | |
36 | } | |
37 | } | |
38 | ||
39 | pub struct PubAllowedFn; | |
40 | ||
41 | impl PubAllowedFn { | |
42 | #[allow(clippy::len_without_is_empty)] | |
43 | pub fn len(&self) -> isize { | |
44 | 1 | |
45 | } | |
46 | } | |
47 | ||
48 | #[allow(clippy::len_without_is_empty)] | |
49 | pub struct PubAllowedStruct; | |
50 | ||
51 | impl PubAllowedStruct { | |
52 | pub fn len(&self) -> isize { | |
53 | 1 | |
54 | } | |
55 | } | |
56 | ||
57 | pub trait PubTraitsToo { | |
58 | fn len(&self) -> isize; | |
59 | } | |
60 | ||
61 | impl PubTraitsToo for One { | |
62 | fn len(&self) -> isize { | |
63 | 0 | |
64 | } | |
65 | } | |
66 | ||
67 | pub struct HasIsEmpty; | |
68 | ||
69 | impl HasIsEmpty { | |
70 | pub fn len(&self) -> isize { | |
71 | 1 | |
72 | } | |
73 | ||
74 | fn is_empty(&self) -> bool { | |
75 | false | |
76 | } | |
77 | } | |
78 | ||
79 | pub struct HasWrongIsEmpty; | |
80 | ||
81 | impl HasWrongIsEmpty { | |
82 | pub fn len(&self) -> isize { | |
83 | 1 | |
84 | } | |
85 | ||
86 | pub fn is_empty(&self, x: u32) -> bool { | |
87 | false | |
88 | } | |
89 | } | |
90 | ||
91 | pub struct MismatchedSelf; | |
92 | ||
93 | impl MismatchedSelf { | |
94 | pub fn len(self) -> isize { | |
95 | 1 | |
96 | } | |
97 | ||
98 | pub fn is_empty(&self) -> bool { | |
99 | false | |
100 | } | |
101 | } | |
102 | ||
103 | struct NotPubOne; | |
104 | ||
105 | impl NotPubOne { | |
106 | pub fn len(&self) -> isize { | |
107 | // No error; `len` is pub but `NotPubOne` is not exported anyway. | |
108 | 1 | |
109 | } | |
110 | } | |
111 | ||
112 | struct One; | |
113 | ||
114 | impl One { | |
115 | fn len(&self) -> isize { | |
116 | // No error; `len` is private; see issue #1085. | |
117 | 1 | |
118 | } | |
119 | } | |
120 | ||
121 | trait TraitsToo { | |
122 | fn len(&self) -> isize; | |
123 | // No error; `len` is private; see issue #1085. | |
124 | } | |
125 | ||
126 | impl TraitsToo for One { | |
127 | fn len(&self) -> isize { | |
128 | 0 | |
129 | } | |
130 | } | |
131 | ||
132 | struct HasPrivateIsEmpty; | |
133 | ||
134 | impl HasPrivateIsEmpty { | |
135 | pub fn len(&self) -> isize { | |
136 | 1 | |
137 | } | |
138 | ||
139 | fn is_empty(&self) -> bool { | |
140 | false | |
141 | } | |
142 | } | |
143 | ||
144 | struct Wither; | |
145 | ||
146 | pub trait WithIsEmpty { | |
147 | fn len(&self) -> isize; | |
148 | fn is_empty(&self) -> bool; | |
149 | } | |
150 | ||
151 | impl WithIsEmpty for Wither { | |
152 | fn len(&self) -> isize { | |
153 | 1 | |
154 | } | |
155 | ||
156 | fn is_empty(&self) -> bool { | |
157 | false | |
158 | } | |
159 | } | |
160 | ||
161 | pub trait Empty { | |
162 | fn is_empty(&self) -> bool; | |
163 | } | |
164 | ||
165 | pub trait InheritingEmpty: Empty { | |
166 | // Must not trigger `LEN_WITHOUT_IS_EMPTY`. | |
167 | fn len(&self) -> isize; | |
168 | } | |
169 | ||
170 | // This used to ICE. | |
171 | pub trait Foo: Sized {} | |
172 | ||
173 | pub trait DependsOnFoo: Foo { | |
174 | fn len(&mut self) -> usize; | |
175 | } | |
176 | ||
cdc7bbd5 | 177 | // issue #1562 |
f20569fa XL |
178 | pub struct MultipleImpls; |
179 | ||
f20569fa XL |
180 | impl MultipleImpls { |
181 | pub fn len(&self) -> usize { | |
182 | 1 | |
183 | } | |
184 | } | |
185 | ||
186 | impl MultipleImpls { | |
187 | pub fn is_empty(&self) -> bool { | |
188 | false | |
189 | } | |
190 | } | |
191 | ||
cdc7bbd5 XL |
192 | // issue #6958 |
193 | pub struct OptionalLen; | |
194 | ||
195 | impl OptionalLen { | |
196 | pub fn len(&self) -> Option<usize> { | |
197 | Some(0) | |
198 | } | |
199 | ||
200 | pub fn is_empty(&self) -> Option<bool> { | |
201 | Some(true) | |
202 | } | |
203 | } | |
204 | ||
205 | pub struct OptionalLen2; | |
206 | impl OptionalLen2 { | |
207 | pub fn len(&self) -> Option<usize> { | |
208 | Some(0) | |
209 | } | |
210 | ||
211 | pub fn is_empty(&self) -> bool { | |
212 | true | |
213 | } | |
214 | } | |
215 | ||
216 | pub struct OptionalLen3; | |
217 | impl OptionalLen3 { | |
218 | pub fn len(&self) -> usize { | |
219 | 0 | |
220 | } | |
221 | ||
222 | // should lint, len is not an option | |
223 | pub fn is_empty(&self) -> Option<bool> { | |
224 | None | |
225 | } | |
226 | } | |
227 | ||
228 | pub struct ResultLen; | |
229 | impl ResultLen { | |
230 | pub fn len(&self) -> Result<usize, ()> { | |
231 | Ok(0) | |
232 | } | |
233 | ||
234 | // Differing result types | |
235 | pub fn is_empty(&self) -> Option<bool> { | |
236 | Some(true) | |
237 | } | |
238 | } | |
239 | ||
240 | pub struct ResultLen2; | |
241 | impl ResultLen2 { | |
242 | pub fn len(&self) -> Result<usize, ()> { | |
243 | Ok(0) | |
244 | } | |
245 | ||
246 | pub fn is_empty(&self) -> Result<bool, ()> { | |
247 | Ok(true) | |
248 | } | |
249 | } | |
250 | ||
251 | pub struct ResultLen3; | |
252 | impl ResultLen3 { | |
253 | pub fn len(&self) -> Result<usize, ()> { | |
254 | Ok(0) | |
255 | } | |
256 | ||
257 | // Non-fallible result is ok. | |
258 | pub fn is_empty(&self) -> bool { | |
259 | true | |
260 | } | |
261 | } | |
262 | ||
263 | pub struct OddLenSig; | |
264 | impl OddLenSig { | |
265 | // don't lint | |
266 | pub fn len(&self) -> bool { | |
267 | true | |
268 | } | |
269 | } | |
270 | ||
271 | // issue #6958 | |
272 | pub struct AsyncLen; | |
273 | impl AsyncLen { | |
274 | async fn async_task(&self) -> bool { | |
275 | true | |
276 | } | |
277 | ||
278 | pub async fn len(&self) -> usize { | |
279 | if self.async_task().await { 0 } else { 1 } | |
280 | } | |
281 | ||
282 | pub async fn is_empty(&self) -> bool { | |
283 | self.len().await == 0 | |
284 | } | |
285 | } | |
286 | ||
f20569fa | 287 | fn main() {} |