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