3 #![warn(clippy::while_let_on_iterator)]
9 clippy::equatable_if_let,
11 clippy::redundant_closure_call
16 while let Option::Some(x) = iter.next() {
21 while let Some(x) = iter.next() {
26 while let Some(_) = iter.next() {}
29 while let None = iter.next() {} // this is fine (if nonsensical)
32 if let Some(x) = iter.next() {
37 // the following shouldn't warn because it can't be written with a for loop
38 let mut iter = 1u32..20;
39 while let Some(_) = iter.next() {
40 println!("next: {:?}", iter.next())
44 let mut iter = 1u32..20;
45 while let Some(_) = iter.next() {
46 println!("next: {:?}", iter.next());
50 let mut iter = 1u32..20;
51 while let Some(_) = iter.next() {
61 // consume all the 42s
62 while let Some(&42) = b.next() {}
67 while let Some(&(1, 2, 3)) = b.next() {}
72 while let Some(&None) = b.next() {}
74 /* This gives “refutable pattern in `for` loop binding: `&_` not covered”
76 for &(1, 2, 3) in b {}
77 for &Option::None in b.next() {}
84 let v = vec![1, 2, 3];
85 let mut it = v.windows(2);
86 while let Some([x, y]) = it.next() {
91 let mut it = v.windows(2);
92 while let Some([x, ..]) = it.next() {
96 let mut it = v.windows(2);
97 while let Some([.., y]) = it.next() {
101 let mut it = v.windows(2);
102 while let Some([..]) = it.next() {}
104 let v = vec![[1], [2], [3]];
105 let mut it = v.iter();
106 while let Some([1]) = it.next() {}
108 let mut it = v.iter();
109 while let Some([_x]) = it.next() {}
114 let v = vec![1, 2, 3];
115 let mut it = v.iter();
116 while let Some(x @ 1) = it.next() {
120 let v = vec![[1], [2], [3]];
121 let mut it = v.iter();
122 while let Some(x @ [_]) = it.next() {
129 let v = vec![1, 2, 3];
130 let mut it = v.iter().map(Some);
131 while let Some(Some(_) | None) = it.next() {
141 let mut y = a.iter();
142 while let Some(_) = y.next() {
143 // use a for loop here
149 use std::collections::HashSet;
150 let mut values = HashSet::new();
153 while let Some(&value) = values.iter().next() {
154 values.remove(&value);
159 // This should not cause an ICE
161 use std::collections::HashSet;
162 let mut values = HashSet::new();
165 while let Some(..) = values.iter().next() {}
169 let array = [Some(0), None, Some(1)];
170 let mut iter = array.iter();
172 while let Some(elem) = iter.next() {
173 let _ = elem.or_else(|| *iter.next()?);
178 // should not lint if the iterator is generated on every iteration
179 use std::collections::HashSet;
180 let mut values = HashSet::new();
183 while let Some(..) = values.iter().next() {
187 while let Some(..) = values.iter().map(|x| x + 1).next() {}
189 let chars = "Hello, World!".char_indices();
190 while let Some((i, ch)) = chars.clone().next() {
191 println!("{}: {}", i, ch);
196 // Used in outer loop, needs &mut
198 while let Some(n) = it.next() {
199 while let Some(m) = it.next() {
203 println!("doing something with m: {}", m);
205 println!("n still is {}", n);
208 // This is fine, inner loop uses a new iterator.
210 while let Some(n) = it.next() {
212 while let Some(m) = it.next() {
216 println!("doing something with m: {}", m);
219 // Weird binding shouldn't change anything.
220 let (mut it, _) = (1..40, 0);
221 while let Some(m) = it.next() {
225 println!("doing something with m: {}", m);
228 // Used after the loop, needs &mut.
230 while let Some(m) = it.next() {
234 println!("doing something with m: {}", m);
236 println!("next item {}", it.next().unwrap());
238 println!("n still is {}", n);
243 // Closure in the outer loop, needs &mut
245 let mut opt = Some(0);
246 while let Some(n) = opt.take().or_else(|| it.next()) {
247 while let Some(m) = it.next() {
251 println!("doing something with m: {}", m);
253 println!("n still is {}", n);
259 impl<T: Iterator<Item = u32>> S<T> {
260 fn f(&mut self) -> Option<u32> {
262 while let Some(i) = self.0.next() {
263 if !(3..8).contains(&i) {
270 fn f2(&mut self) -> Option<u32> {
271 // Don't lint, self borrowed inside the loop
272 while let Some(i) = self.0.next() {
280 impl<T: Iterator<Item = u32>> S<(S<T>, Option<u32>)> {
281 fn f3(&mut self) -> Option<u32> {
282 // Don't lint, self borrowed inside the loop
283 while let Some(i) = self.0.0.0.next() {
288 while let Some(i) = self.0.0.0.next() {
293 // This one is fine, a different field is borrowed
294 while let Some(i) = self.0.0.0.next() {
296 return self.0.1.take();
305 struct S2<T>(T, u32);
306 impl<T: Iterator<Item = u32>> Iterator for S2<T> {
308 fn next(&mut self) -> Option<u32> {
313 // Don't lint, field of the iterator is accessed in the loop
314 let mut it = S2(1..40, 0);
315 while let Some(n) = it.next() {
321 // Needs &mut, field of the iterator is accessed after the loop
322 let mut it = S2(1..40, 0);
323 while let Some(n) = it.next() {
328 println!("iterator field {}", it.1);
334 // Needs &mut, the closure can be called multiple times
335 while let Some(x) = it.next() {
348 // Needs to reborrow `it` as the binding isn't mutable
349 while let Some(x) = it.next() {
354 println!("{}", it.next().unwrap());
359 // Needs to reborrow `it.0` as the binding isn't mutable
360 while let Some(x) = it.0.next() {
365 println!("{}", it.0.next().unwrap());
368 fn exact_match_with_single_field() {
370 let mut s = S(0..10);
371 // Don't lint. `s.0` is used inside the loop.
372 while let Some(_) = s.0.next() {
382 impl<T> core::ops::Deref for S2<T> {
384 fn deref(&self) -> &Self::Target {
388 impl<T> core::ops::DerefMut for S2<T> {
389 fn deref_mut(&mut self) -> &mut Self::Target {
394 let mut s = S2(S1 { x: 0..10 });
395 while let Some(x) = s.x.next() {
402 while let Some(x) = x[0].next() {
407 fn fn_once_closure() {
410 while let Some(x) = it.next() {
417 fn f(_: impl FnOnce()) {}
420 while let Some(x) = it.next() {
427 fn f2(_: impl FnMut()) {}
430 while let Some(x) = it.next() {
440 while let Some(x) = it.next() {
450 while let Some(..) = it.next() {