]> git.lizzy.rs Git - rust.git/blob - tests/ui/len_without_is_empty.rs
Merge commit '6ed6f1e6a1a8f414ba7e6d9b8222e7e5a1686e42' into clippyup
[rust.git] / 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 pub struct MultipleImpls;
176
177 // issue #1562
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 fn main() {}