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 for _v in &vec { } // these are fine
306 for _v in &mut vec { } // these are fine
308 for _v in [1, 2, 3].iter() { }
313 for _v in (&mut [1, 2, 3]).iter() { } // no error
315 for _v in [0; 32].iter() {}
320 for _v in [0; 33].iter() {} // no error
322 let ll: LinkedList<()> = LinkedList::new();
323 for _v in ll.iter() { }
328 let vd: VecDeque<()> = VecDeque::new();
329 for _v in vd.iter() { }
334 let bh: BinaryHeap<()> = BinaryHeap::new();
335 for _v in bh.iter() { }
340 let hm: HashMap<(), ()> = HashMap::new();
341 for _v in hm.iter() { }
346 let bt: BTreeMap<(), ()> = BTreeMap::new();
347 for _v in bt.iter() { }
352 let hs: HashSet<()> = HashSet::new();
353 for _v in hs.iter() { }
358 let bs: BTreeSet<()> = BTreeSet::new();
359 for _v in bs.iter() { }
365 for _v in vec.iter().next() { }
367 let u = Unrelated(vec![]);
368 for _v in u.next() { } // no error
369 for _v in u.iter() { } // no error
371 let mut out = vec![];
372 vec.iter().map(|x| out.push(x)).collect::<Vec<_>>();
373 let _y = vec.iter().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
375 // Loop with explicit counter variable
377 for _v in &vec { _index += 1 }
381 for _v in &vec { _index += 1 }
383 // Potential false positives
386 for _v in &vec { _index += 1 }
390 for _v in &vec { _index += 1 }
393 if true { _index = 1 }
394 for _v in &vec { _index += 1 }
398 for _v in &vec { _index += 1 }
401 for _v in &vec { _index += 1; _index += 1 }
404 for _v in &vec { _index *= 2; _index += 1 }
407 for _v in &vec { _index = 1; _index += 1 }
411 for _v in &vec { let mut _index = 0; _index += 1 }
414 for _v in &vec { _index += 1; _index = 0; }
417 for _v in &vec { for _x in 0..1 { _index += 1; }; _index += 1 }
420 for x in &vec { if *x == 1 { _index += 1 } }
423 if true { _index = 1 };
424 for _v in &vec { _index += 1 }
427 if false { _index = 0 };
428 for _v in &vec { _index += 1 }
431 { let mut _x = &mut index; }
432 for _v in &vec { _index += 1 }
435 for _v in &vec { index += 1 }
436 println!("index: {}", index);
438 for_loop_over_option_and_result();
440 let m : HashMap<u64, u64> = HashMap::new();
449 let m : Rc<HashMap<u64, u64>> = Rc::new(HashMap::new());
456 // Here the `*` is not actually necesarry, but the test tests that we don't suggest
457 // `in *m.values()` as we used to
460 let mut m : HashMap<u64, u64> = HashMap::new();
461 for (_, v) in &mut m {
469 let m: &mut HashMap<u64, u64> = &mut HashMap::new();
470 for (_, v) in &mut *m {
478 let m : HashMap<u64, u64> = HashMap::new();
480 for (k, _value) in rm {
491 #[allow(used_underscore_binding)]
492 fn test_for_kv_map() {
493 let m : HashMap<u64, u64> = HashMap::new();
495 // No error, _value is actually used
496 for (k, _value) in &m {