]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/len_zero.rs
Auto merge of #107843 - bjorn3:sync_cg_clif-2023-02-09, r=bjorn3
[rust.git] / src / tools / clippy / tests / ui / len_zero.rs
1 // run-rustfix
2
3 #![warn(clippy::len_zero)]
4 #![allow(dead_code, unused, clippy::len_without_is_empty)]
5
6 extern crate core;
7 use core::ops::Deref;
8
9 pub struct One;
10 struct Wither;
11
12 trait TraitsToo {
13     fn len(&self) -> isize;
14     // No error; `len` is private; see issue #1085.
15 }
16
17 impl TraitsToo for One {
18     fn len(&self) -> isize {
19         0
20     }
21 }
22
23 pub struct HasIsEmpty;
24
25 impl HasIsEmpty {
26     pub fn len(&self) -> isize {
27         1
28     }
29
30     fn is_empty(&self) -> bool {
31         false
32     }
33 }
34
35 pub struct HasWrongIsEmpty;
36
37 impl HasWrongIsEmpty {
38     pub fn len(&self) -> isize {
39         1
40     }
41
42     pub fn is_empty(&self, x: u32) -> bool {
43         false
44     }
45 }
46
47 pub trait WithIsEmpty {
48     fn len(&self) -> isize;
49     fn is_empty(&self) -> bool;
50 }
51
52 impl WithIsEmpty for Wither {
53     fn len(&self) -> isize {
54         1
55     }
56
57     fn is_empty(&self) -> bool {
58         false
59     }
60 }
61
62 struct DerefToDerefToString;
63
64 impl Deref for DerefToDerefToString {
65     type Target = DerefToString;
66
67     fn deref(&self) -> &Self::Target {
68         &DerefToString {}
69     }
70 }
71
72 struct DerefToString;
73
74 impl Deref for DerefToString {
75     type Target = str;
76
77     fn deref(&self) -> &Self::Target {
78         "Hello, world!"
79     }
80 }
81
82 fn main() {
83     let x = [1, 2];
84     if x.len() == 0 {
85         println!("This should not happen!");
86     }
87
88     if "".len() == 0 {}
89
90     let s = "Hello, world!";
91     let s1 = &s;
92     let s2 = &s1;
93     let s3 = &s2;
94     let s4 = &s3;
95     let s5 = &s4;
96     let s6 = &s5;
97     println!("{}", *s1 == "");
98     println!("{}", **s2 == "");
99     println!("{}", ***s3 == "");
100     println!("{}", ****s4 == "");
101     println!("{}", *****s5 == "");
102     println!("{}", ******(s6) == "");
103
104     let d2s = DerefToDerefToString {};
105     println!("{}", &**d2s == "");
106
107     let y = One;
108     if y.len() == 0 {
109         // No error; `One` does not have `.is_empty()`.
110         println!("This should not happen either!");
111     }
112
113     let z: &dyn TraitsToo = &y;
114     if z.len() > 0 {
115         // No error; `TraitsToo` has no `.is_empty()` method.
116         println!("Nor should this!");
117     }
118
119     let has_is_empty = HasIsEmpty;
120     if has_is_empty.len() == 0 {
121         println!("Or this!");
122     }
123     if has_is_empty.len() != 0 {
124         println!("Or this!");
125     }
126     if has_is_empty.len() > 0 {
127         println!("Or this!");
128     }
129     if has_is_empty.len() < 1 {
130         println!("Or this!");
131     }
132     if has_is_empty.len() >= 1 {
133         println!("Or this!");
134     }
135     if has_is_empty.len() > 1 {
136         // No error.
137         println!("This can happen.");
138     }
139     if has_is_empty.len() <= 1 {
140         // No error.
141         println!("This can happen.");
142     }
143     if 0 == has_is_empty.len() {
144         println!("Or this!");
145     }
146     if 0 != has_is_empty.len() {
147         println!("Or this!");
148     }
149     if 0 < has_is_empty.len() {
150         println!("Or this!");
151     }
152     if 1 <= has_is_empty.len() {
153         println!("Or this!");
154     }
155     if 1 > has_is_empty.len() {
156         println!("Or this!");
157     }
158     if 1 < has_is_empty.len() {
159         // No error.
160         println!("This can happen.");
161     }
162     if 1 >= has_is_empty.len() {
163         // No error.
164         println!("This can happen.");
165     }
166     assert!(!has_is_empty.is_empty());
167
168     let with_is_empty: &dyn WithIsEmpty = &Wither;
169     if with_is_empty.len() == 0 {
170         println!("Or this!");
171     }
172     assert!(!with_is_empty.is_empty());
173
174     let has_wrong_is_empty = HasWrongIsEmpty;
175     if has_wrong_is_empty.len() == 0 {
176         // No error; `HasWrongIsEmpty` does not have `.is_empty()`.
177         println!("Or this!");
178     }
179 }
180
181 fn test_slice(b: &[u8]) {
182     if b.len() != 0 {}
183 }