1
1
// run-rustfix
2
2
3
3
#![warn(clippy::while_let_on_iterator)]
4
- #![allow(clippy::never_loop, unreachable_code, unused_mut)]
4
+ #![allow(clippy::never_loop, unreachable_code, unused_mut, dead_code )]
5
5
6
6
fn base() {
7
7
let mut iter = 1..20;
@@ -38,13 +38,6 @@ fn base() {
38
38
println!("next: {:?}", iter.next());
39
39
}
40
40
41
- // or this
42
- let mut iter = 1u32..20;
43
- while let Some(_) = iter.next() {
44
- break;
45
- }
46
- println!("Remaining iter {:?}", iter);
47
-
48
41
// or this
49
42
let mut iter = 1u32..20;
50
43
while let Some(_) = iter.next() {
@@ -135,18 +128,6 @@ fn refutable2() {
135
128
136
129
fn nested_loops() {
137
130
let a = [42, 1337];
138
- let mut y = a.iter();
139
- loop {
140
- // x is reused, so don't lint here
141
- while let Some(_) = y.next() {}
142
- }
143
-
144
- let mut y = a.iter();
145
- for _ in 0..2 {
146
- while let Some(_) = y.next() {
147
- // y is reused, don't lint
148
- }
149
- }
150
131
151
132
loop {
152
133
let mut y = a.iter();
@@ -167,10 +148,8 @@ fn issue1121() {
167
148
}
168
149
169
150
fn issue2965() {
170
- // This should not cause an ICE and suggest:
171
- //
172
- // for _ in values.iter() {}
173
- //
151
+ // This should not cause an ICE
152
+
174
153
use std::collections::HashSet;
175
154
let mut values = HashSet::new();
176
155
values.insert(1);
@@ -205,13 +184,145 @@ fn issue1654() {
205
184
}
206
185
}
207
186
187
+ fn issue6491() {
188
+ // Used in outer loop, needs &mut
189
+ let mut it = 1..40;
190
+ while let Some(n) = it.next() {
191
+ for m in &mut it {
192
+ if m % 10 == 0 {
193
+ break;
194
+ }
195
+ println!("doing something with m: {}", m);
196
+ }
197
+ println!("n still is {}", n);
198
+ }
199
+
200
+ // This is fine, inner loop uses a new iterator.
201
+ let mut it = 1..40;
202
+ for n in it {
203
+ let mut it = 1..40;
204
+ for m in it {
205
+ if m % 10 == 0 {
206
+ break;
207
+ }
208
+ println!("doing something with m: {}", m);
209
+ }
210
+
211
+ // Weird binding shouldn't change anything.
212
+ let (mut it, _) = (1..40, 0);
213
+ for m in it {
214
+ if m % 10 == 0 {
215
+ break;
216
+ }
217
+ println!("doing something with m: {}", m);
218
+ }
219
+
220
+ // Used after the loop, needs &mut.
221
+ let mut it = 1..40;
222
+ for m in &mut it {
223
+ if m % 10 == 0 {
224
+ break;
225
+ }
226
+ println!("doing something with m: {}", m);
227
+ }
228
+ println!("next item {}", it.next().unwrap());
229
+
230
+ println!("n still is {}", n);
231
+ }
232
+ }
233
+
234
+ fn issue6231() {
235
+ // Closure in the outer loop, needs &mut
236
+ let mut it = 1..40;
237
+ let mut opt = Some(0);
238
+ while let Some(n) = opt.take().or_else(|| it.next()) {
239
+ for m in &mut it {
240
+ if n % 10 == 0 {
241
+ break;
242
+ }
243
+ println!("doing something with m: {}", m);
244
+ }
245
+ println!("n still is {}", n);
246
+ }
247
+ }
248
+
249
+ fn issue1924() {
250
+ struct S<T>(T);
251
+ impl<T: Iterator<Item = u32>> S<T> {
252
+ fn f(&mut self) -> Option<u32> {
253
+ // Used as a field.
254
+ for i in &mut self.0 {
255
+ if !(3..=7).contains(&i) {
256
+ return Some(i);
257
+ }
258
+ }
259
+ None
260
+ }
261
+
262
+ fn f2(&mut self) -> Option<u32> {
263
+ // Don't lint, self borrowed inside the loop
264
+ while let Some(i) = self.0.next() {
265
+ if i == 1 {
266
+ return self.f();
267
+ }
268
+ }
269
+ None
270
+ }
271
+ }
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() {
276
+ if i == 1 {
277
+ return self.0.0.f();
278
+ }
279
+ }
280
+ while let Some(i) = self.0.0.0.next() {
281
+ if i == 1 {
282
+ return self.f3();
283
+ }
284
+ }
285
+ // This one is fine, a different field is borrowed
286
+ for i in &mut self.0.0.0 {
287
+ if i == 1 {
288
+ return self.0.1.take();
289
+ } else {
290
+ self.0.1 = Some(i);
291
+ }
292
+ }
293
+ None
294
+ }
295
+ }
296
+
297
+ struct S2<T>(T, u32);
298
+ impl<T: Iterator<Item = u32>> Iterator for S2<T> {
299
+ type Item = u32;
300
+ fn next(&mut self) -> Option<u32> {
301
+ self.0.next()
302
+ }
303
+ }
304
+
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() {
308
+ if n == it.1 {
309
+ break;
310
+ }
311
+ }
312
+
313
+ // Needs &mut, field of the iterator is accessed after the loop
314
+ let mut it = S2(1..40, 0);
315
+ for n in &mut it {
316
+ if n == 0 {
317
+ break;
318
+ }
319
+ }
320
+ println!("iterator field {}", it.1);
321
+ }
322
+
208
323
fn main() {
209
- base();
210
- refutable();
211
- refutable2();
212
- nested_loops();
213
- issue1121();
214
- issue2965();
215
- issue3670();
216
- issue1654();
324
+ let mut it = 0..20;
325
+ for _ in it {
326
+ println!("test");
327
+ }
217
328
}
0 commit comments