1 #![feature(plugin, step_by, inclusive_range_syntax)]
4 use std::collections::*;
6 static STATIC: [usize; 4] = [ 0, 1, 8, 16 ];
7 const CONST: [usize; 4] = [ 0, 1, 8, 16 ];
10 fn for_loop_over_option_and_result() {
12 let result = option.ok_or("x not found");
15 // check FOR_LOOP_OVER_OPTION lint
18 //~^ ERROR for loop over `option`, which is an `Option`.
19 //~| HELP consider replacing `for x in option` with `if let Some(x) = option`
23 // check FOR_LOOP_OVER_RESULT lint
26 //~^ ERROR for loop over `result`, which is a `Result`.
27 //~| HELP consider replacing `for x in result` with `if let Ok(x) = result`
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")`
37 // make sure LOOP_OVER_NEXT lint takes precedence when next() is the last call in the chain
39 for x in v.iter().next() {
40 //~^ ERROR you are iterating over `Iterator::next()` which is an Option
44 // make sure we lint when next() is not the last call in the chain
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))`
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")`
58 // check for false positives
60 // for loop false positive
65 // while let false positive for Option
66 while let Some(x) = option {
71 // while let false positive for Result
72 while let Ok(x) = result {
78 struct Unrelated(Vec<u8>);
80 fn next(&self) -> std::slice::Iter<u8> {
84 fn iter(&self) -> std::slice::Iter<u8> {
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)]
94 const MAX_LEN: usize = 42;
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]);
105 //~^ ERROR `j` is only used to index `STATIC`
106 println!("{:?}", STATIC[j]);
110 //~^ ERROR `j` is only used to index `CONST`
111 println!("{:?}", CONST[j]);
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);
118 for i in 0..vec.len() { // not an error, indexing more than one variable
119 println!("{} {}", vec[i], vec2[i]);
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]);
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]);
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]);
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]);
143 //~^ ERROR `i` is only used to index `vec`. Consider using `for item in vec.iter().take(10).skip(5)`
144 println!("{}", vec[i]);
148 //~^ ERROR `i` is only used to index `vec`. Consider using `for item in vec.iter().take(10).skip(5)`
149 println!("{}", vec[i]);
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);
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);
163 //~^ERROR this range is empty so this for loop will never run
165 //~|SUGGESTION (0..10).rev()
170 //~^ERROR this range is empty so this for loop will never run
172 //~|SUGGESTION (0..10).rev()
176 for i in MAX_LEN..0 { //~ERROR this range is empty so this for loop will never run
178 //~|SUGGESTION (0..MAX_LEN).rev()
182 for i in 5..5 { //~ERROR this range is empty so this for loop will never run
186 for i in 5...5 { // not an error, this is the range with only one element “5”
190 for i in 0..10 { // not an error, the start index is less than the end index
194 for i in -10..0 { // not an error
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
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
207 for i in (5+2)..(3-1) { //~ERROR this range is empty so this for loop will never run
211 for i in (5+2)..(8-1) { //~ERROR this range is empty so this for loop will never run
215 for i in (2*2)..(2*3) { // no error, 4..6 is fine
219 for i in (10..8).step_by(-1) {
224 for i in x..10 { // no error, not constant-foldable
229 for i in 0..10 { // no error, id_col does not exist outside the loop
230 let mut id_col = vec![0f64; 10];
235 for i in (10..0).map(|x| x * 2) {
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`
242 for _v in &vec { } // these are fine
243 for _v in &mut vec { } // these are fine
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`
264 for _v in vec.iter().next() { } //~ERROR you are iterating over `Iterator::next()`
266 let u = Unrelated(vec![]);
267 for _v in u.next() { } // no error
268 for _v in u.iter() { } // no error
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
274 // Loop with explicit counter variable
276 for _v in &vec { _index += 1 } //~ERROR the variable `_index` is used as a loop counter
280 for _v in &vec { _index += 1 } //~ERROR the variable `_index` is used as a loop counter
282 // Potential false positives
285 for _v in &vec { _index += 1 }
289 for _v in &vec { _index += 1 }
292 if true { _index = 1 }
293 for _v in &vec { _index += 1 }
297 for _v in &vec { _index += 1 }
300 for _v in &vec { _index += 1; _index += 1 }
303 for _v in &vec { _index *= 2; _index += 1 }
306 for _v in &vec { _index = 1; _index += 1 }
310 for _v in &vec { let mut _index = 0; _index += 1 }
313 for _v in &vec { _index += 1; _index = 0; }
316 for _v in &vec { for _x in 0..1 { _index += 1; }; _index += 1 }
319 for x in &vec { if *x == 1 { _index += 1 } }
322 if true { _index = 1 };
323 for _v in &vec { _index += 1 }
326 if false { _index = 0 };
327 for _v in &vec { _index += 1 }
330 { let mut _x = &mut index; }
331 for _v in &vec { _index += 1 }
334 for _v in &vec { index += 1 }
335 println!("index: {}", index);
337 for_loop_over_option_and_result();
339 let m : HashMap<u64, u64> = HashMap::new();
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()
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
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()
365 #[allow(used_underscore_binding)]
366 fn test_for_kv_map() {
367 let m : HashMap<u64, u64> = HashMap::new();
369 // No error, _value is actually used
370 for (k, _value) in &m {