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 #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; // no error, len is private, see #1085
69 impl TraitsToo for One {
70 fn len(self: &Self) -> isize {
75 struct HasPrivateIsEmpty;
77 impl HasPrivateIsEmpty {
78 pub fn len(self: &Self) -> isize {
82 fn is_empty(self: &Self) -> bool {
87 pub struct HasIsEmpty;
90 pub fn len(self: &Self) -> isize {
94 fn is_empty(self: &Self) -> bool {
101 pub trait WithIsEmpty {
102 fn len(self: &Self) -> isize;
103 fn is_empty(self: &Self) -> bool;
106 impl WithIsEmpty for Wither {
107 fn len(self: &Self) -> isize {
111 fn is_empty(self: &Self) -> bool {
116 pub struct HasWrongIsEmpty;
118 impl HasWrongIsEmpty {
119 pub fn len(self: &Self) -> isize {
123 pub fn is_empty(self: &Self, x: u32) -> bool {
129 fn is_empty(&self) -> bool;
132 pub trait InheritingEmpty: Empty {
133 //must not trigger LEN_WITHOUT_IS_EMPTY
134 fn len(&self) -> isize;
140 println!("This should not happen!");
147 //no error because One does not have .is_empty()
148 println!("This should not happen either!");
151 let z: &TraitsToo = &y;
153 //no error, because TraitsToo has no .is_empty() method
154 println!("Nor should this!");
157 let has_is_empty = HasIsEmpty;
158 if has_is_empty.len() == 0 {
159 println!("Or this!");
161 if has_is_empty.len() != 0 {
162 println!("Or this!");
164 if has_is_empty.len() > 0 {
165 println!("Or this!");
167 if has_is_empty.len() < 1 {
168 println!("Or this!");
170 if has_is_empty.len() >= 1 {
171 println!("Or this!");
173 if has_is_empty.len() > 1 {
175 println!("This can happen.");
177 if has_is_empty.len() <= 1 {
179 println!("This can happen.");
181 if 0 == has_is_empty.len() {
182 println!("Or this!");
184 if 0 != has_is_empty.len() {
185 println!("Or this!");
187 if 0 < has_is_empty.len() {
188 println!("Or this!");
190 if 1 <= has_is_empty.len() {
191 println!("Or this!");
193 if 1 > has_is_empty.len() {
194 println!("Or this!");
196 if 1 < has_is_empty.len() {
198 println!("This can happen.");
200 if 1 >= has_is_empty.len() {
202 println!("This can happen.");
204 assert!(!has_is_empty.is_empty());
206 let with_is_empty: &WithIsEmpty = &Wither;
207 if with_is_empty.len() == 0 {
208 println!("Or this!");
210 assert!(!with_is_empty.is_empty());
212 let has_wrong_is_empty = HasWrongIsEmpty;
213 if has_wrong_is_empty.len() == 0 {
214 //no error as HasWrongIsEmpty does not have .is_empty()
215 println!("Or this!");
219 fn test_slice(b: &[u8]) {
224 pub trait Foo: Sized {}
226 pub trait DependsOnFoo: Foo {
227 fn len(&mut self) -> usize;