1 #![warn(clippy::while_let_on_iterator)]
2 #![allow(dead_code, unreachable_code, unused_mut)]
4 clippy::equatable_if_let,
7 clippy::redundant_closure_call,
8 clippy::single_range_in_vec_init,
9 clippy::uninlined_format_args,
28 while let None = iter.next() {} // this is fine (if nonsensical)
31 if let Some(x) = iter.next() {
36 // the following shouldn't warn because it can't be written with a for loop
37 let mut iter = 1u32..20;
38 while let Some(_) = iter.next() {
39 println!("next: {:?}", iter.next())
43 let mut iter = 1u32..20;
44 while let Some(_) = iter.next() {
45 println!("next: {:?}", iter.next());
49 let mut iter = 1u32..20;
50 while let Some(_) = iter.next() {
60 // consume all the 42s
61 while let Some(&42) = b.next() {}
66 while let Some(&(1, 2, 3)) = b.next() {}
71 while let Some(&None) = b.next() {}
73 /* This gives “refutable pattern in `for` loop binding: `&_` not covered”
75 for &(1, 2, 3) in b {}
76 for &Option::None in b.next() {}
83 let v = vec![1, 2, 3];
84 let mut it = v.windows(2);
85 while let Some([x, y]) = it.next() {
90 let mut it = v.windows(2);
91 while let Some([x, ..]) = it.next() {
95 let mut it = v.windows(2);
96 while let Some([.., y]) = it.next() {
100 let mut it = v.windows(2);
103 let v = vec![[1], [2], [3]];
104 let mut it = v.iter();
105 while let Some([1]) = it.next() {}
107 let mut it = v.iter();
113 let v = vec![1, 2, 3];
114 let mut it = v.iter();
115 while let Some(x @ 1) = it.next() {
119 let v = vec![[1], [2], [3]];
120 let mut it = v.iter();
128 let v = vec![1, 2, 3];
129 let mut it = v.iter().map(Some);
130 while let Some(Some(_) | None) = it.next() {
140 let mut y = a.iter();
142 // use a for loop here
148 use std::collections::HashSet;
149 let mut values = HashSet::new();
152 while let Some(&value) = values.iter().next() {
153 values.remove(&value);
158 // This should not cause an ICE
160 use std::collections::HashSet;
161 let mut values = HashSet::new();
164 while let Some(..) = values.iter().next() {}
168 let array = [Some(0), None, Some(1)];
169 let mut iter = array.iter();
171 while let Some(elem) = iter.next() {
172 let _ = elem.or_else(|| *iter.next()?);
177 // should not lint if the iterator is generated on every iteration
178 use std::collections::HashSet;
179 let mut values = HashSet::new();
182 while let Some(..) = values.iter().next() {
186 while let Some(..) = values.iter().map(|x| x + 1).next() {}
188 let chars = "Hello, World!".char_indices();
189 while let Some((i, ch)) = chars.clone().next() {
190 println!("{}: {}", i, ch);
195 // Used in outer loop, needs &mut
197 while let Some(n) = it.next() {
198 for m in it.by_ref() {
202 println!("doing something with m: {}", m);
204 println!("n still is {}", n);
207 // This is fine, inner loop uses a new iterator.
215 println!("doing something with m: {}", m);
218 // Weird binding shouldn't change anything.
219 let (mut it, _) = (1..40, 0);
224 println!("doing something with m: {}", m);
227 // Used after the loop, needs &mut.
229 for m in it.by_ref() {
233 println!("doing something with m: {}", m);
235 println!("next item {}", it.next().unwrap());
237 println!("n still is {}", n);
242 // Closure in the outer loop, needs &mut
244 let mut opt = Some(0);
245 while let Some(n) = opt.take().or_else(|| it.next()) {
246 for m in it.by_ref() {
250 println!("doing something with m: {}", m);
252 println!("n still is {}", n);
258 impl<T: Iterator<Item = u32>> S<T> {
259 fn f(&mut self) -> Option<u32> {
261 for i in self.0.by_ref() {
262 if !(3..8).contains(&i) {
269 fn f2(&mut self) -> Option<u32> {
270 // Don't lint, self borrowed inside the loop
271 while let Some(i) = self.0.next() {
279 impl<T: Iterator<Item = u32>> S<(S<T>, Option<u32>)> {
280 fn f3(&mut self) -> Option<u32> {
281 // Don't lint, self borrowed inside the loop
282 while let Some(i) = self.0.0.0.next() {
287 while let Some(i) = self.0.0.0.next() {
292 // This one is fine, a different field is borrowed
293 for i in self.0.0.0.by_ref() {
295 return self.0.1.take();
304 struct S2<T>(T, u32);
305 impl<T: Iterator<Item = u32>> Iterator for S2<T> {
307 fn next(&mut self) -> Option<u32> {
312 // Don't lint, field of the iterator is accessed in the loop
313 let mut it = S2(1..40, 0);
314 while let Some(n) = it.next() {
320 // Needs &mut, field of the iterator is accessed after the loop
321 let mut it = S2(1..40, 0);
322 for n in it.by_ref() {
327 println!("iterator field {}", it.1);
333 // Needs &mut, the closure can be called multiple times
334 for x in it.by_ref() {
347 // Needs to reborrow `it` as the binding isn't mutable
348 for x in it.by_ref() {
353 println!("{}", it.next().unwrap());
358 // Needs to reborrow `it.0` as the binding isn't mutable
359 for x in it.0.by_ref() {
364 println!("{}", it.0.next().unwrap());
367 fn exact_match_with_single_field() {
369 let mut s = S(0..10);
370 // Don't lint. `s.0` is used inside the loop.
371 while let Some(_) = s.0.next() {
381 impl<T> core::ops::Deref for S2<T> {
383 fn deref(&self) -> &Self::Target {
387 impl<T> core::ops::DerefMut for S2<T> {
388 fn deref_mut(&mut self) -> &mut Self::Target {
393 let mut s = S2(S1 { x: 0..10 });
394 for x in s.x.by_ref() {
401 for x in x[0].by_ref() {
406 fn fn_once_closure() {
416 fn f(_: impl FnOnce()) {}
426 fn f2(_: impl FnMut()) {}
429 for x in it.by_ref() {