1 #![warn(clippy::len_without_is_empty, clippy::len_zero)]
2 #![allow(dead_code, unused)]
7 pub fn len(self: &Self) -> isize {
13 // A second impl for this struct -- the error span shouldn't mention this.
14 pub fn irrelevant(self: &Self) -> bool {
19 // Identical to `PubOne`, but with an `allow` attribute on the impl complaining `len`.
20 pub struct PubAllowed;
22 #[allow(clippy::len_without_is_empty)]
24 pub fn len(self: &Self) -> isize {
29 // No `allow` attribute on this impl block, but that doesn't matter -- we only require one on the
30 // impl containing `len`.
32 pub fn irrelevant(self: &Self) -> bool {
40 pub fn len(self: &Self) -> isize {
41 // No error; `len` is pub but `NotPubOne` is not exported anyway.
49 fn len(self: &Self) -> isize {
50 // No error; `len` is private; see issue #1085.
55 pub trait PubTraitsToo {
56 fn len(self: &Self) -> isize;
59 impl PubTraitsToo for One {
60 fn len(self: &Self) -> isize {
66 fn len(self: &Self) -> isize;
67 // No error; `len` is private; see issue #1085.
70 impl TraitsToo for One {
71 fn len(self: &Self) -> isize {
76 struct HasPrivateIsEmpty;
78 impl HasPrivateIsEmpty {
79 pub fn len(self: &Self) -> isize {
83 fn is_empty(self: &Self) -> bool {
88 pub struct HasIsEmpty;
91 pub fn len(self: &Self) -> isize {
95 fn is_empty(self: &Self) -> bool {
102 pub trait WithIsEmpty {
103 fn len(self: &Self) -> isize;
104 fn is_empty(self: &Self) -> bool;
107 impl WithIsEmpty for Wither {
108 fn len(self: &Self) -> isize {
112 fn is_empty(self: &Self) -> bool {
117 pub struct HasWrongIsEmpty;
119 impl HasWrongIsEmpty {
120 pub fn len(self: &Self) -> isize {
124 pub fn is_empty(self: &Self, x: u32) -> bool {
130 fn is_empty(&self) -> bool;
133 pub trait InheritingEmpty: Empty {
134 // Must not trigger `LEN_WITHOUT_IS_EMPTY`.
135 fn len(&self) -> isize;
141 println!("This should not happen!");
148 // No error; `One` does not have `.is_empty()`.
149 println!("This should not happen either!");
152 let z: &TraitsToo = &y;
154 // No error; `TraitsToo` has no `.is_empty()` method.
155 println!("Nor should this!");
158 let has_is_empty = HasIsEmpty;
159 if has_is_empty.len() == 0 {
160 println!("Or this!");
162 if has_is_empty.len() != 0 {
163 println!("Or this!");
165 if has_is_empty.len() > 0 {
166 println!("Or this!");
168 if has_is_empty.len() < 1 {
169 println!("Or this!");
171 if has_is_empty.len() >= 1 {
172 println!("Or this!");
174 if has_is_empty.len() > 1 {
176 println!("This can happen.");
178 if has_is_empty.len() <= 1 {
180 println!("This can happen.");
182 if 0 == has_is_empty.len() {
183 println!("Or this!");
185 if 0 != has_is_empty.len() {
186 println!("Or this!");
188 if 0 < has_is_empty.len() {
189 println!("Or this!");
191 if 1 <= has_is_empty.len() {
192 println!("Or this!");
194 if 1 > has_is_empty.len() {
195 println!("Or this!");
197 if 1 < has_is_empty.len() {
199 println!("This can happen.");
201 if 1 >= has_is_empty.len() {
203 println!("This can happen.");
205 assert!(!has_is_empty.is_empty());
207 let with_is_empty: &WithIsEmpty = &Wither;
208 if with_is_empty.len() == 0 {
209 println!("Or this!");
211 assert!(!with_is_empty.is_empty());
213 let has_wrong_is_empty = HasWrongIsEmpty;
214 if has_wrong_is_empty.len() == 0 {
215 // No error; `HasWrongIsEmpty` does not have `.is_empty()`.
216 println!("Or this!");
220 fn test_slice(b: &[u8]) {
225 pub trait Foo: Sized {}
227 pub trait DependsOnFoo: Foo {
228 fn len(&mut self) -> usize;