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
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() {
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() {
217 for i in (5+2)..(8-1) { //~ERROR this range is empty so this for loop will never run
221 for i in (2*2)..(2*3) { // no error, 4..6 is fine
225 for i in (10..8).step_by(-1) {
230 for i in x..10 { // no error, not constant-foldable
235 for i in 0..10 { // no error, id_col does not exist outside the loop
236 let mut id_col = vec![0f64; 10];
241 for i in (10..0).map(|x| x * 2) {
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`
248 for _v in &vec { } // these are fine
249 for _v in &mut vec { } // these are fine
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`
270 for _v in vec.iter().next() { } //~ERROR you are iterating over `Iterator::next()`
272 let u = Unrelated(vec![]);
273 for _v in u.next() { } // no error
274 for _v in u.iter() { } // no error
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
280 // Loop with explicit counter variable
282 for _v in &vec { _index += 1 } //~ERROR the variable `_index` is used as a loop counter
286 for _v in &vec { _index += 1 } //~ERROR the variable `_index` is used as a loop counter
288 // Potential false positives
291 for _v in &vec { _index += 1 }
295 for _v in &vec { _index += 1 }
298 if true { _index = 1 }
299 for _v in &vec { _index += 1 }
303 for _v in &vec { _index += 1 }
306 for _v in &vec { _index += 1; _index += 1 }
309 for _v in &vec { _index *= 2; _index += 1 }
312 for _v in &vec { _index = 1; _index += 1 }
316 for _v in &vec { let mut _index = 0; _index += 1 }
319 for _v in &vec { _index += 1; _index = 0; }
322 for _v in &vec { for _x in 0..1 { _index += 1; }; _index += 1 }
325 for x in &vec { if *x == 1 { _index += 1 } }
328 if true { _index = 1 };
329 for _v in &vec { _index += 1 }
332 if false { _index = 0 };
333 for _v in &vec { _index += 1 }
336 { let mut _x = &mut index; }
337 for _v in &vec { _index += 1 }
340 for _v in &vec { index += 1 }
341 println!("index: {}", index);
343 for_loop_over_option_and_result();
345 let m : HashMap<u64, u64> = HashMap::new();
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()
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
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()
371 #[allow(used_underscore_binding)]
372 fn test_for_kv_map() {
373 let m : HashMap<u64, u64> = HashMap::new();
375 // No error, _value is actually used
376 for (k, _value) in &m {