]>
git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/while_let_on_iterator.rs
3 #![warn(clippy::while_let_on_iterator)]
9 clippy
::equatable_if_let
14 while let Option
::Some(x
) = iter
.next() {
19 while let Some(x
) = iter
.next() {
24 while let Some(_
) = iter
.next() {}
27 while let None
= iter
.next() {}
// this is fine (if nonsensical)
30 if let Some(x
) = iter
.next() {
35 // the following shouldn't warn because it can't be written with a for loop
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() {
44 println
!("next: {:?}", iter
.next());
48 let mut iter
= 1u32..20;
49 while let Some(_
) = iter
.next() {
59 // consume all the 42s
60 while let Some(&42) = b
.next() {}
65 while let Some(&(1, 2, 3)) = b
.next() {}
70 while let Some(&None
) = b
.next() {}
72 /* This gives “refutable pattern in `for` loop binding: `&_` not covered”
74 for &(1, 2, 3) in b {}
75 for &Option::None in b.next() {}
82 let v
= vec
![1, 2, 3];
83 let mut it
= v
.windows(2);
84 while let Some([x
, y
]) = it
.next() {
89 let mut it
= v
.windows(2);
90 while let Some([x
, ..]) = it
.next() {
94 let mut it
= v
.windows(2);
95 while let Some([.., y
]) = it
.next() {
99 let mut it
= v
.windows(2);
100 while let Some([..]) = it
.next() {}
102 let v
= vec
![[1], [2], [3]];
103 let mut it
= v
.iter();
104 while let Some([1]) = it
.next() {}
106 let mut it
= v
.iter();
107 while let Some([_x
]) = it
.next() {}
112 let v
= vec
![1, 2, 3];
113 let mut it
= v
.iter();
114 while let Some(x @
1) = it
.next() {
118 let v
= vec
![[1], [2], [3]];
119 let mut it
= v
.iter();
120 while let Some(x @
[_
]) = it
.next() {
127 let v
= vec
![1, 2, 3];
128 let mut it
= v
.iter().map(Some
);
129 while let Some(Some(_
) | None
) = it
.next() {
139 let mut y
= a
.iter();
140 while let Some(_
) = y
.next() {
141 // use a for loop here
147 use std
::collections
::HashSet
;
148 let mut values
= HashSet
::new();
151 while let Some(&value
) = values
.iter().next() {
152 values
.remove(&value
);
157 // This should not cause an ICE
159 use std
::collections
::HashSet
;
160 let mut values
= HashSet
::new();
163 while let Some(..) = values
.iter().next() {}
167 let array
= [Some(0), None
, Some(1)];
168 let mut iter
= array
.iter();
170 while let Some(elem
) = iter
.next() {
171 let _
= elem
.or_else(|| *iter
.next()?
);
176 // should not lint if the iterator is generated on every iteration
177 use std
::collections
::HashSet
;
178 let mut values
= HashSet
::new();
181 while let Some(..) = values
.iter().next() {
185 while let Some(..) = values
.iter().map(|x
| x
+ 1).next() {}
187 let chars
= "Hello, World!".char_indices();
188 while let Some((i
, ch
)) = chars
.clone().next() {
189 println
!("{}: {}", i
, ch
);
194 // Used in outer loop, needs &mut
196 while let Some(n
) = it
.next() {
197 while let Some(m
) = it
.next() {
201 println
!("doing something with m: {}", m
);
203 println
!("n still is {}", n
);
206 // This is fine, inner loop uses a new iterator.
208 while let Some(n
) = it
.next() {
210 while let Some(m
) = it
.next() {
214 println
!("doing something with m: {}", m
);
217 // Weird binding shouldn't change anything.
218 let (mut it
, _
) = (1..40, 0);
219 while let Some(m
) = it
.next() {
223 println
!("doing something with m: {}", m
);
226 // Used after the loop, needs &mut.
228 while let Some(m
) = it
.next() {
232 println
!("doing something with m: {}", m
);
234 println
!("next item {}", it
.next().unwrap());
236 println
!("n still is {}", n
);
241 // Closure in the outer loop, needs &mut
243 let mut opt
= Some(0);
244 while let Some(n
) = opt
.take().or_else(|| it
.next()) {
245 while let Some(m
) = it
.next() {
249 println
!("doing something with m: {}", m
);
251 println
!("n still is {}", n
);
257 impl<T
: Iterator
<Item
= u32>> S
<T
> {
258 fn f(&mut self) -> Option
<u32> {
260 while let Some(i
) = self.0.next() {
268 fn f2(&mut self) -> Option
<u32> {
269 // Don't lint, self borrowed inside the loop
270 while let Some(i
) = self.0.next() {
278 impl<T
: Iterator
<Item
= u32>> S
<(S
<T
>, Option
<u32>)> {
279 fn f3(&mut self) -> Option
<u32> {
280 // Don't lint, self borrowed inside the loop
281 while let Some(i
) = self.0.0.0.next() {
286 while let Some(i
) = self.0.0.0.next() {
291 // This one is fine, a different field is borrowed
292 while let Some(i
) = self.0.0.0.next() {
294 return self.0.1.take();
303 struct S2
<T
>(T
, u32);
304 impl<T
: Iterator
<Item
= u32>> Iterator
for S2
<T
> {
306 fn next(&mut self) -> Option
<u32> {
311 // Don't lint, field of the iterator is accessed in the loop
312 let mut it
= S2(1..40, 0);
313 while let Some(n
) = it
.next() {
319 // Needs &mut, field of the iterator is accessed after the loop
320 let mut it
= S2(1..40, 0);
321 while let Some(n
) = it
.next() {
326 println
!("iterator field {}", it
.1);
332 // Needs &mut, the closure can be called multiple times
333 while let Some(x
) = it
.next() {
346 // Needs to reborrow `it` as the binding isn't mutable
347 while let Some(x
) = it
.next() {
352 println
!("{}", it
.next().unwrap());
357 // Needs to reborrow `it.0` as the binding isn't mutable
358 while let Some(x
) = it
.0.next() {
363 println
!("{}", it
.0.next().unwrap());
366 fn exact_match_with_single_field() {
368 let mut s
= S(0..10);
369 // Don't lint. `s.0` is used inside the loop.
370 while let Some(_
) = s
.0.next() {
377 while let Some(..) = it
.next() {