1 #![feature(plugin, 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 #[warn(needless_range_loop, explicit_iter_loop, explicit_into_iter_loop, iter_next_loop, reverse_range_loop, explicit_counter_loop, for_kv_map)]
76 #[warn(unused_collect)]
77 #[allow(linkedlist, shadow_unrelated, unnecessary_mut_passed, cyclomatic_complexity, similar_names)]
78 #[allow(many_single_char_names, unused_variables)]
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
193 for i in x..10 { // no error, not constant-foldable
198 for i in 0..10 { // no error, id_col does not exist outside the loop
199 let mut id_col = vec![0f64; 10];
203 for _v in vec.iter() { }
205 for _v in vec.iter_mut() { }
207 let out_vec = vec![1,2,3];
208 for _v in out_vec.into_iter() { }
210 let array = [1, 2, 3];
211 for _v in array.into_iter() {}
213 for _v in &vec { } // these are fine
214 for _v in &mut vec { } // these are fine
216 for _v in [1, 2, 3].iter() { }
218 for _v in (&mut [1, 2, 3]).iter() { } // no error
220 for _v in [0; 32].iter() {}
222 for _v in [0; 33].iter() {} // no error
224 let ll: LinkedList<()> = LinkedList::new();
225 for _v in ll.iter() { }
227 let vd: VecDeque<()> = VecDeque::new();
228 for _v in vd.iter() { }
230 let bh: BinaryHeap<()> = BinaryHeap::new();
231 for _v in bh.iter() { }
233 let hm: HashMap<(), ()> = HashMap::new();
234 for _v in hm.iter() { }
236 let bt: BTreeMap<(), ()> = BTreeMap::new();
237 for _v in bt.iter() { }
239 let hs: HashSet<()> = HashSet::new();
240 for _v in hs.iter() { }
242 let bs: BTreeSet<()> = BTreeSet::new();
243 for _v in bs.iter() { }
245 for _v in vec.iter().next() { }
247 let u = Unrelated(vec![]);
248 for _v in u.next() { } // no error
249 for _v in u.iter() { } // no error
251 let mut out = vec![];
252 vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>();
253 let _y = vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
255 // Loop with explicit counter variable
257 for _v in &vec { _index += 1 }
261 for _v in &vec { _index += 1 }
263 // Potential false positives
266 for _v in &vec { _index += 1 }
270 for _v in &vec { _index += 1 }
273 if true { _index = 1 }
274 for _v in &vec { _index += 1 }
278 for _v in &vec { _index += 1 }
281 for _v in &vec { _index += 1; _index += 1 }
284 for _v in &vec { _index *= 2; _index += 1 }
287 for _v in &vec { _index = 1; _index += 1 }
291 for _v in &vec { let mut _index = 0; _index += 1 }
294 for _v in &vec { _index += 1; _index = 0; }
297 for _v in &vec { for _x in 0..1 { _index += 1; }; _index += 1 }
300 for x in &vec { if *x == 1 { _index += 1 } }
303 if true { _index = 1 };
304 for _v in &vec { _index += 1 }
307 if false { _index = 0 };
308 for _v in &vec { _index += 1 }
311 { let mut _x = &mut index; }
312 for _v in &vec { _index += 1 }
315 for _v in &vec { index += 1 }
316 println!("index: {}", index);
318 for_loop_over_option_and_result();
320 let m : HashMap<u64, u64> = HashMap::new();
325 let m : Rc<HashMap<u64, u64>> = Rc::new(HashMap::new());
328 // Here the `*` is not actually necesarry, but the test tests that we don't suggest
329 // `in *m.values()` as we used to
332 let mut m : HashMap<u64, u64> = HashMap::new();
333 for (_, v) in &mut m {
337 let m: &mut HashMap<u64, u64> = &mut HashMap::new();
338 for (_, v) in &mut *m {
342 let m : HashMap<u64, u64> = HashMap::new();
344 for (k, _value) in rm {
350 fn f<T>(_: &T, _: &T) -> bool { unimplemented!() }
351 fn g<T>(_: &mut [T], _: usize, _: usize) { unimplemented!() }
352 for i in 1..vec.len() {
353 if f(&vec[i - 1], &vec[i]) {
354 g(&mut vec, i - 1, i);
358 for mid in 1..vec.len() {
359 let (_, _) = vec.split_at(mid);
363 #[allow(used_underscore_binding)]
364 fn test_for_kv_map() {
365 let m : HashMap<u64, u64> = HashMap::new();
367 // No error, _value is actually used
368 for (k, _value) in &m {
375 fn partition<T:PartialOrd+Send>(v: &mut [T]) -> usize {
376 let pivot = v.len() - 1;
379 if v[j] <= v[pivot] {