2019-04-16 19:46:07 +00:00
|
|
|
// run-rustfix
|
2016-08-29 21:06:59 +00:00
|
|
|
|
2019-04-16 19:46:07 +00:00
|
|
|
#![warn(clippy::len_zero)]
|
|
|
|
#![allow(dead_code, unused, clippy::len_without_is_empty)]
|
2016-08-29 21:06:59 +00:00
|
|
|
|
2019-04-16 19:46:07 +00:00
|
|
|
pub struct One;
|
|
|
|
struct Wither;
|
2016-08-29 21:06:59 +00:00
|
|
|
|
2015-05-20 06:52:19 +00:00
|
|
|
trait TraitsToo {
|
2019-01-31 01:15:29 +00:00
|
|
|
fn len(self: &Self) -> isize;
|
|
|
|
// No error; `len` is private; see issue #1085.
|
2015-05-20 06:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TraitsToo for One {
|
2015-08-11 18:22:20 +00:00
|
|
|
fn len(self: &Self) -> isize {
|
|
|
|
0
|
|
|
|
}
|
2015-05-20 06:52:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 19:46:07 +00:00
|
|
|
pub struct HasIsEmpty;
|
2016-08-29 21:06:59 +00:00
|
|
|
|
2019-04-16 19:46:07 +00:00
|
|
|
impl HasIsEmpty {
|
2016-08-29 21:06:59 +00:00
|
|
|
pub fn len(self: &Self) -> isize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_empty(self: &Self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 19:46:07 +00:00
|
|
|
pub struct HasWrongIsEmpty;
|
2015-05-20 06:52:19 +00:00
|
|
|
|
2019-04-16 19:46:07 +00:00
|
|
|
impl HasWrongIsEmpty {
|
2017-02-08 13:58:07 +00:00
|
|
|
pub fn len(self: &Self) -> isize {
|
2015-08-11 18:22:20 +00:00
|
|
|
1
|
|
|
|
}
|
2015-06-01 10:49:36 +00:00
|
|
|
|
2019-04-16 19:46:07 +00:00
|
|
|
pub fn is_empty(self: &Self, x: u32) -> bool {
|
2015-08-11 18:22:20 +00:00
|
|
|
false
|
|
|
|
}
|
2015-06-01 10:49:36 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 21:06:59 +00:00
|
|
|
pub trait WithIsEmpty {
|
2015-08-11 18:22:20 +00:00
|
|
|
fn len(self: &Self) -> isize;
|
|
|
|
fn is_empty(self: &Self) -> bool;
|
2015-06-01 10:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WithIsEmpty for Wither {
|
2015-08-11 18:22:20 +00:00
|
|
|
fn len(self: &Self) -> isize {
|
|
|
|
1
|
|
|
|
}
|
2015-06-01 10:49:36 +00:00
|
|
|
|
2015-08-11 18:22:20 +00:00
|
|
|
fn is_empty(self: &Self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
2015-06-01 10:49:36 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 06:52:19 +00:00
|
|
|
fn main() {
|
2015-08-11 18:22:20 +00:00
|
|
|
let x = [1, 2];
|
2016-02-24 19:52:47 +00:00
|
|
|
if x.len() == 0 {
|
2015-08-11 18:22:20 +00:00
|
|
|
println!("This should not happen!");
|
|
|
|
}
|
|
|
|
|
2018-05-05 13:01:51 +00:00
|
|
|
if "".len() == 0 {}
|
2016-03-12 20:12:35 +00:00
|
|
|
|
2015-08-11 18:22:20 +00:00
|
|
|
let y = One;
|
2018-05-05 13:01:51 +00:00
|
|
|
if y.len() == 0 {
|
2019-01-31 01:15:29 +00:00
|
|
|
// No error; `One` does not have `.is_empty()`.
|
2015-08-11 18:22:20 +00:00
|
|
|
println!("This should not happen either!");
|
|
|
|
}
|
|
|
|
|
2019-05-30 06:23:47 +00:00
|
|
|
let z: &dyn TraitsToo = &y;
|
2018-05-05 13:01:51 +00:00
|
|
|
if z.len() > 0 {
|
2019-01-31 01:15:29 +00:00
|
|
|
// No error; `TraitsToo` has no `.is_empty()` method.
|
2015-08-11 18:22:20 +00:00
|
|
|
println!("Nor should this!");
|
|
|
|
}
|
|
|
|
|
2016-03-01 09:13:54 +00:00
|
|
|
let has_is_empty = HasIsEmpty;
|
|
|
|
if has_is_empty.len() == 0 {
|
2015-08-11 18:22:20 +00:00
|
|
|
println!("Or this!");
|
|
|
|
}
|
2016-03-01 09:13:54 +00:00
|
|
|
if has_is_empty.len() != 0 {
|
2015-08-12 08:53:14 +00:00
|
|
|
println!("Or this!");
|
|
|
|
}
|
2016-03-01 09:13:54 +00:00
|
|
|
if has_is_empty.len() > 0 {
|
2015-08-12 08:53:14 +00:00
|
|
|
println!("Or this!");
|
|
|
|
}
|
2018-05-05 13:01:51 +00:00
|
|
|
if has_is_empty.len() < 1 {
|
|
|
|
println!("Or this!");
|
|
|
|
}
|
|
|
|
if has_is_empty.len() >= 1 {
|
|
|
|
println!("Or this!");
|
|
|
|
}
|
|
|
|
if has_is_empty.len() > 1 {
|
2019-01-31 01:15:29 +00:00
|
|
|
// No error.
|
2018-05-05 13:01:51 +00:00
|
|
|
println!("This can happen.");
|
|
|
|
}
|
|
|
|
if has_is_empty.len() <= 1 {
|
2019-01-31 01:15:29 +00:00
|
|
|
// No error.
|
2018-05-05 13:01:51 +00:00
|
|
|
println!("This can happen.");
|
|
|
|
}
|
|
|
|
if 0 == has_is_empty.len() {
|
|
|
|
println!("Or this!");
|
|
|
|
}
|
|
|
|
if 0 != has_is_empty.len() {
|
|
|
|
println!("Or this!");
|
|
|
|
}
|
|
|
|
if 0 < has_is_empty.len() {
|
|
|
|
println!("Or this!");
|
|
|
|
}
|
|
|
|
if 1 <= has_is_empty.len() {
|
|
|
|
println!("Or this!");
|
|
|
|
}
|
|
|
|
if 1 > has_is_empty.len() {
|
|
|
|
println!("Or this!");
|
|
|
|
}
|
|
|
|
if 1 < has_is_empty.len() {
|
2019-01-31 01:15:29 +00:00
|
|
|
// No error.
|
2018-05-05 13:01:51 +00:00
|
|
|
println!("This can happen.");
|
|
|
|
}
|
|
|
|
if 1 >= has_is_empty.len() {
|
2019-01-31 01:15:29 +00:00
|
|
|
// No error.
|
2018-05-05 13:01:51 +00:00
|
|
|
println!("This can happen.");
|
|
|
|
}
|
2016-03-01 09:13:54 +00:00
|
|
|
assert!(!has_is_empty.is_empty());
|
2015-08-11 18:22:20 +00:00
|
|
|
|
2019-05-30 06:23:47 +00:00
|
|
|
let with_is_empty: &dyn WithIsEmpty = &Wither;
|
2016-03-01 09:13:54 +00:00
|
|
|
if with_is_empty.len() == 0 {
|
2015-08-11 18:22:20 +00:00
|
|
|
println!("Or this!");
|
|
|
|
}
|
2016-03-01 09:13:54 +00:00
|
|
|
assert!(!with_is_empty.is_empty());
|
2015-08-11 18:22:20 +00:00
|
|
|
|
2016-03-01 09:13:54 +00:00
|
|
|
let has_wrong_is_empty = HasWrongIsEmpty;
|
2018-05-05 13:01:51 +00:00
|
|
|
if has_wrong_is_empty.len() == 0 {
|
2019-01-31 01:15:29 +00:00
|
|
|
// No error; `HasWrongIsEmpty` does not have `.is_empty()`.
|
2015-08-11 18:22:20 +00:00
|
|
|
println!("Or this!");
|
|
|
|
}
|
2015-05-20 06:52:19 +00:00
|
|
|
}
|
2017-02-25 03:26:33 +00:00
|
|
|
|
|
|
|
fn test_slice(b: &[u8]) {
|
2018-05-05 13:01:51 +00:00
|
|
|
if b.len() != 0 {}
|
2017-02-25 03:26:33 +00:00
|
|
|
}
|
2020-05-27 22:36:15 +00:00
|
|
|
|
|
|
|
mod issue_3807 {
|
|
|
|
// Avoid suggesting changes to ranges if the user did not enable `range_is_empty`.
|
|
|
|
// See https://github.com/rust-lang/rust/issues/48111#issuecomment-445132965
|
|
|
|
fn no_suggestion() {
|
|
|
|
let _ = (0..42).len() == 0;
|
|
|
|
}
|
|
|
|
}
|