1 #![feature(plugin, step_by, inclusive_range_syntax)]
4 use std::collections::*;
7 static STATIC: [usize; 4] = [ 0, 1, 8, 16 ];
8 const CONST: [usize; 4] = [ 0, 1, 8, 16 ];
11 fn for_loop_over_option_and_result() {
13 let result = option.ok_or("x not found");
16 // check FOR_LOOP_OVER_OPTION lint
24 // check FOR_LOOP_OVER_RESULT lint
32 for x in option.ok_or("x not found") {
38 // make sure LOOP_OVER_NEXT lint takes precedence when next() is the last call in the chain
40 for x in v.iter().next() {
45 // make sure we lint when next() is not the last call in the chain
47 for x in v.iter().next().and(Some(0)) {
53 for x in v.iter().next().ok_or("x not found") {
59 // check for false positives
61 // for loop false positive
66 // while let false positive for Option
67 while let Some(x) = option {
72 // while let false positive for Result
73 while let Ok(x) = result {
79 struct Unrelated(Vec<u8>);
81 fn next(&self) -> std::slice::Iter<u8> {
85 fn iter(&self) -> std::slice::Iter<u8> {
90 #[deny(needless_range_loop, explicit_iter_loop, explicit_into_iter_loop, iter_next_loop, reverse_range_loop, explicit_counter_loop, for_kv_map)]
91 #[deny(unused_collect)]
92 #[allow(linkedlist, shadow_unrelated, unnecessary_mut_passed, cyclomatic_complexity, similar_names)]
93 #[allow(many_single_char_names)]
95 const MAX_LEN: usize = 42;
97 let mut vec = vec![1, 2, 3, 4];
98 let vec2 = vec![1, 2, 3, 4];
99 for i in 0..vec.len() {
104 println!("{}", vec[i]);
107 for i in 0..vec.len() {
109 let i = 42; // make a different `i`
110 println!("{}", vec[i]); // ok, not the `i` of the for-loop
113 for i in 0..vec.len() { let _ = vec[i]; }
125 println!("{:?}", STATIC[j]);
133 println!("{:?}", CONST[j]);
136 for i in 0..vec.len() {
141 println!("{} {}", vec[i], i);
143 for i in 0..vec.len() { // not an error, indexing more than one variable
144 println!("{} {}", vec[i], vec2[i]);
147 for i in 0..vec.len() {
152 println!("{}", vec2[i]);
155 for i in 5..vec.len() {
160 println!("{}", vec[i]);
163 for i in 0..MAX_LEN {
168 println!("{}", vec[i]);
171 for i in 0...MAX_LEN {
176 println!("{}", vec[i]);
184 println!("{}", vec[i]);
192 println!("{}", vec[i]);
195 for i in 5..vec.len() {
200 println!("{} {}", vec[i], i);
208 println!("{} {}", vec[i], i);
225 for i in MAX_LEN..0 {
235 for i in 5...5 { // not an error, this is the range with only one element “5”
239 for i in 0..10 { // not an error, the start index is less than the end index
243 for i in -10..0 { // not an error
247 for i in (10..0).map(|x| x * 2) { // not an error, it can't be known what arbitrary methods do to a range
251 // testing that the empty range lint folds constants
259 for i in (5+2)..(3-1) {
266 for i in (5+2)..(8-1) {
270 for i in (2*2)..(2*3) { // no error, 4..6 is fine
274 for i in (10..8).step_by(-1) {
279 for i in x..10 { // no error, not constant-foldable
284 for i in 0..10 { // no error, id_col does not exist outside the loop
285 let mut id_col = vec![0f64; 10];
289 for _v in vec.iter() { }
294 for _v in vec.iter_mut() { }
299 let out_vec = vec![1,2,3];
300 for _v in out_vec.into_iter() { }
305 let array = [1, 2, 3];
306 for _v in array.into_iter() {} //~ERROR it is more idiomatic to loop over `&array`
308 for _v in &vec { } // these are fine
309 for _v in &mut vec { } // these are fine
311 for _v in [1, 2, 3].iter() { }
316 for _v in (&mut [1, 2, 3]).iter() { } // no error
318 for _v in [0; 32].iter() {}
323 for _v in [0; 33].iter() {} // no error
325 let ll: LinkedList<()> = LinkedList::new();
326 for _v in ll.iter() { }
331 let vd: VecDeque<()> = VecDeque::new();
332 for _v in vd.iter() { }
337 let bh: BinaryHeap<()> = BinaryHeap::new();
338 for _v in bh.iter() { }
343 let hm: HashMap<(), ()> = HashMap::new();
344 for _v in hm.iter() { }
349 let bt: BTreeMap<(), ()> = BTreeMap::new();
350 for _v in bt.iter() { }
355 let hs: HashSet<()> = HashSet::new();
356 for _v in hs.iter() { }
361 let bs: BTreeSet<()> = BTreeSet::new();
362 for _v in bs.iter() { }
368 for _v in vec.iter().next() { }
370 let u = Unrelated(vec![]);
371 for _v in u.next() { } // no error
372 for _v in u.iter() { } // no error
374 let mut out = vec![];
375 vec.iter().map(|x| out.push(x)).collect::<Vec<_>>();
376 let _y = vec.iter().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
378 // Loop with explicit counter variable
380 for _v in &vec { _index += 1 }
384 for _v in &vec { _index += 1 }
386 // Potential false positives
389 for _v in &vec { _index += 1 }
393 for _v in &vec { _index += 1 }
396 if true { _index = 1 }
397 for _v in &vec { _index += 1 }
401 for _v in &vec { _index += 1 }
404 for _v in &vec { _index += 1; _index += 1 }
407 for _v in &vec { _index *= 2; _index += 1 }
410 for _v in &vec { _index = 1; _index += 1 }
414 for _v in &vec { let mut _index = 0; _index += 1 }
417 for _v in &vec { _index += 1; _index = 0; }
420 for _v in &vec { for _x in 0..1 { _index += 1; }; _index += 1 }
423 for x in &vec { if *x == 1 { _index += 1 } }
426 if true { _index = 1 };
427 for _v in &vec { _index += 1 }
430 if false { _index = 0 };
431 for _v in &vec { _index += 1 }
434 { let mut _x = &mut index; }
435 for _v in &vec { _index += 1 }
438 for _v in &vec { index += 1 }
439 println!("index: {}", index);
441 for_loop_over_option_and_result();
443 let m : HashMap<u64, u64> = HashMap::new();
452 let m : Rc<HashMap<u64, u64>> = Rc::new(HashMap::new());
459 // Here the `*` is not actually necesarry, but the test tests that we don't suggest
460 // `in *m.values()` as we used to
463 let mut m : HashMap<u64, u64> = HashMap::new();
464 for (_, v) in &mut m {
472 let m: &mut HashMap<u64, u64> = &mut HashMap::new();
473 for (_, v) in &mut *m {
481 let m : HashMap<u64, u64> = HashMap::new();
483 for (k, _value) in rm {
494 #[allow(used_underscore_binding)]
495 fn test_for_kv_map() {
496 let m : HashMap<u64, u64> = HashMap::new();
498 // No error, _value is actually used
499 for (k, _value) in &m {