]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/for_loop.rs
d35beb617e05cf0fb0e21e06e47f0104f7387617
[rust.git] / tests / compile-fail / for_loop.rs
1 #![feature(plugin, step_by, inclusive_range_syntax)]
2 #![plugin(clippy)]
3
4 use std::collections::*;
5
6 static STATIC: [usize; 4] = [ 0,  1,  8, 16 ];
7 const CONST: [usize; 4] = [ 0,  1,  8, 16 ];
8
9 #[deny(clippy)]
10 fn for_loop_over_option_and_result() {
11     let option = Some(1);
12     let result = option.ok_or("x not found");
13     let v = vec![0,1,2];
14
15     // check FOR_LOOP_OVER_OPTION lint
16
17     for x in option {
18         //~^ ERROR for loop over `option`, which is an `Option`.
19         //~| HELP consider replacing `for x in option` with `if let Some(x) = option`
20         println!("{}", x);
21     }
22
23     // check FOR_LOOP_OVER_RESULT lint
24
25     for x in result {
26         //~^ ERROR for loop over `result`, which is a `Result`.
27         //~| HELP consider replacing `for x in result` with `if let Ok(x) = result`
28         println!("{}", x);
29     }
30
31     for x in option.ok_or("x not found") {
32         //~^ ERROR for loop over `option.ok_or("x not found")`, which is a `Result`.
33         //~| HELP consider replacing `for x in option.ok_or("x not found")` with `if let Ok(x) = option.ok_or("x not found")`
34         println!("{}", x);
35     }
36
37     // make sure LOOP_OVER_NEXT lint takes precedence when next() is the last call in the chain
38
39     for x in v.iter().next() {
40         //~^ ERROR you are iterating over `Iterator::next()` which is an Option
41         println!("{}", x);
42     }
43
44     // make sure we lint when next() is not the last call in the chain
45
46     for x in v.iter().next().and(Some(0)) {
47         //~^ ERROR for loop over `v.iter().next().and(Some(0))`, which is an `Option`
48         //~| HELP consider replacing `for x in v.iter().next().and(Some(0))` with `if let Some(x) = v.iter().next().and(Some(0))`
49         println!("{}", x);
50     }
51
52     for x in v.iter().next().ok_or("x not found") {
53         //~^ ERROR for loop over `v.iter().next().ok_or("x not found")`, which is a `Result`
54         //~| HELP consider replacing `for x in v.iter().next().ok_or("x not found")` with `if let Ok(x) = v.iter().next().ok_or("x not found")`
55         println!("{}", x);
56     }
57
58     // check for false positives
59
60     // for loop false positive
61     for x in v {
62         println!("{}", x);
63     }
64
65     // while let false positive for Option
66     while let Some(x) = option {
67         println!("{}", x);
68         break;
69     }
70
71     // while let false positive for Result
72     while let Ok(x) = result {
73         println!("{}", x);
74         break;
75     }
76 }
77
78 struct Unrelated(Vec<u8>);
79 impl Unrelated {
80     fn next(&self) -> std::slice::Iter<u8> {
81         self.0.iter()
82     }
83
84     fn iter(&self) -> std::slice::Iter<u8> {
85         self.0.iter()
86     }
87 }
88
89 #[deny(needless_range_loop, explicit_iter_loop, iter_next_loop, reverse_range_loop, explicit_counter_loop)]
90 #[deny(unused_collect)]
91 #[allow(linkedlist, shadow_unrelated, unnecessary_mut_passed, cyclomatic_complexity, similar_names)]
92 #[allow(many_single_char_names)]
93 fn main() {
94     const MAX_LEN: usize = 42;
95
96     let mut vec = vec![1, 2, 3, 4];
97     let vec2 = vec![1, 2, 3, 4];
98     for i in 0..vec.len() {
99         //~^ ERROR `i` is only used to index `vec`. Consider using `for item in &vec`
100         println!("{}", vec[i]);
101     }
102
103     // ICE #746
104     for j in 0..4 {
105         //~^ ERROR `j` is only used to index `STATIC`
106         println!("{:?}", STATIC[j]);
107     }
108
109     for j in 0..4 {
110         //~^ ERROR `j` is only used to index `CONST`
111         println!("{:?}", CONST[j]);
112     }
113
114     for i in 0..vec.len() {
115         //~^ ERROR `i` is used to index `vec`. Consider using `for (i, item) in vec.iter().enumerate()`
116         println!("{} {}", vec[i], i);
117     }
118     for i in 0..vec.len() {      // not an error, indexing more than one variable
119         println!("{} {}", vec[i], vec2[i]);
120     }
121
122     for i in 0..vec.len() {
123         //~^ ERROR `i` is only used to index `vec2`. Consider using `for item in vec2.iter().take(vec.len())`
124         println!("{}", vec2[i]);
125     }
126
127     for i in 5..vec.len() {
128         //~^ ERROR `i` is only used to index `vec`. Consider using `for item in vec.iter().skip(5)`
129         println!("{}", vec[i]);
130     }
131
132     for i in 0..MAX_LEN {
133         //~^ ERROR `i` is only used to index `vec`. Consider using `for item in vec.iter().take(MAX_LEN)`
134         println!("{}", vec[i]);
135     }
136
137     for i in 0...MAX_LEN {
138         //~^ ERROR `i` is only used to index `vec`. Consider using `for item in vec.iter().take(MAX_LEN)`
139         println!("{}", vec[i]);
140     }
141
142     for i in 5..10 {
143         //~^ ERROR `i` is only used to index `vec`. Consider using `for item in vec.iter().take(10).skip(5)`
144         println!("{}", vec[i]);
145     }
146
147     for i in 5...10 {
148         //~^ ERROR `i` is only used to index `vec`. Consider using `for item in vec.iter().take(10).skip(5)`
149         println!("{}", vec[i]);
150     }
151
152     for i in 5..vec.len() {
153         //~^ ERROR `i` is used to index `vec`. Consider using `for (i, item) in vec.iter().enumerate().skip(5)`
154         println!("{} {}", vec[i], i);
155     }
156
157     for i in 5..10 {
158         //~^ ERROR `i` is used to index `vec`. Consider using `for (i, item) in vec.iter().enumerate().take(10).skip(5)`
159         println!("{} {}", vec[i], i);
160     }
161
162     for i in 10..0 {
163         //~^ERROR this range is empty so this for loop will never run
164         //~|HELP consider
165         //~|SUGGESTION (0..10).rev()
166         println!("{}", i);
167     }
168
169     for i in 10...0 {
170         //~^ERROR this range is empty so this for loop will never run
171         //~|HELP consider
172         //~|SUGGESTION (0..10).rev()
173         println!("{}", i);
174     }
175
176     for i in MAX_LEN..0 { //~ERROR this range is empty so this for loop will never run
177         //~|HELP consider
178         //~|SUGGESTION (0..MAX_LEN).rev()
179         println!("{}", i);
180     }
181
182     for i in 5..5 { //~ERROR this range is empty so this for loop will never run
183         println!("{}", i);
184     }
185
186     for i in 5...5 { // not an error, this is the range with only one element “5”
187         println!("{}", i);
188     }
189
190     for i in 0..10 { // not an error, the start index is less than the end index
191         println!("{}", i);
192     }
193
194     for i in -10..0 { // not an error
195         println!("{}", i);
196     }
197
198     for i in (10..0).map(|x| x * 2) { // not an error, it can't be known what arbitrary methods do to a range
199         println!("{}", i);
200     }
201
202     // testing that the empty range lint folds constants
203     for i in 10..5+4 {
204     //~^ ERROR this range is empty so this for loop will never run
205     //~| HELP if you are attempting to iterate over this range in reverse
206     //~| SUGGESTION for i in (5+4..10).rev() {
207         println!("{}", i);
208     }
209
210     for i in (5+2)..(3-1) {
211     //~^ ERROR this range is empty so this for loop will never run
212     //~| HELP if you are attempting to iterate over this range in reverse
213     //~| SUGGESTION for i in ((3-1)..(5+2)).rev() {
214         println!("{}", i);
215     }
216
217     for i in (5+2)..(8-1) { //~ERROR this range is empty so this for loop will never run
218         println!("{}", i);
219     }
220
221     for i in (2*2)..(2*3) { // no error, 4..6 is fine
222         println!("{}", i);
223     }
224
225     for i in (10..8).step_by(-1) {
226         println!("{}", i);
227     }
228
229     let x = 42;
230     for i in x..10 { // no error, not constant-foldable
231         println!("{}", i);
232     }
233
234     // See #601
235     for i in 0..10 { // no error, id_col does not exist outside the loop
236         let mut id_col = vec![0f64; 10];
237         id_col[i] = 1f64;
238     }
239
240     /*
241     for i in (10..0).map(|x| x * 2) {
242         println!("{}", i);
243     }*/
244
245     for _v in vec.iter() { } //~ERROR it is more idiomatic to loop over `&vec`
246     for _v in vec.iter_mut() { } //~ERROR it is more idiomatic to loop over `&mut vec`
247
248     for _v in &vec { } // these are fine
249     for _v in &mut vec { } // these are fine
250
251     for _v in [1, 2, 3].iter() { } //~ERROR it is more idiomatic to loop over `&[
252     for _v in (&mut [1, 2, 3]).iter() { } // no error
253     for _v in [0; 32].iter() {} //~ERROR it is more idiomatic to loop over `&[
254     for _v in [0; 33].iter() {} // no error
255     let ll: LinkedList<()> = LinkedList::new();
256     for _v in ll.iter() { } //~ERROR it is more idiomatic to loop over `&ll`
257     let vd: VecDeque<()> = VecDeque::new();
258     for _v in vd.iter() { } //~ERROR it is more idiomatic to loop over `&vd`
259     let bh: BinaryHeap<()> = BinaryHeap::new();
260     for _v in bh.iter() { } //~ERROR it is more idiomatic to loop over `&bh`
261     let hm: HashMap<(), ()> = HashMap::new();
262     for _v in hm.iter() { } //~ERROR it is more idiomatic to loop over `&hm`
263     let bt: BTreeMap<(), ()> = BTreeMap::new();
264     for _v in bt.iter() { } //~ERROR it is more idiomatic to loop over `&bt`
265     let hs: HashSet<()> = HashSet::new();
266     for _v in hs.iter() { } //~ERROR it is more idiomatic to loop over `&hs`
267     let bs: BTreeSet<()> = BTreeSet::new();
268     for _v in bs.iter() { } //~ERROR it is more idiomatic to loop over `&bs`
269
270     for _v in vec.iter().next() { } //~ERROR you are iterating over `Iterator::next()`
271
272     let u = Unrelated(vec![]);
273     for _v in u.next() { } // no error
274     for _v in u.iter() { } // no error
275
276     let mut out = vec![];
277     vec.iter().map(|x| out.push(x)).collect::<Vec<_>>(); //~ERROR you are collect()ing an iterator
278     let _y = vec.iter().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
279
280     // Loop with explicit counter variable
281     let mut _index = 0;
282     for _v in &vec { _index += 1 } //~ERROR the variable `_index` is used as a loop counter
283
284     let mut _index = 1;
285     _index = 0;
286     for _v in &vec { _index += 1 } //~ERROR the variable `_index` is used as a loop counter
287
288     // Potential false positives
289     let mut _index = 0;
290     _index = 1;
291     for _v in &vec { _index += 1 }
292
293     let mut _index = 0;
294     _index += 1;
295     for _v in &vec { _index += 1 }
296
297     let mut _index = 0;
298     if true { _index = 1 }
299     for _v in &vec { _index += 1 }
300
301     let mut _index = 0;
302     let mut _index = 1;
303     for _v in &vec { _index += 1 }
304
305     let mut _index = 0;
306     for _v in &vec { _index += 1; _index += 1 }
307
308     let mut _index = 0;
309     for _v in &vec { _index *= 2; _index += 1 }
310
311     let mut _index = 0;
312     for _v in &vec { _index = 1; _index += 1 }
313
314     let mut _index = 0;
315
316     for _v in &vec { let mut _index = 0; _index += 1 }
317
318     let mut _index = 0;
319     for _v in &vec { _index += 1; _index = 0; }
320
321     let mut _index = 0;
322     for _v in &vec { for _x in 0..1 { _index += 1; }; _index += 1 }
323
324     let mut _index = 0;
325     for x in &vec { if *x == 1 { _index += 1 } }
326
327     let mut _index = 0;
328     if true { _index = 1 };
329     for _v in &vec { _index += 1 }
330
331     let mut _index = 1;
332     if false { _index = 0 };
333     for _v in &vec { _index += 1 }
334
335     let mut index = 0;
336     { let mut _x = &mut index; }
337     for _v in &vec { _index += 1 }
338
339     let mut index = 0;
340     for _v in &vec { index += 1 }
341     println!("index: {}", index);
342
343     for_loop_over_option_and_result();
344
345     let m : HashMap<u64, u64> = HashMap::new();
346     for (_, v) in &m {
347         //~^ you seem to want to iterate on a map's values
348         //~| HELP use the corresponding method
349         //~| SUGGESTION for v in m.values()
350         let _v = v;
351     }
352
353     let mut m : HashMap<u64, u64> = HashMap::new();
354     for (_, v) in &mut m {
355         // Ok, there is no values_mut method or equivalent
356         let _v = v;
357     }
358
359
360     let rm = &m;
361     for (k, _value) in rm {
362         //~^ you seem to want to iterate on a map's keys
363         //~| HELP use the corresponding method
364         //~| SUGGESTION for k in rm.keys()
365         let _k = k;
366     }
367
368     test_for_kv_map();
369 }
370
371 #[allow(used_underscore_binding)]
372 fn test_for_kv_map() {
373     let m : HashMap<u64, u64> = HashMap::new();
374
375     // No error, _value is actually used
376     for (k, _value) in &m {
377         let _ = _value;
378         let _k = k;
379     }
380 }