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
21 // check FOR_LOOP_OVER_RESULT lint
26 for x in option.ok_or("x not found") {
30 // make sure LOOP_OVER_NEXT lint takes precedence when next() is the last call in the chain
31 for x in v.iter().next() {
35 // make sure we lint when next() is not the last call in the chain
36 for x in v.iter().next().and(Some(0)) {
40 for x in v.iter().next().ok_or("x not found") {
44 // check for false positives
46 // for loop false positive
51 // while let false positive for Option
52 while let Some(x) = option {
57 // while let false positive for Result
58 while let Ok(x) = result {
64 struct Unrelated(Vec<u8>);
66 fn next(&self) -> std::slice::Iter<u8> {
70 fn iter(&self) -> std::slice::Iter<u8> {
75 #[deny(needless_range_loop, explicit_iter_loop, explicit_into_iter_loop, iter_next_loop, reverse_range_loop, explicit_counter_loop, for_kv_map)]
76 #[deny(unused_collect)]
77 #[allow(linkedlist, shadow_unrelated, unnecessary_mut_passed, cyclomatic_complexity, similar_names)]
78 #[allow(many_single_char_names)]
80 const MAX_LEN: usize = 42;
82 let mut vec = vec![1, 2, 3, 4];
83 let vec2 = vec![1, 2, 3, 4];
84 for i in 0..vec.len() {
85 println!("{}", vec[i]);
88 for i in 0..vec.len() {
89 let i = 42; // make a different `i`
90 println!("{}", vec[i]); // ok, not the `i` of the for-loop
93 for i in 0..vec.len() { let _ = vec[i]; }
97 println!("{:?}", STATIC[j]);
101 println!("{:?}", CONST[j]);
104 for i in 0..vec.len() {
105 println!("{} {}", vec[i], i);
107 for i in 0..vec.len() { // not an error, indexing more than one variable
108 println!("{} {}", vec[i], vec2[i]);
111 for i in 0..vec.len() {
112 println!("{}", vec2[i]);
115 for i in 5..vec.len() {
116 println!("{}", vec[i]);
119 for i in 0..MAX_LEN {
120 println!("{}", vec[i]);
123 for i in 0...MAX_LEN {
124 println!("{}", vec[i]);
128 println!("{}", vec[i]);
132 println!("{}", vec[i]);
135 for i in 5..vec.len() {
136 println!("{} {}", vec[i], i);
140 println!("{} {}", vec[i], i);
151 for i in MAX_LEN..0 {
159 for i in 5...5 { // not an error, this is the range with only one element “5”
163 for i in 0..10 { // not an error, the start index is less than the end index
167 for i in -10..0 { // not an error
171 for i in (10..0).map(|x| x * 2) { // not an error, it can't be known what arbitrary methods do to a range
175 // testing that the empty range lint folds constants
180 for i in (5+2)..(3-1) {
184 for i in (5+2)..(8-1) {
188 for i in (2*2)..(2*3) { // no error, 4..6 is fine
192 for i in (10..8).step_by(-1) {
197 for i in x..10 { // no error, not constant-foldable
202 for i in 0..10 { // no error, id_col does not exist outside the loop
203 let mut id_col = vec![0f64; 10];
207 for _v in vec.iter() { }
209 for _v in vec.iter_mut() { }
211 let out_vec = vec![1,2,3];
212 for _v in out_vec.into_iter() { }
214 let array = [1, 2, 3];
215 for _v in array.into_iter() {}
217 for _v in &vec { } // these are fine
218 for _v in &mut vec { } // these are fine
220 for _v in [1, 2, 3].iter() { }
222 for _v in (&mut [1, 2, 3]).iter() { } // no error
224 for _v in [0; 32].iter() {}
226 for _v in [0; 33].iter() {} // no error
228 let ll: LinkedList<()> = LinkedList::new();
229 for _v in ll.iter() { }
231 let vd: VecDeque<()> = VecDeque::new();
232 for _v in vd.iter() { }
234 let bh: BinaryHeap<()> = BinaryHeap::new();
235 for _v in bh.iter() { }
237 let hm: HashMap<(), ()> = HashMap::new();
238 for _v in hm.iter() { }
240 let bt: BTreeMap<(), ()> = BTreeMap::new();
241 for _v in bt.iter() { }
243 let hs: HashSet<()> = HashSet::new();
244 for _v in hs.iter() { }
246 let bs: BTreeSet<()> = BTreeSet::new();
247 for _v in bs.iter() { }
249 for _v in vec.iter().next() { }
251 let u = Unrelated(vec![]);
252 for _v in u.next() { } // no error
253 for _v in u.iter() { } // no error
255 let mut out = vec![];
256 vec.iter().map(|x| out.push(x)).collect::<Vec<_>>();
257 let _y = vec.iter().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
259 // Loop with explicit counter variable
261 for _v in &vec { _index += 1 }
265 for _v in &vec { _index += 1 }
267 // Potential false positives
270 for _v in &vec { _index += 1 }
274 for _v in &vec { _index += 1 }
277 if true { _index = 1 }
278 for _v in &vec { _index += 1 }
282 for _v in &vec { _index += 1 }
285 for _v in &vec { _index += 1; _index += 1 }
288 for _v in &vec { _index *= 2; _index += 1 }
291 for _v in &vec { _index = 1; _index += 1 }
295 for _v in &vec { let mut _index = 0; _index += 1 }
298 for _v in &vec { _index += 1; _index = 0; }
301 for _v in &vec { for _x in 0..1 { _index += 1; }; _index += 1 }
304 for x in &vec { if *x == 1 { _index += 1 } }
307 if true { _index = 1 };
308 for _v in &vec { _index += 1 }
311 if false { _index = 0 };
312 for _v in &vec { _index += 1 }
315 { let mut _x = &mut index; }
316 for _v in &vec { _index += 1 }
319 for _v in &vec { index += 1 }
320 println!("index: {}", index);
322 for_loop_over_option_and_result();
324 let m : HashMap<u64, u64> = HashMap::new();
329 let m : Rc<HashMap<u64, u64>> = Rc::new(HashMap::new());
332 // Here the `*` is not actually necesarry, but the test tests that we don't suggest
333 // `in *m.values()` as we used to
336 let mut m : HashMap<u64, u64> = HashMap::new();
337 for (_, v) in &mut m {
341 let m: &mut HashMap<u64, u64> = &mut HashMap::new();
342 for (_, v) in &mut *m {
346 let m : HashMap<u64, u64> = HashMap::new();
348 for (k, _value) in rm {
355 #[allow(used_underscore_binding)]
356 fn test_for_kv_map() {
357 let m : HashMap<u64, u64> = HashMap::new();
359 // No error, _value is actually used
360 for (k, _value) in &m {