]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/len_zero.fixed
Merge commit '3d0b0e66afdfaa519d8855b338b35b4605775945' into clippyup
[rust.git] / src / tools / clippy / tests / ui / len_zero.fixed
1 // run-rustfix
2
3 #![warn(clippy::len_zero)]
4 #![allow(dead_code, unused, clippy::len_without_is_empty)]
5
6 pub struct One;
7 struct Wither;
8
9 trait TraitsToo {
10     fn len(&self) -> isize;
11     // No error; `len` is private; see issue #1085.
12 }
13
14 impl TraitsToo for One {
15     fn len(&self) -> isize {
16         0
17     }
18 }
19
20 pub struct HasIsEmpty;
21
22 impl HasIsEmpty {
23     pub fn len(&self) -> isize {
24         1
25     }
26
27     fn is_empty(&self) -> bool {
28         false
29     }
30 }
31
32 pub struct HasWrongIsEmpty;
33
34 impl HasWrongIsEmpty {
35     pub fn len(&self) -> isize {
36         1
37     }
38
39     pub fn is_empty(&self, x: u32) -> bool {
40         false
41     }
42 }
43
44 pub trait WithIsEmpty {
45     fn len(&self) -> isize;
46     fn is_empty(&self) -> bool;
47 }
48
49 impl WithIsEmpty for Wither {
50     fn len(&self) -> isize {
51         1
52     }
53
54     fn is_empty(&self) -> bool {
55         false
56     }
57 }
58
59 fn main() {
60     let x = [1, 2];
61     if x.is_empty() {
62         println!("This should not happen!");
63     }
64
65     if "".is_empty() {}
66
67     let y = One;
68     if y.len() == 0 {
69         // No error; `One` does not have `.is_empty()`.
70         println!("This should not happen either!");
71     }
72
73     let z: &dyn TraitsToo = &y;
74     if z.len() > 0 {
75         // No error; `TraitsToo` has no `.is_empty()` method.
76         println!("Nor should this!");
77     }
78
79     let has_is_empty = HasIsEmpty;
80     if has_is_empty.is_empty() {
81         println!("Or this!");
82     }
83     if !has_is_empty.is_empty() {
84         println!("Or this!");
85     }
86     if !has_is_empty.is_empty() {
87         println!("Or this!");
88     }
89     if has_is_empty.is_empty() {
90         println!("Or this!");
91     }
92     if !has_is_empty.is_empty() {
93         println!("Or this!");
94     }
95     if has_is_empty.len() > 1 {
96         // No error.
97         println!("This can happen.");
98     }
99     if has_is_empty.len() <= 1 {
100         // No error.
101         println!("This can happen.");
102     }
103     if has_is_empty.is_empty() {
104         println!("Or this!");
105     }
106     if !has_is_empty.is_empty() {
107         println!("Or this!");
108     }
109     if !has_is_empty.is_empty() {
110         println!("Or this!");
111     }
112     if !has_is_empty.is_empty() {
113         println!("Or this!");
114     }
115     if has_is_empty.is_empty() {
116         println!("Or this!");
117     }
118     if 1 < has_is_empty.len() {
119         // No error.
120         println!("This can happen.");
121     }
122     if 1 >= has_is_empty.len() {
123         // No error.
124         println!("This can happen.");
125     }
126     assert!(!has_is_empty.is_empty());
127
128     let with_is_empty: &dyn WithIsEmpty = &Wither;
129     if with_is_empty.is_empty() {
130         println!("Or this!");
131     }
132     assert!(!with_is_empty.is_empty());
133
134     let has_wrong_is_empty = HasWrongIsEmpty;
135     if has_wrong_is_empty.len() == 0 {
136         // No error; `HasWrongIsEmpty` does not have `.is_empty()`.
137         println!("Or this!");
138     }
139 }
140
141 fn test_slice(b: &[u8]) {
142     if !b.is_empty() {}
143 }