]> git.lizzy.rs Git - rust.git/blob - tests/ui/len_zero.rs
Merge pull request #3918 from matthiaskrgr/typos
[rust.git] / tests / ui / len_zero.rs
1 #![warn(clippy::len_without_is_empty, clippy::len_zero)]
2 #![allow(dead_code, unused)]
3
4 pub struct PubOne;
5
6 impl PubOne {
7     pub fn len(self: &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: &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: &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: &Self) -> bool {
33         false
34     }
35 }
36
37 struct NotPubOne;
38
39 impl NotPubOne {
40     pub fn len(self: &Self) -> isize {
41         // No error; `len` is pub but `NotPubOne` is not exported anyway.
42         1
43     }
44 }
45
46 struct One;
47
48 impl One {
49     fn len(self: &Self) -> isize {
50         // No error; `len` is private; see issue #1085.
51         1
52     }
53 }
54
55 pub trait PubTraitsToo {
56     fn len(self: &Self) -> isize;
57 }
58
59 impl PubTraitsToo for One {
60     fn len(self: &Self) -> isize {
61         0
62     }
63 }
64
65 trait TraitsToo {
66     fn len(self: &Self) -> isize;
67     // No error; `len` is private; see issue #1085.
68 }
69
70 impl TraitsToo for One {
71     fn len(self: &Self) -> isize {
72         0
73     }
74 }
75
76 struct HasPrivateIsEmpty;
77
78 impl HasPrivateIsEmpty {
79     pub fn len(self: &Self) -> isize {
80         1
81     }
82
83     fn is_empty(self: &Self) -> bool {
84         false
85     }
86 }
87
88 pub struct HasIsEmpty;
89
90 impl HasIsEmpty {
91     pub fn len(self: &Self) -> isize {
92         1
93     }
94
95     fn is_empty(self: &Self) -> bool {
96         false
97     }
98 }
99
100 struct Wither;
101
102 pub trait WithIsEmpty {
103     fn len(self: &Self) -> isize;
104     fn is_empty(self: &Self) -> bool;
105 }
106
107 impl WithIsEmpty for Wither {
108     fn len(self: &Self) -> isize {
109         1
110     }
111
112     fn is_empty(self: &Self) -> bool {
113         false
114     }
115 }
116
117 pub struct HasWrongIsEmpty;
118
119 impl HasWrongIsEmpty {
120     pub fn len(self: &Self) -> isize {
121         1
122     }
123
124     pub fn is_empty(self: &Self, x: u32) -> bool {
125         false
126     }
127 }
128
129 pub trait Empty {
130     fn is_empty(&self) -> bool;
131 }
132
133 pub trait InheritingEmpty: Empty {
134     // Must not trigger `LEN_WITHOUT_IS_EMPTY`.
135     fn len(&self) -> isize;
136 }
137
138 fn main() {
139     let x = [1, 2];
140     if x.len() == 0 {
141         println!("This should not happen!");
142     }
143
144     if "".len() == 0 {}
145
146     let y = One;
147     if y.len() == 0 {
148         // No error; `One` does not have `.is_empty()`.
149         println!("This should not happen either!");
150     }
151
152     let z: &TraitsToo = &y;
153     if z.len() > 0 {
154         // No error; `TraitsToo` has no `.is_empty()` method.
155         println!("Nor should this!");
156     }
157
158     let has_is_empty = HasIsEmpty;
159     if has_is_empty.len() == 0 {
160         println!("Or this!");
161     }
162     if has_is_empty.len() != 0 {
163         println!("Or this!");
164     }
165     if has_is_empty.len() > 0 {
166         println!("Or this!");
167     }
168     if has_is_empty.len() < 1 {
169         println!("Or this!");
170     }
171     if has_is_empty.len() >= 1 {
172         println!("Or this!");
173     }
174     if has_is_empty.len() > 1 {
175         // No error.
176         println!("This can happen.");
177     }
178     if has_is_empty.len() <= 1 {
179         // No error.
180         println!("This can happen.");
181     }
182     if 0 == has_is_empty.len() {
183         println!("Or this!");
184     }
185     if 0 != has_is_empty.len() {
186         println!("Or this!");
187     }
188     if 0 < has_is_empty.len() {
189         println!("Or this!");
190     }
191     if 1 <= has_is_empty.len() {
192         println!("Or this!");
193     }
194     if 1 > has_is_empty.len() {
195         println!("Or this!");
196     }
197     if 1 < has_is_empty.len() {
198         // No error.
199         println!("This can happen.");
200     }
201     if 1 >= has_is_empty.len() {
202         // No error.
203         println!("This can happen.");
204     }
205     assert!(!has_is_empty.is_empty());
206
207     let with_is_empty: &WithIsEmpty = &Wither;
208     if with_is_empty.len() == 0 {
209         println!("Or this!");
210     }
211     assert!(!with_is_empty.is_empty());
212
213     let has_wrong_is_empty = HasWrongIsEmpty;
214     if has_wrong_is_empty.len() == 0 {
215         // No error; `HasWrongIsEmpty` does not have `.is_empty()`.
216         println!("Or this!");
217     }
218 }
219
220 fn test_slice(b: &[u8]) {
221     if b.len() != 0 {}
222 }
223
224 // This used to ICE.
225 pub trait Foo: Sized {}
226
227 pub trait DependsOnFoo: Foo {
228     fn len(&mut self) -> usize;
229 }