]> git.lizzy.rs Git - rust.git/blob - tests/ui/len_zero.rs
clean tests/ui/len_zero.rs
[rust.git] / tests / ui / len_zero.rs
1 #![feature(plugin)]
2 #![plugin(clippy)]
3
4 #![deny(len_without_is_empty, len_zero)]
5 #![allow(dead_code, unused)]
6
7 pub struct PubOne;
8
9 impl PubOne {
10     pub fn len(self: &Self) -> isize {
11         1
12     }
13 }
14
15 impl PubOne { // 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(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 { // no error, len is pub but `NotPubOne` is not exported anyway
43         1
44     }
45 }
46
47 struct One;
48
49 impl One {
50     fn len(self: &Self) -> isize { // 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 fn main() {
129     let x = [1, 2];
130     if x.len() == 0 {
131         println!("This should not happen!");
132     }
133
134     if "".len() == 0 {
135     }
136
137     let y = One;
138     if y.len()  == 0 { //no error because One does not have .is_empty()
139         println!("This should not happen either!");
140     }
141
142     let z : &TraitsToo = &y;
143     if z.len() > 0 { //no error, because TraitsToo has no .is_empty() method
144         println!("Nor should this!");
145     }
146
147     let has_is_empty = HasIsEmpty;
148     if has_is_empty.len() == 0 {
149         println!("Or this!");
150     }
151     if has_is_empty.len() != 0 {
152         println!("Or this!");
153     }
154     if has_is_empty.len() > 0 {
155         println!("Or this!");
156     }
157     assert!(!has_is_empty.is_empty());
158
159     let with_is_empty: &WithIsEmpty = &Wither;
160     if with_is_empty.len() == 0 {
161         println!("Or this!");
162     }
163     assert!(!with_is_empty.is_empty());
164
165     let has_wrong_is_empty = HasWrongIsEmpty;
166     if has_wrong_is_empty.len() == 0 { //no error as HasWrongIsEmpty does not have .is_empty()
167         println!("Or this!");
168     }
169 }
170
171 fn test_slice(b: &[u8]) {
172     if b.len() != 0 {
173     }
174 }