1 #![feature(tool_lints)]
3 #![warn(clippy::len_without_is_empty, clippy::len_zero)]
4 #![allow(dead_code, unused)]
9 pub fn len(self: &Self) -> isize {
15 // A second impl for this struct - the error span shouldn't mention this
16 pub fn irrelevant(self: &Self) -> bool {
21 // Identical to PubOne, but with an allow attribute on the impl complaining len
22 pub struct PubAllowed;
24 #[allow(clippy::len_without_is_empty)]
26 pub fn len(self: &Self) -> isize {
31 // No allow attribute on this impl block, but that doesn't matter - we only require one on the
32 // impl containing len.
34 pub fn irrelevant(self: &Self) -> bool {
42 pub fn len(self: &Self) -> isize {
43 // no error, len is pub but `NotPubOne` is not exported anyway
51 fn len(self: &Self) -> isize {
52 // no error, len is private, see #1085
57 pub trait PubTraitsToo {
58 fn len(self: &Self) -> isize;
61 impl PubTraitsToo for One {
62 fn len(self: &Self) -> isize {
68 fn len(self: &Self) -> isize; // no error, len is private, see #1085
71 impl TraitsToo for One {
72 fn len(self: &Self) -> isize {
77 struct HasPrivateIsEmpty;
79 impl HasPrivateIsEmpty {
80 pub fn len(self: &Self) -> isize {
84 fn is_empty(self: &Self) -> bool {
89 pub struct HasIsEmpty;
92 pub fn len(self: &Self) -> isize {
96 fn is_empty(self: &Self) -> bool {
103 pub trait WithIsEmpty {
104 fn len(self: &Self) -> isize;
105 fn is_empty(self: &Self) -> bool;
108 impl WithIsEmpty for Wither {
109 fn len(self: &Self) -> isize {
113 fn is_empty(self: &Self) -> bool {
118 pub struct HasWrongIsEmpty;
120 impl HasWrongIsEmpty {
121 pub fn len(self: &Self) -> isize {
125 pub fn is_empty(self: &Self, x: u32) -> bool {
131 fn is_empty(&self) -> bool;
134 pub trait InheritingEmpty: Empty {
135 //must not trigger LEN_WITHOUT_IS_EMPTY
136 fn len(&self) -> isize;
142 println!("This should not happen!");
149 //no error because One does not have .is_empty()
150 println!("This should not happen either!");
153 let z: &TraitsToo = &y;
155 //no error, because TraitsToo has no .is_empty() method
156 println!("Nor should this!");
159 let has_is_empty = HasIsEmpty;
160 if has_is_empty.len() == 0 {
161 println!("Or this!");
163 if has_is_empty.len() != 0 {
164 println!("Or this!");
166 if has_is_empty.len() > 0 {
167 println!("Or this!");
169 if has_is_empty.len() < 1 {
170 println!("Or this!");
172 if has_is_empty.len() >= 1 {
173 println!("Or this!");
175 if has_is_empty.len() > 1 {
177 println!("This can happen.");
179 if has_is_empty.len() <= 1 {
181 println!("This can happen.");
183 if 0 == has_is_empty.len() {
184 println!("Or this!");
186 if 0 != has_is_empty.len() {
187 println!("Or this!");
189 if 0 < has_is_empty.len() {
190 println!("Or this!");
192 if 1 <= has_is_empty.len() {
193 println!("Or this!");
195 if 1 > has_is_empty.len() {
196 println!("Or this!");
198 if 1 < has_is_empty.len() {
200 println!("This can happen.");
202 if 1 >= has_is_empty.len() {
204 println!("This can happen.");
206 assert!(!has_is_empty.is_empty());
208 let with_is_empty: &WithIsEmpty = &Wither;
209 if with_is_empty.len() == 0 {
210 println!("Or this!");
212 assert!(!with_is_empty.is_empty());
214 let has_wrong_is_empty = HasWrongIsEmpty;
215 if has_wrong_is_empty.len() == 0 {
216 //no error as HasWrongIsEmpty does not have .is_empty()
217 println!("Or this!");
221 fn test_slice(b: &[u8]) {
226 pub trait Foo: Sized {}
228 pub trait DependsOnFoo: Foo {
229 fn len(&mut self) -> usize;