fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1#![deny(clippy::useless_conversion)] 2#![allow(clippy::needless_if, clippy::unnecessary_wraps)] 3 4fn test_generic<T: Copy>(val: T) -> T { 5 let _ = val; 6 val 7} 8 9fn test_generic2<T: Copy + Into<i32> + Into<U>, U: From<T>>(val: T) { 10 // ok 11 let _: i32 = val.into(); 12 let _: U = val.into(); 13 let _ = U::from(val); 14} 15 16fn test_questionmark() -> Result<(), ()> { 17 { 18 let _: i32 = 0i32; 19 Ok(Ok(())) 20 }??; 21 Ok(()) 22} 23 24fn test_issue_3913() -> Result<(), std::io::Error> { 25 use std::fs; 26 use std::path::Path; 27 28 let path = Path::new("."); 29 for _ in fs::read_dir(path)? {} 30 31 Ok(()) 32} 33 34fn dont_lint_on_type_alias() { 35 type A = i32; 36 _ = A::from(0i32); 37} 38 39fn dont_lint_into_iter_on_immutable_local_implementing_iterator_in_expr() { 40 let text = "foo\r\nbar\n\nbaz\n"; 41 let lines = text.lines(); 42 if Some("ok") == lines.into_iter().next() {} 43} 44 45fn lint_into_iter_on_mutable_local_implementing_iterator_in_expr() { 46 let text = "foo\r\nbar\n\nbaz\n"; 47 let mut lines = text.lines(); 48 if Some("ok") == lines.next() {} 49} 50 51fn lint_into_iter_on_expr_implementing_iterator() { 52 let text = "foo\r\nbar\n\nbaz\n"; 53 let mut lines = text.lines(); 54 if Some("ok") == lines.next() {} 55} 56 57fn lint_into_iter_on_expr_implementing_iterator_2() { 58 let text = "foo\r\nbar\n\nbaz\n"; 59 if Some("ok") == text.lines().next() {} 60} 61 62#[allow(const_item_mutation)] 63fn lint_into_iter_on_const_implementing_iterator() { 64 const NUMBERS: std::ops::Range<i32> = 0..10; 65 let _ = NUMBERS.next(); 66} 67 68fn lint_into_iter_on_const_implementing_iterator_2() { 69 const NUMBERS: std::ops::Range<i32> = 0..10; 70 let mut n = NUMBERS; 71 n.next(); 72} 73 74#[derive(Clone, Copy)] 75struct CopiableCounter { 76 counter: u32, 77} 78 79impl Iterator for CopiableCounter { 80 type Item = u32; 81 82 fn next(&mut self) -> Option<Self::Item> { 83 self.counter = self.counter.wrapping_add(1); 84 Some(self.counter) 85 } 86} 87 88fn dont_lint_into_iter_on_copy_iter() { 89 let mut c = CopiableCounter { counter: 0 }; 90 assert_eq!(c.into_iter().next(), Some(1)); 91 assert_eq!(c.into_iter().next(), Some(1)); 92 assert_eq!(c.next(), Some(1)); 93 assert_eq!(c.next(), Some(2)); 94} 95 96fn dont_lint_into_iter_on_static_copy_iter() { 97 static mut C: CopiableCounter = CopiableCounter { counter: 0 }; 98 unsafe { 99 assert_eq!(C.into_iter().next(), Some(1)); 100 assert_eq!(C.into_iter().next(), Some(1)); 101 assert_eq!(C.next(), Some(1)); 102 assert_eq!(C.next(), Some(2)); 103 } 104} 105 106fn main() { 107 test_generic(10i32); 108 test_generic2::<i32, i32>(10i32); 109 test_questionmark().unwrap(); 110 test_issue_3913().unwrap(); 111 112 dont_lint_on_type_alias(); 113 dont_lint_into_iter_on_immutable_local_implementing_iterator_in_expr(); 114 lint_into_iter_on_mutable_local_implementing_iterator_in_expr(); 115 lint_into_iter_on_expr_implementing_iterator(); 116 lint_into_iter_on_expr_implementing_iterator_2(); 117 lint_into_iter_on_const_implementing_iterator(); 118 lint_into_iter_on_const_implementing_iterator_2(); 119 dont_lint_into_iter_on_copy_iter(); 120 dont_lint_into_iter_on_static_copy_iter(); 121 122 let _: String = "foo".into(); 123 let _: String = From::from("foo"); 124 let _ = String::from("foo"); 125 #[allow(clippy::useless_conversion)] 126 { 127 let _: String = "foo".into(); 128 let _ = String::from("foo"); 129 let _ = "".lines().into_iter(); 130 } 131 132 let _: String = "foo".to_string(); 133 let _: String = "foo".to_string(); 134 let _ = "foo".to_string(); 135 let _ = format!("A: {:04}", 123); 136 let _ = "".lines(); 137 let _ = vec![1, 2, 3].into_iter(); 138 let _: String = format!("Hello {}", "world"); 139 140 // keep parentheses around `a + b` for suggestion (see #4750) 141 let a: i32 = 1; 142 let b: i32 = 1; 143 let _ = (a + b) * 3; 144 145 // see #7205 146 let s: Foo<'a'> = Foo; 147 let _: Foo<'b'> = s.into(); 148 let s2: Foo<'a'> = Foo; 149 let _: Foo<'a'> = s2; 150 let s3: Foo<'a'> = Foo; 151 let _ = s3; 152 let s4: Foo<'a'> = Foo; 153 let _ = vec![s4, s4, s4].into_iter(); 154 155 issue11300::bar(); 156} 157 158#[allow(dead_code)] 159fn issue11065_fp() { 160 use std::option::IntoIter; 161 fn takes_into_iter(_: impl IntoIterator<Item = i32>) {} 162 163 macro_rules! x { 164 ($e:expr) => { 165 takes_into_iter($e); 166 let _: IntoIter<i32> = $e; // removing `.into_iter()` leads to a type error here 167 }; 168 } 169 x!(Some(5).into_iter()); 170} 171 172#[allow(dead_code)] 173fn explicit_into_iter_fn_arg() { 174 fn a<T>(_: T) {} 175 fn b<T: IntoIterator<Item = i32>>(_: T) {} 176 fn c(_: impl IntoIterator<Item = i32>) {} 177 fn d<T>(_: T) 178 where 179 T: IntoIterator<Item = i32>, 180 { 181 } 182 fn f(_: std::vec::IntoIter<i32>) {} 183 184 a(vec![1, 2].into_iter()); 185 b(vec![1, 2]); 186 c(vec![1, 2]); 187 d(vec![1, 2]); 188 b([&1, &2, &3].into_iter().cloned()); 189 190 b(vec![1, 2]); 191 b(vec![1, 2]); 192 193 macro_rules! macro_generated { 194 () => { 195 vec![1, 2].into_iter() 196 }; 197 } 198 b(macro_generated!()); 199} 200 201mod issue11300 { 202 pub fn foo<I>(i: I) 203 where 204 I: IntoIterator<Item = i32> + ExactSizeIterator, 205 { 206 assert_eq!(i.len(), 3); 207 } 208 209 trait Helper<T: ?Sized> {} 210 impl Helper<i32> for [i32; 3] {} 211 impl Helper<i32> for std::array::IntoIter<i32, 3> {} 212 impl Helper<()> for std::array::IntoIter<i32, 3> {} 213 214 fn foo2<X: ?Sized, I>(_: I) 215 where 216 I: IntoIterator<Item = i32> + Helper<X>, 217 { 218 } 219 220 trait Helper2<T> {} 221 impl Helper2<std::array::IntoIter<i32, 3>> for i32 {} 222 impl Helper2<[i32; 3]> for i32 {} 223 fn foo3<I>(_: I) 224 where 225 I: IntoIterator<Item = i32>, 226 i32: Helper2<I>, 227 { 228 } 229 230 pub fn bar() { 231 // This should not trigger the lint: 232 // `[i32, 3]` does not satisfy the `ExactSizeIterator` bound, so the into_iter call cannot be 233 // removed and is not useless. 234 foo([1, 2, 3].into_iter()); 235 236 // This should trigger the lint, receiver type [i32; 3] also implements `Helper` 237 foo2::<i32, _>([1, 2, 3]); 238 239 // This again should *not* lint, since X = () and I = std::array::IntoIter<i32, 3>, 240 // and `[i32; 3]: Helper<()>` is not true (only `std::array::IntoIter<i32, 3>: Helper<()>` is). 241 foo2::<(), _>([1, 2, 3].into_iter()); 242 243 // This should lint. Removing the `.into_iter()` means that `I` gets substituted with `[i32; 3]`, 244 // and `i32: Helper2<[i32, 3]>` is true, so this call is indeed unncessary. 245 foo3([1, 2, 3]); 246 } 247 248 fn ice() { 249 struct S1; 250 impl S1 { 251 pub fn foo<I: IntoIterator>(&self, _: I) {} 252 } 253 254 S1.foo([1, 2]); 255 256 // ICE that occured in itertools 257 trait Itertools { 258 fn interleave_shortest<J>(self, other: J) 259 where 260 J: IntoIterator, 261 Self: Sized; 262 } 263 impl<I: Iterator> Itertools for I { 264 fn interleave_shortest<J>(self, other: J) 265 where 266 J: IntoIterator, 267 Self: Sized, 268 { 269 } 270 } 271 let v0: Vec<i32> = vec![0, 2, 4]; 272 let v1: Vec<i32> = vec![1, 3, 5, 7]; 273 v0.into_iter().interleave_shortest(v1); 274 275 trait TraitWithLifetime<'a> {} 276 impl<'a> TraitWithLifetime<'a> for std::array::IntoIter<&'a i32, 2> {} 277 278 struct Helper; 279 impl<'a> Helper { 280 fn with_lt<I>(&self, _: I) 281 where 282 I: IntoIterator + TraitWithLifetime<'a>, 283 { 284 } 285 } 286 Helper.with_lt([&1, &2].into_iter()); 287 } 288} 289 290#[derive(Copy, Clone)] 291struct Foo<const C: char>; 292 293impl From<Foo<'a'>> for Foo<'b'> { 294 fn from(_s: Foo<'a'>) -> Self { 295 Foo 296 } 297}