1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 #![warn(clippy::len_without_is_empty, clippy::len_zero)]
11 #![allow(dead_code, unused)]
16 pub fn len(self: &Self) -> isize {
22 // A second impl for this struct - the error span shouldn't mention this
23 pub fn irrelevant(self: &Self) -> bool {
28 // Identical to PubOne, but with an allow attribute on the impl complaining len
29 pub struct PubAllowed;
31 #[allow(clippy::len_without_is_empty)]
33 pub fn len(self: &Self) -> isize {
38 // No allow attribute on this impl block, but that doesn't matter - we only require one on the
39 // impl containing len.
41 pub fn irrelevant(self: &Self) -> bool {
49 pub fn len(self: &Self) -> isize {
50 // no error, len is pub but `NotPubOne` is not exported anyway
58 fn len(self: &Self) -> isize {
59 // no error, len is private, see #1085
64 pub trait PubTraitsToo {
65 fn len(self: &Self) -> isize;
68 impl PubTraitsToo for One {
69 fn len(self: &Self) -> isize {
75 fn len(self: &Self) -> isize; // no error, len is private, see #1085
78 impl TraitsToo for One {
79 fn len(self: &Self) -> isize {
84 struct HasPrivateIsEmpty;
86 impl HasPrivateIsEmpty {
87 pub fn len(self: &Self) -> isize {
91 fn is_empty(self: &Self) -> bool {
96 pub struct HasIsEmpty;
99 pub fn len(self: &Self) -> isize {
103 fn is_empty(self: &Self) -> bool {
110 pub trait WithIsEmpty {
111 fn len(self: &Self) -> isize;
112 fn is_empty(self: &Self) -> bool;
115 impl WithIsEmpty for Wither {
116 fn len(self: &Self) -> isize {
120 fn is_empty(self: &Self) -> bool {
125 pub struct HasWrongIsEmpty;
127 impl HasWrongIsEmpty {
128 pub fn len(self: &Self) -> isize {
132 pub fn is_empty(self: &Self, x: u32) -> bool {
138 fn is_empty(&self) -> bool;
141 pub trait InheritingEmpty: Empty {
142 //must not trigger LEN_WITHOUT_IS_EMPTY
143 fn len(&self) -> isize;
149 println!("This should not happen!");
156 //no error because One does not have .is_empty()
157 println!("This should not happen either!");
160 let z: &TraitsToo = &y;
162 //no error, because TraitsToo has no .is_empty() method
163 println!("Nor should this!");
166 let has_is_empty = HasIsEmpty;
167 if has_is_empty.len() == 0 {
168 println!("Or this!");
170 if has_is_empty.len() != 0 {
171 println!("Or this!");
173 if has_is_empty.len() > 0 {
174 println!("Or this!");
176 if has_is_empty.len() < 1 {
177 println!("Or this!");
179 if has_is_empty.len() >= 1 {
180 println!("Or this!");
182 if has_is_empty.len() > 1 {
184 println!("This can happen.");
186 if has_is_empty.len() <= 1 {
188 println!("This can happen.");
190 if 0 == has_is_empty.len() {
191 println!("Or this!");
193 if 0 != has_is_empty.len() {
194 println!("Or this!");
196 if 0 < has_is_empty.len() {
197 println!("Or this!");
199 if 1 <= has_is_empty.len() {
200 println!("Or this!");
202 if 1 > has_is_empty.len() {
203 println!("Or this!");
205 if 1 < has_is_empty.len() {
207 println!("This can happen.");
209 if 1 >= has_is_empty.len() {
211 println!("This can happen.");
213 assert!(!has_is_empty.is_empty());
215 let with_is_empty: &WithIsEmpty = &Wither;
216 if with_is_empty.len() == 0 {
217 println!("Or this!");
219 assert!(!with_is_empty.is_empty());
221 let has_wrong_is_empty = HasWrongIsEmpty;
222 if has_wrong_is_empty.len() == 0 {
223 //no error as HasWrongIsEmpty does not have .is_empty()
224 println!("Or this!");
228 fn test_slice(b: &[u8]) {
233 pub trait Foo: Sized {}
235 pub trait DependsOnFoo: Foo {
236 fn len(&mut self) -> usize;