1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 use std::collections::*;
13 static STATIC: [usize; 4] = [0, 1, 8, 16];
14 const CONST: [usize; 4] = [0, 1, 8, 16];
17 struct Unrelated(Vec<u8>);
19 fn next(&self) -> std::slice::Iter<u8> {
23 fn iter(&self) -> std::slice::Iter<u8> {
29 clippy::needless_range_loop,
30 clippy::explicit_iter_loop,
31 clippy::explicit_into_iter_loop,
32 clippy::iter_next_loop,
33 clippy::reverse_range_loop,
36 #[warn(clippy::unused_collect)]
39 clippy::shadow_unrelated,
40 clippy::unnecessary_mut_passed,
41 clippy::cyclomatic_complexity,
44 #[allow(clippy::many_single_char_names, unused_variables, clippy::into_iter_on_array)]
46 const MAX_LEN: usize = 42;
48 let mut vec = vec![1, 2, 3, 4];
49 let vec2 = vec![1, 2, 3, 4];
50 for i in 0..vec.len() {
51 println!("{}", vec[i]);
54 for i in 0..vec.len() {
55 let i = 42; // make a different `i`
56 println!("{}", vec[i]); // ok, not the `i` of the for-loop
59 for i in 0..vec.len() {
65 println!("{:?}", STATIC[j]);
69 println!("{:?}", CONST[j]);
72 for i in 0..vec.len() {
73 println!("{} {}", vec[i], i);
75 for i in 0..vec.len() {
76 // not an error, indexing more than one variable
77 println!("{} {}", vec[i], vec2[i]);
80 for i in 0..vec.len() {
81 println!("{}", vec2[i]);
84 for i in 5..vec.len() {
85 println!("{}", vec[i]);
89 println!("{}", vec[i]);
92 for i in 0..=MAX_LEN {
93 println!("{}", vec[i]);
97 println!("{}", vec[i]);
101 println!("{}", vec[i]);
104 for i in 5..vec.len() {
105 println!("{} {}", vec[i], i);
109 println!("{} {}", vec[i], i);
120 for i in MAX_LEN..0 {
129 // not an error, this is the range with only one element “5”
134 // not an error, the start index is less than the end index
143 for i in (10..0).map(|x| x * 2) {
144 // not an error, it can't be known what arbitrary methods do to a range
148 // testing that the empty range lint folds constants
153 for i in (5 + 2)..(3 - 1) {
157 for i in (5 + 2)..(8 - 1) {
161 for i in (2 * 2)..(2 * 3) {
162 // no error, 4..6 is fine
168 // no error, not constant-foldable
174 // no error, id_col does not exist outside the loop
175 let mut id_col = vec![0f64; 10];
179 for _v in vec.iter() {}
181 for _v in vec.iter_mut() {}
183 let out_vec = vec![1, 2, 3];
184 for _v in out_vec.into_iter() {}
186 let array = [1, 2, 3];
187 for _v in array.into_iter() {}
189 for _v in &vec {} // these are fine
190 for _v in &mut vec {} // these are fine
192 for _v in [1, 2, 3].iter() {}
194 for _v in (&mut [1, 2, 3]).iter() {} // no error
196 for _v in [0; 32].iter() {}
198 for _v in [0; 33].iter() {} // no error
200 let ll: LinkedList<()> = LinkedList::new();
201 for _v in ll.iter() {}
203 let vd: VecDeque<()> = VecDeque::new();
204 for _v in vd.iter() {}
206 let bh: BinaryHeap<()> = BinaryHeap::new();
207 for _v in bh.iter() {}
209 let hm: HashMap<(), ()> = HashMap::new();
210 for _v in hm.iter() {}
212 let bt: BTreeMap<(), ()> = BTreeMap::new();
213 for _v in bt.iter() {}
215 let hs: HashSet<()> = HashSet::new();
216 for _v in hs.iter() {}
218 let bs: BTreeSet<()> = BTreeSet::new();
219 for _v in bs.iter() {}
221 for _v in vec.iter().next() {}
223 let u = Unrelated(vec![]);
224 for _v in u.next() {} // no error
225 for _v in u.iter() {} // no error
227 let mut out = vec![];
228 vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>();
229 let _y = vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
231 // Loop with explicit counter variable
233 // Potential false positives
324 let mut _x = &mut index;
334 println!("index: {}", index);
336 let m: HashMap<u64, u64> = HashMap::new();
341 let m: Rc<HashMap<u64, u64>> = Rc::new(HashMap::new());
344 // Here the `*` is not actually necessary, but the test tests that we don't
346 // `in *m.values()` as we used to
349 let mut m: HashMap<u64, u64> = HashMap::new();
350 for (_, v) in &mut m {
354 let m: &mut HashMap<u64, u64> = &mut HashMap::new();
355 for (_, v) in &mut *m {
359 let m: HashMap<u64, u64> = HashMap::new();
361 for (k, _value) in rm {
367 fn f<T>(_: &T, _: &T) -> bool {
370 fn g<T>(_: &mut [T], _: usize, _: usize) {
373 for i in 1..vec.len() {
374 if f(&vec[i - 1], &vec[i]) {
375 g(&mut vec, i - 1, i);
379 for mid in 1..vec.len() {
380 let (_, _) = vec.split_at(mid);
384 #[allow(clippy::used_underscore_binding)]
385 fn test_for_kv_map() {
386 let m: HashMap<u64, u64> = HashMap::new();
388 // No error, _value is actually used
389 for (k, _value) in &m {
396 fn partition<T: PartialOrd + Send>(v: &mut [T]) -> usize {
397 let pivot = v.len() - 1;
400 if v[j] <= v[pivot] {
409 const LOOP_OFFSET: usize = 5000;
411 #[warn(clippy::needless_range_loop)]
412 pub fn manual_copy(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) {
413 // plain manual memcpy
414 for i in 0..src.len() {
419 for i in 0..src.len() {
420 dst[i + 10] = src[i];
424 for i in 0..src.len() {
425 dst[i] = src[i + 10];
429 for i in 11..src.len() {
430 dst[i] = src[i - 10];
433 // overwrite entire dst
434 for i in 0..dst.len() {
438 // manual copy with branch - can't easily convert to memcpy!
439 for i in 0..src.len() {
446 // multiple copies - suggest two memcpy statements
449 dst2[i + 500] = src[i]
452 // this is a reversal - the copy lint shouldn't be triggered
453 for i in 10..LOOP_OFFSET {
454 dst[i + LOOP_OFFSET] = src[LOOP_OFFSET - i];
458 // Offset in variable
459 for i in 10..LOOP_OFFSET {
460 dst[i + LOOP_OFFSET] = src[i - some_var];
463 // Non continuous copy - don't trigger lint
468 let src_vec = vec![1, 2, 3, 4, 5];
469 let mut dst_vec = vec![0, 0, 0, 0, 0];
471 // make sure vectors are supported
472 for i in 0..src_vec.len() {
473 dst_vec[i] = src_vec[i];
476 // lint should not trigger when either
477 // source or destination type is not
478 // slice-like, like DummyStruct
479 struct DummyStruct(i32);
481 impl ::std::ops::Index<usize> for DummyStruct {
484 fn index(&self, _: usize) -> &i32 {
489 let src = DummyStruct(5);
490 let mut dst_vec = vec![0; 10];
496 // Simplify suggestion (issue #3004)
497 let src = [0, 1, 2, 3, 4];
498 let mut dst = [0, 0, 0, 0, 0, 0];
501 for i in from..from + src.len() {
502 dst[i] = src[i - from];
505 for i in from..from + 3 {
506 dst[i] = src[i - from];
510 #[warn(clippy::needless_range_loop)]
511 pub fn manual_clone(src: &[String], dst: &mut [String]) {
512 for i in 0..src.len() {
513 dst[i] = src[i].clone();
517 #[warn(clippy::needless_range_loop)]
518 pub fn manual_copy_same_destination(dst: &mut [i32], d: usize, s: usize) {
519 // Same source and destination - don't trigger lint
520 for i in 0..dst.len() {
521 dst[d + i] = dst[s + i];
527 fn new_for_index(index: usize) -> Self;
528 fn index(&self) -> usize;
531 pub fn test<H: Handle>() -> H {
533 let next_handle = H::new_for_index(x);
534 println!("{}", next_handle.index());