]> git.lizzy.rs Git - rust.git/blob - tests/ui/len_zero.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / len_zero.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![warn(clippy::len_without_is_empty, clippy::len_zero)]
11 #![allow(dead_code, unused)]
12
13 pub struct PubOne;
14
15 impl PubOne {
16     pub fn len(self: &Self) -> isize {
17         1
18     }
19 }
20
21 impl PubOne {
22     // A second impl for this struct - the error span shouldn't mention this
23     pub fn irrelevant(self: &Self) -> bool {
24         false
25     }
26 }
27
28 // Identical to PubOne, but with an allow attribute on the impl complaining len
29 pub struct PubAllowed;
30
31 #[allow(clippy::len_without_is_empty)]
32 impl PubAllowed {
33     pub fn len(self: &Self) -> isize {
34         1
35     }
36 }
37
38 // No allow attribute on this impl block, but that doesn't matter - we only require one on the
39 // impl containing len.
40 impl PubAllowed {
41     pub fn irrelevant(self: &Self) -> bool {
42         false
43     }
44 }
45
46 struct NotPubOne;
47
48 impl NotPubOne {
49     pub fn len(self: &Self) -> isize {
50         // no error, len is pub but `NotPubOne` is not exported anyway
51         1
52     }
53 }
54
55 struct One;
56
57 impl One {
58     fn len(self: &Self) -> isize {
59         // no error, len is private, see #1085
60         1
61     }
62 }
63
64 pub trait PubTraitsToo {
65     fn len(self: &Self) -> isize;
66 }
67
68 impl PubTraitsToo for One {
69     fn len(self: &Self) -> isize {
70         0
71     }
72 }
73
74 trait TraitsToo {
75     fn len(self: &Self) -> isize; // no error, len is private, see #1085
76 }
77
78 impl TraitsToo for One {
79     fn len(self: &Self) -> isize {
80         0
81     }
82 }
83
84 struct HasPrivateIsEmpty;
85
86 impl HasPrivateIsEmpty {
87     pub fn len(self: &Self) -> isize {
88         1
89     }
90
91     fn is_empty(self: &Self) -> bool {
92         false
93     }
94 }
95
96 pub struct HasIsEmpty;
97
98 impl HasIsEmpty {
99     pub fn len(self: &Self) -> isize {
100         1
101     }
102
103     fn is_empty(self: &Self) -> bool {
104         false
105     }
106 }
107
108 struct Wither;
109
110 pub trait WithIsEmpty {
111     fn len(self: &Self) -> isize;
112     fn is_empty(self: &Self) -> bool;
113 }
114
115 impl WithIsEmpty for Wither {
116     fn len(self: &Self) -> isize {
117         1
118     }
119
120     fn is_empty(self: &Self) -> bool {
121         false
122     }
123 }
124
125 pub struct HasWrongIsEmpty;
126
127 impl HasWrongIsEmpty {
128     pub fn len(self: &Self) -> isize {
129         1
130     }
131
132     pub fn is_empty(self: &Self, x: u32) -> bool {
133         false
134     }
135 }
136
137 pub trait Empty {
138     fn is_empty(&self) -> bool;
139 }
140
141 pub trait InheritingEmpty: Empty {
142     //must not trigger LEN_WITHOUT_IS_EMPTY
143     fn len(&self) -> isize;
144 }
145
146 fn main() {
147     let x = [1, 2];
148     if x.len() == 0 {
149         println!("This should not happen!");
150     }
151
152     if "".len() == 0 {}
153
154     let y = One;
155     if y.len() == 0 {
156         //no error because One does not have .is_empty()
157         println!("This should not happen either!");
158     }
159
160     let z: &TraitsToo = &y;
161     if z.len() > 0 {
162         //no error, because TraitsToo has no .is_empty() method
163         println!("Nor should this!");
164     }
165
166     let has_is_empty = HasIsEmpty;
167     if has_is_empty.len() == 0 {
168         println!("Or this!");
169     }
170     if has_is_empty.len() != 0 {
171         println!("Or this!");
172     }
173     if has_is_empty.len() > 0 {
174         println!("Or this!");
175     }
176     if has_is_empty.len() < 1 {
177         println!("Or this!");
178     }
179     if has_is_empty.len() >= 1 {
180         println!("Or this!");
181     }
182     if has_is_empty.len() > 1 {
183         // no error
184         println!("This can happen.");
185     }
186     if has_is_empty.len() <= 1 {
187         // no error
188         println!("This can happen.");
189     }
190     if 0 == has_is_empty.len() {
191         println!("Or this!");
192     }
193     if 0 != has_is_empty.len() {
194         println!("Or this!");
195     }
196     if 0 < has_is_empty.len() {
197         println!("Or this!");
198     }
199     if 1 <= has_is_empty.len() {
200         println!("Or this!");
201     }
202     if 1 > has_is_empty.len() {
203         println!("Or this!");
204     }
205     if 1 < has_is_empty.len() {
206         // no error
207         println!("This can happen.");
208     }
209     if 1 >= has_is_empty.len() {
210         // no error
211         println!("This can happen.");
212     }
213     assert!(!has_is_empty.is_empty());
214
215     let with_is_empty: &WithIsEmpty = &Wither;
216     if with_is_empty.len() == 0 {
217         println!("Or this!");
218     }
219     assert!(!with_is_empty.is_empty());
220
221     let has_wrong_is_empty = HasWrongIsEmpty;
222     if has_wrong_is_empty.len() == 0 {
223         //no error as HasWrongIsEmpty does not have .is_empty()
224         println!("Or this!");
225     }
226 }
227
228 fn test_slice(b: &[u8]) {
229     if b.len() != 0 {}
230 }
231
232 // this used to ICE
233 pub trait Foo: Sized {}
234
235 pub trait DependsOnFoo: Foo {
236     fn len(&mut self) -> usize;
237 }