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