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.
11 #![feature(tool_lints)]
13 #![warn(clippy::len_without_is_empty, clippy::len_zero)]
14 #![allow(dead_code, unused)]
19 pub fn len(self: &Self) -> isize {
25 // A second impl for this struct - the error span shouldn't mention this
26 pub fn irrelevant(self: &Self) -> bool {
31 // Identical to PubOne, but with an allow attribute on the impl complaining len
32 pub struct PubAllowed;
34 #[allow(clippy::len_without_is_empty)]
36 pub fn len(self: &Self) -> isize {
41 // No allow attribute on this impl block, but that doesn't matter - we only require one on the
42 // impl containing len.
44 pub fn irrelevant(self: &Self) -> bool {
52 pub fn len(self: &Self) -> isize {
53 // no error, len is pub but `NotPubOne` is not exported anyway
61 fn len(self: &Self) -> isize {
62 // no error, len is private, see #1085
67 pub trait PubTraitsToo {
68 fn len(self: &Self) -> isize;
71 impl PubTraitsToo for One {
72 fn len(self: &Self) -> isize {
78 fn len(self: &Self) -> isize; // no error, len is private, see #1085
81 impl TraitsToo for One {
82 fn len(self: &Self) -> isize {
87 struct HasPrivateIsEmpty;
89 impl HasPrivateIsEmpty {
90 pub fn len(self: &Self) -> isize {
94 fn is_empty(self: &Self) -> bool {
99 pub struct HasIsEmpty;
102 pub fn len(self: &Self) -> isize {
106 fn is_empty(self: &Self) -> bool {
113 pub trait WithIsEmpty {
114 fn len(self: &Self) -> isize;
115 fn is_empty(self: &Self) -> bool;
118 impl WithIsEmpty for Wither {
119 fn len(self: &Self) -> isize {
123 fn is_empty(self: &Self) -> bool {
128 pub struct HasWrongIsEmpty;
130 impl HasWrongIsEmpty {
131 pub fn len(self: &Self) -> isize {
135 pub fn is_empty(self: &Self, x: u32) -> bool {
141 fn is_empty(&self) -> bool;
144 pub trait InheritingEmpty: Empty {
145 //must not trigger LEN_WITHOUT_IS_EMPTY
146 fn len(&self) -> isize;
152 println!("This should not happen!");
159 //no error because One does not have .is_empty()
160 println!("This should not happen either!");
163 let z: &TraitsToo = &y;
165 //no error, because TraitsToo has no .is_empty() method
166 println!("Nor should this!");
169 let has_is_empty = HasIsEmpty;
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() > 0 {
177 println!("Or this!");
179 if has_is_empty.len() < 1 {
180 println!("Or this!");
182 if has_is_empty.len() >= 1 {
183 println!("Or this!");
185 if has_is_empty.len() > 1 {
187 println!("This can happen.");
189 if has_is_empty.len() <= 1 {
191 println!("This can happen.");
193 if 0 == has_is_empty.len() {
194 println!("Or this!");
196 if 0 != has_is_empty.len() {
197 println!("Or this!");
199 if 0 < 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() {
206 println!("Or this!");
208 if 1 < has_is_empty.len() {
210 println!("This can happen.");
212 if 1 >= has_is_empty.len() {
214 println!("This can happen.");
216 assert!(!has_is_empty.is_empty());
218 let with_is_empty: &WithIsEmpty = &Wither;
219 if with_is_empty.len() == 0 {
220 println!("Or this!");
222 assert!(!with_is_empty.is_empty());
224 let has_wrong_is_empty = HasWrongIsEmpty;
225 if has_wrong_is_empty.len() == 0 {
226 //no error as HasWrongIsEmpty does not have .is_empty()
227 println!("Or this!");
231 fn test_slice(b: &[u8]) {
236 pub trait Foo: Sized {}
238 pub trait DependsOnFoo: Foo {
239 fn len(&mut self) -> usize;