]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/len_zero.rs
48a10042658741ea8e2eebb1703a0429eb2412fa
[rust.git] / tests / compile-fail / len_zero.rs
1 #![feature(plugin)]
2 #![plugin(clippy)]
3
4 struct One;
5
6 #[deny(len_without_is_empty)]
7 impl One {
8     fn len(self: &Self) -> isize { //~ERROR Item 'One' has a '.len(_: &Self)'
9         1
10     }
11 }
12
13 #[deny(len_without_is_empty)]
14 trait TraitsToo {
15     fn len(self: &Self) -> isize; //~ERROR Trait 'TraitsToo' has a '.len(_:
16 }
17
18 impl TraitsToo for One {
19     fn len(self: &Self) -> isize {
20         0
21     }
22 }
23
24 struct HasIsEmpty;
25
26 #[deny(len_without_is_empty)]
27 impl HasIsEmpty {
28     fn len(self: &Self) -> isize {
29         1
30     }
31
32     fn is_empty(self: &Self) -> bool {
33         false
34     }
35 }
36
37 struct Wither;
38
39 #[deny(len_without_is_empty)]
40 trait WithIsEmpty {
41     fn len(self: &Self) -> isize;
42     fn is_empty(self: &Self) -> bool;
43 }
44
45 impl WithIsEmpty for Wither {
46     fn len(self: &Self) -> isize {
47         1
48     }
49
50     fn is_empty(self: &Self) -> bool {
51         false
52     }
53 }
54
55 struct HasWrongIsEmpty;
56
57 #[deny(len_without_is_empty)]
58 impl HasWrongIsEmpty {
59     fn len(self: &Self) -> isize { //~ERROR Item 'HasWrongIsEmpty' has a '.len(_: &Self)'
60         1
61     }
62
63     #[allow(dead_code, unused)]
64     fn is_empty(self: &Self, x : u32) -> bool {
65         false
66     }
67 }
68
69 #[deny(len_zero)]
70 fn main() {
71     let x = [1, 2];
72     if x.len() == 0 { //~ERROR Consider replacing the len comparison
73         println!("This should not happen!");
74     }
75
76     let y = One;
77     if y.len()  == 0 { //no error because One does not have .is_empty()
78         println!("This should not happen either!");
79     }
80
81     let z : &TraitsToo = &y;
82     if z.len() > 0 { //no error, because TraitsToo has no .is_empty() method
83         println!("Nor should this!");
84     }
85
86     let hie = HasIsEmpty;
87     if hie.len() == 0 { //~ERROR Consider replacing the len comparison
88         println!("Or this!");
89     }
90     assert!(!hie.is_empty());
91
92     let wie : &WithIsEmpty = &Wither;
93     if wie.len() == 0 { //~ERROR Consider replacing the len comparison
94         println!("Or this!");
95     }
96     assert!(!wie.is_empty());
97
98     let hwie = HasWrongIsEmpty;
99     if hwie.len() == 0 { //no error as HasWrongIsEmpty does not have .is_empty()
100         println!("Or this!");
101     }
102 }