3 #![warn(clippy::while_let_on_iterator)]
4 #![allow(clippy::never_loop, unreachable_code, unused_mut, dead_code)]
21 while let None = iter.next() {} // this is fine (if nonsensical)
24 if let Some(x) = iter.next() {
29 // the following shouldn't warn because it can't be written with a for loop
30 let mut iter = 1u32..20;
31 while let Some(_) = iter.next() {
32 println!("next: {:?}", iter.next())
36 let mut iter = 1u32..20;
37 while let Some(_) = iter.next() {
38 println!("next: {:?}", iter.next());
42 let mut iter = 1u32..20;
43 while let Some(_) = iter.next() {
53 // consume all the 42s
54 while let Some(&42) = b.next() {}
59 while let Some(&(1, 2, 3)) = b.next() {}
64 while let Some(&None) = b.next() {}
66 /* This gives “refutable pattern in `for` loop binding: `&_` not covered”
68 for &(1, 2, 3) in b {}
69 for &Option::None in b.next() {}
76 let v = vec![1, 2, 3];
77 let mut it = v.windows(2);
78 while let Some([x, y]) = it.next() {
83 let mut it = v.windows(2);
84 while let Some([x, ..]) = it.next() {
88 let mut it = v.windows(2);
89 while let Some([.., y]) = it.next() {
93 let mut it = v.windows(2);
96 let v = vec![[1], [2], [3]];
97 let mut it = v.iter();
98 while let Some([1]) = it.next() {}
100 let mut it = v.iter();
106 let v = vec![1, 2, 3];
107 let mut it = v.iter();
108 while let Some(x @ 1) = it.next() {
112 let v = vec![[1], [2], [3]];
113 let mut it = v.iter();
121 let v = vec![1, 2, 3];
122 let mut it = v.iter().map(Some);
123 while let Some(Some(_) | None) = it.next() {
133 let mut y = a.iter();
135 // use a for loop here
141 use std::collections::HashSet;
142 let mut values = HashSet::new();
145 while let Some(&value) = values.iter().next() {
146 values.remove(&value);
151 // This should not cause an ICE
153 use std::collections::HashSet;
154 let mut values = HashSet::new();
157 while let Some(..) = values.iter().next() {}
161 let array = [Some(0), None, Some(1)];
162 let mut iter = array.iter();
164 while let Some(elem) = iter.next() {
165 let _ = elem.or_else(|| *iter.next()?);
170 // should not lint if the iterator is generated on every iteration
171 use std::collections::HashSet;
172 let mut values = HashSet::new();
175 while let Some(..) = values.iter().next() {
179 while let Some(..) = values.iter().map(|x| x + 1).next() {}
181 let chars = "Hello, World!".char_indices();
182 while let Some((i, ch)) = chars.clone().next() {
183 println!("{}: {}", i, ch);
188 // Used in outer loop, needs &mut
190 while let Some(n) = it.next() {
195 println!("doing something with m: {}", m);
197 println!("n still is {}", n);
200 // This is fine, inner loop uses a new iterator.
208 println!("doing something with m: {}", m);
211 // Weird binding shouldn't change anything.
212 let (mut it, _) = (1..40, 0);
217 println!("doing something with m: {}", m);
220 // Used after the loop, needs &mut.
226 println!("doing something with m: {}", m);
228 println!("next item {}", it.next().unwrap());
230 println!("n still is {}", n);
235 // Closure in the outer loop, needs &mut
237 let mut opt = Some(0);
238 while let Some(n) = opt.take().or_else(|| it.next()) {
243 println!("doing something with m: {}", m);
245 println!("n still is {}", n);
251 impl<T: Iterator<Item = u32>> S<T> {
252 fn f(&mut self) -> Option<u32> {
254 for i in &mut self.0 {
255 if !(3..=7).contains(&i) {
262 fn f2(&mut self) -> Option<u32> {
263 // Don't lint, self borrowed inside the loop
264 while let Some(i) = self.0.next() {
272 impl<T: Iterator<Item = u32>> S<(S<T>, Option<u32>)> {
273 fn f3(&mut self) -> Option<u32> {
274 // Don't lint, self borrowed inside the loop
275 while let Some(i) = self.0.0.0.next() {
280 while let Some(i) = self.0.0.0.next() {
285 // This one is fine, a different field is borrowed
286 for i in &mut self.0.0.0 {
288 return self.0.1.take();
297 struct S2<T>(T, u32);
298 impl<T: Iterator<Item = u32>> Iterator for S2<T> {
300 fn next(&mut self) -> Option<u32> {
305 // Don't lint, field of the iterator is accessed in the loop
306 let mut it = S2(1..40, 0);
307 while let Some(n) = it.next() {
313 // Needs &mut, field of the iterator is accessed after the loop
314 let mut it = S2(1..40, 0);
320 println!("iterator field {}", it.1);