]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/for_loop.rs
Fix tests
[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 { //~ERROR this range is empty so this for loop will never run
204         println!("{}", i);
205     }
206
207     for i in (5+2)..(3-1) { //~ERROR this range is empty so this for loop will never run
208         println!("{}", i);
209     }
210
211     for i in (5+2)..(8-1) { //~ERROR this range is empty so this for loop will never run
212         println!("{}", i);
213     }
214
215     for i in (2*2)..(2*3) { // no error, 4..6 is fine
216         println!("{}", i);
217     }
218
219     for i in (10..8).step_by(-1) {
220         println!("{}", i);
221     }
222
223     let x = 42;
224     for i in x..10 { // no error, not constant-foldable
225         println!("{}", i);
226     }
227
228     // See #601
229     for i in 0..10 { // no error, id_col does not exist outside the loop
230         let mut id_col = vec![0f64; 10];
231         id_col[i] = 1f64;
232     }
233
234     /*
235     for i in (10..0).map(|x| x * 2) {
236         println!("{}", i);
237     }*/
238
239     for _v in vec.iter() { } //~ERROR it is more idiomatic to loop over `&vec`
240     for _v in vec.iter_mut() { } //~ERROR it is more idiomatic to loop over `&mut vec`
241
242     for _v in &vec { } // these are fine
243     for _v in &mut vec { } // these are fine
244
245     for _v in [1, 2, 3].iter() { } //~ERROR it is more idiomatic to loop over `&[
246     for _v in (&mut [1, 2, 3]).iter() { } // no error
247     for _v in [0; 32].iter() {} //~ERROR it is more idiomatic to loop over `&[
248     for _v in [0; 33].iter() {} // no error
249     let ll: LinkedList<()> = LinkedList::new();
250     for _v in ll.iter() { } //~ERROR it is more idiomatic to loop over `&ll`
251     let vd: VecDeque<()> = VecDeque::new();
252     for _v in vd.iter() { } //~ERROR it is more idiomatic to loop over `&vd`
253     let bh: BinaryHeap<()> = BinaryHeap::new();
254     for _v in bh.iter() { } //~ERROR it is more idiomatic to loop over `&bh`
255     let hm: HashMap<(), ()> = HashMap::new();
256     for _v in hm.iter() { } //~ERROR it is more idiomatic to loop over `&hm`
257     let bt: BTreeMap<(), ()> = BTreeMap::new();
258     for _v in bt.iter() { } //~ERROR it is more idiomatic to loop over `&bt`
259     let hs: HashSet<()> = HashSet::new();
260     for _v in hs.iter() { } //~ERROR it is more idiomatic to loop over `&hs`
261     let bs: BTreeSet<()> = BTreeSet::new();
262     for _v in bs.iter() { } //~ERROR it is more idiomatic to loop over `&bs`
263
264     for _v in vec.iter().next() { } //~ERROR you are iterating over `Iterator::next()`
265
266     let u = Unrelated(vec![]);
267     for _v in u.next() { } // no error
268     for _v in u.iter() { } // no error
269
270     let mut out = vec![];
271     vec.iter().map(|x| out.push(x)).collect::<Vec<_>>(); //~ERROR you are collect()ing an iterator
272     let _y = vec.iter().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
273
274     // Loop with explicit counter variable
275     let mut _index = 0;
276     for _v in &vec { _index += 1 } //~ERROR the variable `_index` is used as a loop counter
277
278     let mut _index = 1;
279     _index = 0;
280     for _v in &vec { _index += 1 } //~ERROR the variable `_index` is used as a loop counter
281
282     // Potential false positives
283     let mut _index = 0;
284     _index = 1;
285     for _v in &vec { _index += 1 }
286
287     let mut _index = 0;
288     _index += 1;
289     for _v in &vec { _index += 1 }
290
291     let mut _index = 0;
292     if true { _index = 1 }
293     for _v in &vec { _index += 1 }
294
295     let mut _index = 0;
296     let mut _index = 1;
297     for _v in &vec { _index += 1 }
298
299     let mut _index = 0;
300     for _v in &vec { _index += 1; _index += 1 }
301
302     let mut _index = 0;
303     for _v in &vec { _index *= 2; _index += 1 }
304
305     let mut _index = 0;
306     for _v in &vec { _index = 1; _index += 1 }
307
308     let mut _index = 0;
309
310     for _v in &vec { let mut _index = 0; _index += 1 }
311
312     let mut _index = 0;
313     for _v in &vec { _index += 1; _index = 0; }
314
315     let mut _index = 0;
316     for _v in &vec { for _x in 0..1 { _index += 1; }; _index += 1 }
317
318     let mut _index = 0;
319     for x in &vec { if *x == 1 { _index += 1 } }
320
321     let mut _index = 0;
322     if true { _index = 1 };
323     for _v in &vec { _index += 1 }
324
325     let mut _index = 1;
326     if false { _index = 0 };
327     for _v in &vec { _index += 1 }
328
329     let mut index = 0;
330     { let mut _x = &mut index; }
331     for _v in &vec { _index += 1 }
332
333     let mut index = 0;
334     for _v in &vec { index += 1 }
335     println!("index: {}", index);
336
337     for_loop_over_option_and_result();
338
339     let m : HashMap<u64, u64> = HashMap::new();
340     for (_, v) in &m {
341         //~^ you seem to want to iterate on a map's values
342         //~| HELP use the corresponding method
343         //~| SUGGESTION for v in m.values()
344         let _v = v;
345     }
346
347     let mut m : HashMap<u64, u64> = HashMap::new();
348     for (_, v) in &mut m {
349         // Ok, there is no values_mut method or equivalent
350         let _v = v;
351     }
352
353
354     let rm = &m;
355     for (k, _value) in rm {
356         //~^ you seem to want to iterate on a map's keys
357         //~| HELP use the corresponding method
358         //~| SUGGESTION for k in rm.keys()
359         let _k = k;
360     }
361
362     test_for_kv_map();
363 }
364
365 #[allow(used_underscore_binding)]
366 fn test_for_kv_map() {
367     let m : HashMap<u64, u64> = HashMap::new();
368
369     // No error, _value is actually used
370     for (k, _value) in &m {
371         let _ = _value;
372         let _k = k;
373     }
374 }