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