rust-clippy/tests/ui/len_zero.rs

229 lines
4.7 KiB
Rust
Raw Normal View History

#![warn(clippy::len_zero)]
#![allow(
dead_code,
unused,
clippy::needless_if,
clippy::len_without_is_empty,
clippy::const_is_empty
)]
2022-11-26 06:17:49 +00:00
extern crate core;
use core::ops::Deref;
pub struct One;
struct Wither;
trait TraitsToo {
2020-08-05 01:37:29 +00:00
fn len(&self) -> isize;
2019-01-31 01:15:29 +00:00
// No error; `len` is private; see issue #1085.
}
impl TraitsToo for One {
2020-08-05 01:37:29 +00:00
fn len(&self) -> isize {
0
}
}
pub struct HasIsEmpty;
impl HasIsEmpty {
2020-08-05 01:37:29 +00:00
pub fn len(&self) -> isize {
1
}
2020-08-05 01:37:29 +00:00
fn is_empty(&self) -> bool {
false
}
}
pub struct HasWrongIsEmpty;
impl HasWrongIsEmpty {
2020-08-05 01:37:29 +00:00
pub fn len(&self) -> isize {
1
}
2020-08-05 01:37:29 +00:00
pub fn is_empty(&self, x: u32) -> bool {
false
}
}
pub trait WithIsEmpty {
2020-08-05 01:37:29 +00:00
fn len(&self) -> isize;
fn is_empty(&self) -> bool;
}
impl WithIsEmpty for Wither {
2020-08-05 01:37:29 +00:00
fn len(&self) -> isize {
1
}
2020-08-05 01:37:29 +00:00
fn is_empty(&self) -> bool {
false
}
}
2022-11-26 06:17:49 +00:00
struct DerefToDerefToString;
impl Deref for DerefToDerefToString {
type Target = DerefToString;
fn deref(&self) -> &Self::Target {
&DerefToString {}
}
}
struct DerefToString;
impl Deref for DerefToString {
type Target = str;
fn deref(&self) -> &Self::Target {
"Hello, world!"
}
}
fn main() {
let x = [1, 2];
2016-02-24 19:52:47 +00:00
if x.len() == 0 {
println!("This should not happen!");
}
if "".len() == 0 {}
2022-11-26 06:17:49 +00:00
let s = "Hello, world!";
let s1 = &s;
let s2 = &s1;
let s3 = &s2;
let s4 = &s3;
let s5 = &s4;
let s6 = &s5;
println!("{}", *s1 == "");
println!("{}", **s2 == "");
println!("{}", ***s3 == "");
println!("{}", ****s4 == "");
println!("{}", *****s5 == "");
println!("{}", ******(s6) == "");
let d2s = DerefToDerefToString {};
println!("{}", &**d2s == "");
let y = One;
if y.len() == 0 {
2019-01-31 01:15:29 +00:00
// No error; `One` does not have `.is_empty()`.
println!("This should not happen either!");
}
let z: &dyn TraitsToo = &y;
if z.len() > 0 {
2019-01-31 01:15:29 +00:00
// No error; `TraitsToo` has no `.is_empty()` method.
println!("Nor should this!");
}
let has_is_empty = HasIsEmpty;
if has_is_empty.len() == 0 {
println!("Or this!");
}
if has_is_empty.len() != 0 {
println!("Or this!");
}
if has_is_empty.len() > 0 {
println!("Or this!");
}
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.
println!("This can happen.");
}
if has_is_empty.len() <= 1 {
2019-01-31 01:15:29 +00:00
// No error.
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.
println!("This can happen.");
}
if 1 >= has_is_empty.len() {
2019-01-31 01:15:29 +00:00
// No error.
println!("This can happen.");
}
assert!(!has_is_empty.is_empty());
let with_is_empty: &dyn WithIsEmpty = &Wither;
if with_is_empty.len() == 0 {
println!("Or this!");
}
assert!(!with_is_empty.is_empty());
let has_wrong_is_empty = HasWrongIsEmpty;
if has_wrong_is_empty.len() == 0 {
2019-01-31 01:15:29 +00:00
// No error; `HasWrongIsEmpty` does not have `.is_empty()`.
println!("Or this!");
}
// issue #10529
(has_is_empty.len() > 0).then(|| println!("This can happen."));
(has_is_empty.len() == 0).then(|| println!("Or this!"));
}
2017-02-25 03:26:33 +00:00
fn test_slice(b: &[u8]) {
if b.len() != 0 {}
2017-02-25 03:26:33 +00:00
}
// issue #11992
fn binop_with_macros() {
macro_rules! len {
($seq:ident) => {
$seq.len()
};
}
macro_rules! compare_to {
($val:literal) => {
$val
};
($val:expr) => {{ $val }};
}
macro_rules! zero {
() => {
0
};
}
let has_is_empty = HasIsEmpty;
// Don't lint, suggesting changes might break macro compatibility.
(len!(has_is_empty) > 0).then(|| println!("This can happen."));
// Don't lint, suggesting changes might break macro compatibility.
if len!(has_is_empty) == 0 {}
// Don't lint
if has_is_empty.len() == compare_to!(if true { 0 } else { 1 }) {}
// This is fine
if has_is_empty.len() == compare_to!(1) {}
if has_is_empty.len() == compare_to!(0) {}
if has_is_empty.len() == zero!() {}
(compare_to!(0) < has_is_empty.len()).then(|| println!("This can happen."));
}