]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/len_without_is_empty.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / len_without_is_empty.rs
CommitLineData
cdc7bbd5
XL
1// edition:2018
2
f20569fa
XL
3#![warn(clippy::len_without_is_empty)]
4#![allow(dead_code, unused)]
5
6pub struct PubOne;
7
8impl PubOne {
9 pub fn len(&self) -> isize {
10 1
11 }
12}
13
14impl 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`.
22pub struct PubAllowed;
23
24#[allow(clippy::len_without_is_empty)]
25impl 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`.
33impl PubAllowed {
34 pub fn irrelevant(&self) -> bool {
35 false
36 }
37}
38
39pub struct PubAllowedFn;
40
41impl 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)]
49pub struct PubAllowedStruct;
50
51impl PubAllowedStruct {
52 pub fn len(&self) -> isize {
53 1
54 }
55}
56
57pub trait PubTraitsToo {
58 fn len(&self) -> isize;
59}
60
61impl PubTraitsToo for One {
62 fn len(&self) -> isize {
63 0
64 }
65}
66
67pub struct HasIsEmpty;
68
69impl HasIsEmpty {
70 pub fn len(&self) -> isize {
71 1
72 }
73
74 fn is_empty(&self) -> bool {
75 false
76 }
77}
78
79pub struct HasWrongIsEmpty;
80
81impl 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
91pub struct MismatchedSelf;
92
93impl MismatchedSelf {
94 pub fn len(self) -> isize {
95 1
96 }
97
98 pub fn is_empty(&self) -> bool {
99 false
100 }
101}
102
103struct NotPubOne;
104
105impl NotPubOne {
106 pub fn len(&self) -> isize {
107 // No error; `len` is pub but `NotPubOne` is not exported anyway.
108 1
109 }
110}
111
112struct One;
113
114impl One {
115 fn len(&self) -> isize {
116 // No error; `len` is private; see issue #1085.
117 1
118 }
119}
120
121trait TraitsToo {
122 fn len(&self) -> isize;
123 // No error; `len` is private; see issue #1085.
124}
125
126impl TraitsToo for One {
127 fn len(&self) -> isize {
128 0
129 }
130}
131
132struct HasPrivateIsEmpty;
133
134impl HasPrivateIsEmpty {
135 pub fn len(&self) -> isize {
136 1
137 }
138
139 fn is_empty(&self) -> bool {
140 false
141 }
142}
143
144struct Wither;
145
146pub trait WithIsEmpty {
147 fn len(&self) -> isize;
148 fn is_empty(&self) -> bool;
149}
150
151impl WithIsEmpty for Wither {
152 fn len(&self) -> isize {
153 1
154 }
155
156 fn is_empty(&self) -> bool {
157 false
158 }
159}
160
161pub trait Empty {
162 fn is_empty(&self) -> bool;
163}
164
165pub trait InheritingEmpty: Empty {
166 // Must not trigger `LEN_WITHOUT_IS_EMPTY`.
167 fn len(&self) -> isize;
168}
169
170// This used to ICE.
171pub trait Foo: Sized {}
172
173pub trait DependsOnFoo: Foo {
174 fn len(&mut self) -> usize;
175}
176
cdc7bbd5 177// issue #1562
f20569fa
XL
178pub struct MultipleImpls;
179
f20569fa
XL
180impl MultipleImpls {
181 pub fn len(&self) -> usize {
182 1
183 }
184}
185
186impl MultipleImpls {
187 pub fn is_empty(&self) -> bool {
188 false
189 }
190}
191
cdc7bbd5
XL
192// issue #6958
193pub struct OptionalLen;
194
195impl 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
205pub struct OptionalLen2;
206impl 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
216pub struct OptionalLen3;
217impl 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
228pub struct ResultLen;
229impl 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
240pub struct ResultLen2;
241impl 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
251pub struct ResultLen3;
252impl 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
263pub struct OddLenSig;
264impl OddLenSig {
265 // don't lint
266 pub fn len(&self) -> bool {
267 true
268 }
269}
270
271// issue #6958
272pub struct AsyncLen;
273impl 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 287fn main() {}