fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1#![warn(clippy::len_zero)] 2#![allow(dead_code, unused, clippy::needless_if, clippy::len_without_is_empty)] 3 4extern crate core; 5use core::ops::Deref; 6 7pub struct One; 8struct Wither; 9 10trait TraitsToo { 11 fn len(&self) -> isize; 12 // No error; `len` is private; see issue #1085. 13} 14 15impl TraitsToo for One { 16 fn len(&self) -> isize { 17 0 18 } 19} 20 21pub struct HasIsEmpty; 22 23impl HasIsEmpty { 24 pub fn len(&self) -> isize { 25 1 26 } 27 28 fn is_empty(&self) -> bool { 29 false 30 } 31} 32 33pub struct HasWrongIsEmpty; 34 35impl HasWrongIsEmpty { 36 pub fn len(&self) -> isize { 37 1 38 } 39 40 pub fn is_empty(&self, x: u32) -> bool { 41 false 42 } 43} 44 45pub trait WithIsEmpty { 46 fn len(&self) -> isize; 47 fn is_empty(&self) -> bool; 48} 49 50impl WithIsEmpty for Wither { 51 fn len(&self) -> isize { 52 1 53 } 54 55 fn is_empty(&self) -> bool { 56 false 57 } 58} 59 60struct DerefToDerefToString; 61 62impl Deref for DerefToDerefToString { 63 type Target = DerefToString; 64 65 fn deref(&self) -> &Self::Target { 66 &DerefToString {} 67 } 68} 69 70struct DerefToString; 71 72impl Deref for DerefToString { 73 type Target = str; 74 75 fn deref(&self) -> &Self::Target { 76 "Hello, world!" 77 } 78} 79 80fn main() { 81 let x = [1, 2]; 82 if x.is_empty() { 83 println!("This should not happen!"); 84 } 85 86 if "".is_empty() {} 87 88 let s = "Hello, world!"; 89 let s1 = &s; 90 let s2 = &s1; 91 let s3 = &s2; 92 let s4 = &s3; 93 let s5 = &s4; 94 let s6 = &s5; 95 println!("{}", s1.is_empty()); 96 println!("{}", s2.is_empty()); 97 println!("{}", s3.is_empty()); 98 println!("{}", s4.is_empty()); 99 println!("{}", s5.is_empty()); 100 println!("{}", (s6).is_empty()); 101 102 let d2s = DerefToDerefToString {}; 103 println!("{}", (**d2s).is_empty()); 104 105 let y = One; 106 if y.len() == 0 { 107 // No error; `One` does not have `.is_empty()`. 108 println!("This should not happen either!"); 109 } 110 111 let z: &dyn TraitsToo = &y; 112 if z.len() > 0 { 113 // No error; `TraitsToo` has no `.is_empty()` method. 114 println!("Nor should this!"); 115 } 116 117 let has_is_empty = HasIsEmpty; 118 if has_is_empty.is_empty() { 119 println!("Or this!"); 120 } 121 if !has_is_empty.is_empty() { 122 println!("Or this!"); 123 } 124 if !has_is_empty.is_empty() { 125 println!("Or this!"); 126 } 127 if has_is_empty.is_empty() { 128 println!("Or this!"); 129 } 130 if !has_is_empty.is_empty() { 131 println!("Or this!"); 132 } 133 if has_is_empty.len() > 1 { 134 // No error. 135 println!("This can happen."); 136 } 137 if has_is_empty.len() <= 1 { 138 // No error. 139 println!("This can happen."); 140 } 141 if has_is_empty.is_empty() { 142 println!("Or this!"); 143 } 144 if !has_is_empty.is_empty() { 145 println!("Or this!"); 146 } 147 if !has_is_empty.is_empty() { 148 println!("Or this!"); 149 } 150 if !has_is_empty.is_empty() { 151 println!("Or this!"); 152 } 153 if has_is_empty.is_empty() { 154 println!("Or this!"); 155 } 156 if 1 < has_is_empty.len() { 157 // No error. 158 println!("This can happen."); 159 } 160 if 1 >= has_is_empty.len() { 161 // No error. 162 println!("This can happen."); 163 } 164 assert!(!has_is_empty.is_empty()); 165 166 let with_is_empty: &dyn WithIsEmpty = &Wither; 167 if with_is_empty.is_empty() { 168 println!("Or this!"); 169 } 170 assert!(!with_is_empty.is_empty()); 171 172 let has_wrong_is_empty = HasWrongIsEmpty; 173 if has_wrong_is_empty.len() == 0 { 174 // No error; `HasWrongIsEmpty` does not have `.is_empty()`. 175 println!("Or this!"); 176 } 177 178 // issue #10529 179 (!has_is_empty.is_empty()).then(|| println!("This can happen.")); 180 (has_is_empty.is_empty()).then(|| println!("Or this!")); 181} 182 183fn test_slice(b: &[u8]) { 184 if !b.is_empty() {} 185}