]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/while_let_on_iterator.fixed
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / while_let_on_iterator.fixed
1 #![warn(clippy::while_let_on_iterator)]
2 #![allow(dead_code, unreachable_code, unused_mut)]
3 #![allow(
4 clippy::equatable_if_let,
5 clippy::manual_find,
6 clippy::never_loop,
7 clippy::redundant_closure_call,
8 clippy::single_range_in_vec_init,
9 clippy::uninlined_format_args,
10 clippy::useless_vec
11 )]
12
13 fn base() {
14 let mut iter = 1..20;
15 for x in iter {
16 println!("{}", x);
17 }
18
19 let mut iter = 1..20;
20 for x in iter {
21 println!("{}", x);
22 }
23
24 let mut iter = 1..20;
25 for _ in iter {}
26
27 let mut iter = 1..20;
28 while let None = iter.next() {} // this is fine (if nonsensical)
29
30 let mut iter = 1..20;
31 if let Some(x) = iter.next() {
32 // also fine
33 println!("{}", x)
34 }
35
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())
40 }
41
42 // neither can this
43 let mut iter = 1u32..20;
44 while let Some(_) = iter.next() {
45 println!("next: {:?}", iter.next());
46 }
47
48 // or this
49 let mut iter = 1u32..20;
50 while let Some(_) = iter.next() {
51 iter = 1..20;
52 }
53 }
54
55 // Issue #1188
56 fn refutable() {
57 let a = [42, 1337];
58 let mut b = a.iter();
59
60 // consume all the 42s
61 while let Some(&42) = b.next() {}
62
63 let a = [(1, 2, 3)];
64 let mut b = a.iter();
65
66 while let Some(&(1, 2, 3)) = b.next() {}
67
68 let a = [Some(42)];
69 let mut b = a.iter();
70
71 while let Some(&None) = b.next() {}
72
73 /* This gives “refutable pattern in `for` loop binding: `&_` not covered”
74 for &42 in b {}
75 for &(1, 2, 3) in b {}
76 for &Option::None in b.next() {}
77 // */
78 }
79
80 fn refutable2() {
81 // Issue 3780
82 {
83 let v = vec![1, 2, 3];
84 let mut it = v.windows(2);
85 while let Some([x, y]) = it.next() {
86 println!("x: {}", x);
87 println!("y: {}", y);
88 }
89
90 let mut it = v.windows(2);
91 while let Some([x, ..]) = it.next() {
92 println!("x: {}", x);
93 }
94
95 let mut it = v.windows(2);
96 while let Some([.., y]) = it.next() {
97 println!("y: {}", y);
98 }
99
100 let mut it = v.windows(2);
101 for [..] in it {}
102
103 let v = vec![[1], [2], [3]];
104 let mut it = v.iter();
105 while let Some([1]) = it.next() {}
106
107 let mut it = v.iter();
108 for [_x] in it {}
109 }
110
111 // binding
112 {
113 let v = vec![1, 2, 3];
114 let mut it = v.iter();
115 while let Some(x @ 1) = it.next() {
116 println!("{}", x);
117 }
118
119 let v = vec![[1], [2], [3]];
120 let mut it = v.iter();
121 for x @ [_] in it {
122 println!("{:?}", x);
123 }
124 }
125
126 // false negative
127 {
128 let v = vec![1, 2, 3];
129 let mut it = v.iter().map(Some);
130 while let Some(Some(_) | None) = it.next() {
131 println!("1");
132 }
133 }
134 }
135
136 fn nested_loops() {
137 let a = [42, 1337];
138
139 loop {
140 let mut y = a.iter();
141 for _ in y {
142 // use a for loop here
143 }
144 }
145 }
146
147 fn issue1121() {
148 use std::collections::HashSet;
149 let mut values = HashSet::new();
150 values.insert(1);
151
152 while let Some(&value) = values.iter().next() {
153 values.remove(&value);
154 }
155 }
156
157 fn issue2965() {
158 // This should not cause an ICE
159
160 use std::collections::HashSet;
161 let mut values = HashSet::new();
162 values.insert(1);
163
164 while let Some(..) = values.iter().next() {}
165 }
166
167 fn issue3670() {
168 let array = [Some(0), None, Some(1)];
169 let mut iter = array.iter();
170
171 while let Some(elem) = iter.next() {
172 let _ = elem.or_else(|| *iter.next()?);
173 }
174 }
175
176 fn issue1654() {
177 // should not lint if the iterator is generated on every iteration
178 use std::collections::HashSet;
179 let mut values = HashSet::new();
180 values.insert(1);
181
182 while let Some(..) = values.iter().next() {
183 values.remove(&1);
184 }
185
186 while let Some(..) = values.iter().map(|x| x + 1).next() {}
187
188 let chars = "Hello, World!".char_indices();
189 while let Some((i, ch)) = chars.clone().next() {
190 println!("{}: {}", i, ch);
191 }
192 }
193
194 fn issue6491() {
195 // Used in outer loop, needs &mut
196 let mut it = 1..40;
197 while let Some(n) = it.next() {
198 for m in it.by_ref() {
199 if m % 10 == 0 {
200 break;
201 }
202 println!("doing something with m: {}", m);
203 }
204 println!("n still is {}", n);
205 }
206
207 // This is fine, inner loop uses a new iterator.
208 let mut it = 1..40;
209 for n in it {
210 let mut it = 1..40;
211 for m in it {
212 if m % 10 == 0 {
213 break;
214 }
215 println!("doing something with m: {}", m);
216 }
217
218 // Weird binding shouldn't change anything.
219 let (mut it, _) = (1..40, 0);
220 for m in it {
221 if m % 10 == 0 {
222 break;
223 }
224 println!("doing something with m: {}", m);
225 }
226
227 // Used after the loop, needs &mut.
228 let mut it = 1..40;
229 for m in it.by_ref() {
230 if m % 10 == 0 {
231 break;
232 }
233 println!("doing something with m: {}", m);
234 }
235 println!("next item {}", it.next().unwrap());
236
237 println!("n still is {}", n);
238 }
239 }
240
241 fn issue6231() {
242 // Closure in the outer loop, needs &mut
243 let mut it = 1..40;
244 let mut opt = Some(0);
245 while let Some(n) = opt.take().or_else(|| it.next()) {
246 for m in it.by_ref() {
247 if n % 10 == 0 {
248 break;
249 }
250 println!("doing something with m: {}", m);
251 }
252 println!("n still is {}", n);
253 }
254 }
255
256 fn issue1924() {
257 struct S<T>(T);
258 impl<T: Iterator<Item = u32>> S<T> {
259 fn f(&mut self) -> Option<u32> {
260 // Used as a field.
261 for i in self.0.by_ref() {
262 if !(3..8).contains(&i) {
263 return Some(i);
264 }
265 }
266 None
267 }
268
269 fn f2(&mut self) -> Option<u32> {
270 // Don't lint, self borrowed inside the loop
271 while let Some(i) = self.0.next() {
272 if i == 1 {
273 return self.f();
274 }
275 }
276 None
277 }
278 }
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() {
283 if i == 1 {
284 return self.0.0.f();
285 }
286 }
287 while let Some(i) = self.0.0.0.next() {
288 if i == 1 {
289 return self.f3();
290 }
291 }
292 // This one is fine, a different field is borrowed
293 for i in self.0.0.0.by_ref() {
294 if i == 1 {
295 return self.0.1.take();
296 } else {
297 self.0.1 = Some(i);
298 }
299 }
300 None
301 }
302 }
303
304 struct S2<T>(T, u32);
305 impl<T: Iterator<Item = u32>> Iterator for S2<T> {
306 type Item = u32;
307 fn next(&mut self) -> Option<u32> {
308 self.0.next()
309 }
310 }
311
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() {
315 if n == it.1 {
316 break;
317 }
318 }
319
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() {
323 if n == 0 {
324 break;
325 }
326 }
327 println!("iterator field {}", it.1);
328 }
329
330 fn issue7249() {
331 let mut it = 0..10;
332 let mut x = || {
333 // Needs &mut, the closure can be called multiple times
334 for x in it.by_ref() {
335 if x % 2 == 0 {
336 break;
337 }
338 }
339 };
340 x();
341 x();
342 }
343
344 fn issue7510() {
345 let mut it = 0..10;
346 let it = &mut it;
347 // Needs to reborrow `it` as the binding isn't mutable
348 for x in it.by_ref() {
349 if x % 2 == 0 {
350 break;
351 }
352 }
353 println!("{}", it.next().unwrap());
354
355 struct S<T>(T);
356 let mut it = 0..10;
357 let it = S(&mut it);
358 // Needs to reborrow `it.0` as the binding isn't mutable
359 for x in it.0.by_ref() {
360 if x % 2 == 0 {
361 break;
362 }
363 }
364 println!("{}", it.0.next().unwrap());
365 }
366
367 fn exact_match_with_single_field() {
368 struct S<T>(T);
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() {
372 let _ = &mut s.0;
373 }
374 }
375
376 fn custom_deref() {
377 struct S1<T> {
378 x: T,
379 }
380 struct S2<T>(S1<T>);
381 impl<T> core::ops::Deref for S2<T> {
382 type Target = S1<T>;
383 fn deref(&self) -> &Self::Target {
384 &self.0
385 }
386 }
387 impl<T> core::ops::DerefMut for S2<T> {
388 fn deref_mut(&mut self) -> &mut Self::Target {
389 &mut self.0
390 }
391 }
392
393 let mut s = S2(S1 { x: 0..10 });
394 for x in s.x.by_ref() {
395 println!("{}", x);
396 }
397 }
398
399 fn issue_8113() {
400 let mut x = [0..10];
401 for x in x[0].by_ref() {
402 println!("{}", x);
403 }
404 }
405
406 fn fn_once_closure() {
407 let mut it = 0..10;
408 (|| {
409 for x in it {
410 if x % 2 == 0 {
411 break;
412 }
413 }
414 })();
415
416 fn f(_: impl FnOnce()) {}
417 let mut it = 0..10;
418 f(|| {
419 for x in it {
420 if x % 2 == 0 {
421 break;
422 }
423 }
424 });
425
426 fn f2(_: impl FnMut()) {}
427 let mut it = 0..10;
428 f2(|| {
429 for x in it.by_ref() {
430 if x % 2 == 0 {
431 break;
432 }
433 }
434 });
435
436 fn f3(_: fn()) {}
437 f3(|| {
438 let mut it = 0..10;
439 for x in it {
440 if x % 2 == 0 {
441 break;
442 }
443 }
444 })
445 }
446
447 fn main() {
448 let mut it = 0..20;
449 for _ in it {
450 println!("test");
451 }
452 }