]> git.lizzy.rs Git - rust.git/blob - tests/ui/len_zero.rs
Merge pull request #3265 from mikerite/fix-export
[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
11 #![feature(tool_lints)]
12
13 #![warn(clippy::len_without_is_empty, clippy::len_zero)]
14 #![allow(dead_code, unused)]
15
16 pub struct PubOne;
17
18 impl PubOne {
19     pub fn len(self: &Self) -> isize {
20         1
21     }
22 }
23
24 impl PubOne {
25     // A second impl for this struct - the error span shouldn't mention this
26     pub fn irrelevant(self: &Self) -> bool {
27         false
28     }
29 }
30
31 // Identical to PubOne, but with an allow attribute on the impl complaining len
32 pub struct PubAllowed;
33
34 #[allow(clippy::len_without_is_empty)]
35 impl PubAllowed {
36     pub fn len(self: &Self) -> isize {
37         1
38     }
39 }
40
41 // No allow attribute on this impl block, but that doesn't matter - we only require one on the
42 // impl containing len.
43 impl PubAllowed {
44     pub fn irrelevant(self: &Self) -> bool {
45         false
46     }
47 }
48
49 struct NotPubOne;
50
51 impl NotPubOne {
52     pub fn len(self: &Self) -> isize {
53         // no error, len is pub but `NotPubOne` is not exported anyway
54         1
55     }
56 }
57
58 struct One;
59
60 impl One {
61     fn len(self: &Self) -> isize {
62         // no error, len is private, see #1085
63         1
64     }
65 }
66
67 pub trait PubTraitsToo {
68     fn len(self: &Self) -> isize;
69 }
70
71 impl PubTraitsToo for One {
72     fn len(self: &Self) -> isize {
73         0
74     }
75 }
76
77 trait TraitsToo {
78     fn len(self: &Self) -> isize; // no error, len is private, see #1085
79 }
80
81 impl TraitsToo for One {
82     fn len(self: &Self) -> isize {
83         0
84     }
85 }
86
87 struct HasPrivateIsEmpty;
88
89 impl HasPrivateIsEmpty {
90     pub fn len(self: &Self) -> isize {
91         1
92     }
93
94     fn is_empty(self: &Self) -> bool {
95         false
96     }
97 }
98
99 pub struct HasIsEmpty;
100
101 impl HasIsEmpty {
102     pub fn len(self: &Self) -> isize {
103         1
104     }
105
106     fn is_empty(self: &Self) -> bool {
107         false
108     }
109 }
110
111 struct Wither;
112
113 pub trait WithIsEmpty {
114     fn len(self: &Self) -> isize;
115     fn is_empty(self: &Self) -> bool;
116 }
117
118 impl WithIsEmpty for Wither {
119     fn len(self: &Self) -> isize {
120         1
121     }
122
123     fn is_empty(self: &Self) -> bool {
124         false
125     }
126 }
127
128 pub struct HasWrongIsEmpty;
129
130 impl HasWrongIsEmpty {
131     pub fn len(self: &Self) -> isize {
132         1
133     }
134
135     pub fn is_empty(self: &Self, x: u32) -> bool {
136         false
137     }
138 }
139
140 pub trait Empty {
141     fn is_empty(&self) -> bool;
142 }
143
144 pub trait InheritingEmpty: Empty {
145     //must not trigger LEN_WITHOUT_IS_EMPTY
146     fn len(&self) -> isize;
147 }
148
149 fn main() {
150     let x = [1, 2];
151     if x.len() == 0 {
152         println!("This should not happen!");
153     }
154
155     if "".len() == 0 {}
156
157     let y = One;
158     if y.len() == 0 {
159         //no error because One does not have .is_empty()
160         println!("This should not happen either!");
161     }
162
163     let z: &TraitsToo = &y;
164     if z.len() > 0 {
165         //no error, because TraitsToo has no .is_empty() method
166         println!("Nor should this!");
167     }
168
169     let has_is_empty = HasIsEmpty;
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() > 0 {
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         println!("Or this!");
184     }
185     if has_is_empty.len() > 1 {
186         // no error
187         println!("This can happen.");
188     }
189     if has_is_empty.len() <= 1 {
190         // no error
191         println!("This can happen.");
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 0 < 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         println!("Or this!");
207     }
208     if 1 < has_is_empty.len() {
209         // no error
210         println!("This can happen.");
211     }
212     if 1 >= has_is_empty.len() {
213         // no error
214         println!("This can happen.");
215     }
216     assert!(!has_is_empty.is_empty());
217
218     let with_is_empty: &WithIsEmpty = &Wither;
219     if with_is_empty.len() == 0 {
220         println!("Or this!");
221     }
222     assert!(!with_is_empty.is_empty());
223
224     let has_wrong_is_empty = HasWrongIsEmpty;
225     if has_wrong_is_empty.len() == 0 {
226         //no error as HasWrongIsEmpty does not have .is_empty()
227         println!("Or this!");
228     }
229 }
230
231 fn test_slice(b: &[u8]) {
232     if b.len() != 0 {}
233 }
234
235 // this used to ICE
236 pub trait Foo: Sized {}
237
238 pub trait DependsOnFoo: Foo {
239     fn len(&mut self) -> usize;
240 }