1 use std::collections::*;
4 static STATIC: [usize; 4] = [0, 1, 8, 16];
5 const CONST: [usize; 4] = [0, 1, 8, 16];
8 struct Unrelated(Vec<u8>);
10 fn next(&self) -> std::slice::Iter<u8> {
14 fn iter(&self) -> std::slice::Iter<u8> {
20 clippy::needless_range_loop,
21 clippy::explicit_iter_loop,
22 clippy::explicit_into_iter_loop,
23 clippy::iter_next_loop,
24 clippy::reverse_range_loop,
27 #[warn(clippy::unused_collect)]
30 clippy::shadow_unrelated,
31 clippy::unnecessary_mut_passed,
32 clippy::cognitive_complexity,
35 #[allow(clippy::many_single_char_names, unused_variables, clippy::into_iter_on_array)]
37 const MAX_LEN: usize = 42;
39 let mut vec = vec![1, 2, 3, 4];
40 let vec2 = vec![1, 2, 3, 4];
41 for i in 0..vec.len() {
42 println!("{}", vec[i]);
45 for i in 0..vec.len() {
46 let i = 42; // make a different `i`
47 println!("{}", vec[i]); // ok, not the `i` of the for-loop
50 for i in 0..vec.len() {
56 println!("{:?}", STATIC[j]);
60 println!("{:?}", CONST[j]);
63 for i in 0..vec.len() {
64 println!("{} {}", vec[i], i);
66 for i in 0..vec.len() {
67 // not an error, indexing more than one variable
68 println!("{} {}", vec[i], vec2[i]);
71 for i in 0..vec.len() {
72 println!("{}", vec2[i]);
75 for i in 5..vec.len() {
76 println!("{}", vec[i]);
80 println!("{}", vec[i]);
83 for i in 0..=MAX_LEN {
84 println!("{}", vec[i]);
88 println!("{}", vec[i]);
92 println!("{}", vec[i]);
95 for i in 5..vec.len() {
96 println!("{} {}", vec[i], i);
100 println!("{} {}", vec[i], i);
111 for i in MAX_LEN..0 {
120 // not an error, this is the range with only one element “5”
125 // not an error, the start index is less than the end index
134 for i in (10..0).map(|x| x * 2) {
135 // not an error, it can't be known what arbitrary methods do to a range
139 // testing that the empty range lint folds constants
144 for i in (5 + 2)..(3 - 1) {
148 for i in (5 + 2)..(8 - 1) {
152 for i in (2 * 2)..(2 * 3) {
153 // no error, 4..6 is fine
159 // no error, not constant-foldable
165 // no error, id_col does not exist outside the loop
166 let mut id_col = vec![0f64; 10];
170 for _v in vec.iter() {}
172 for _v in vec.iter_mut() {}
174 let out_vec = vec![1, 2, 3];
175 for _v in out_vec.into_iter() {}
177 let array = [1, 2, 3];
178 for _v in array.into_iter() {}
180 for _v in &vec {} // these are fine
181 for _v in &mut vec {} // these are fine
183 for _v in [1, 2, 3].iter() {}
185 for _v in (&mut [1, 2, 3]).iter() {} // no error
187 for _v in [0; 32].iter() {}
189 for _v in [0; 33].iter() {} // no error
191 let ll: LinkedList<()> = LinkedList::new();
192 for _v in ll.iter() {}
194 let vd: VecDeque<()> = VecDeque::new();
195 for _v in vd.iter() {}
197 let bh: BinaryHeap<()> = BinaryHeap::new();
198 for _v in bh.iter() {}
200 let hm: HashMap<(), ()> = HashMap::new();
201 for _v in hm.iter() {}
203 let bt: BTreeMap<(), ()> = BTreeMap::new();
204 for _v in bt.iter() {}
206 let hs: HashSet<()> = HashSet::new();
207 for _v in hs.iter() {}
209 let bs: BTreeSet<()> = BTreeSet::new();
210 for _v in bs.iter() {}
212 for _v in vec.iter().next() {}
214 let u = Unrelated(vec![]);
215 for _v in u.next() {} // no error
216 for _v in u.iter() {} // no error
218 let mut out = vec![];
219 vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>();
220 let _y = vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
222 // Loop with explicit counter variable
224 // Potential false positives
315 let mut _x = &mut index;
325 println!("index: {}", index);
327 fn f<T>(_: &T, _: &T) -> bool {
330 fn g<T>(_: &mut [T], _: usize, _: usize) {
333 for i in 1..vec.len() {
334 if f(&vec[i - 1], &vec[i]) {
335 g(&mut vec, i - 1, i);
339 for mid in 1..vec.len() {
340 let (_, _) = vec.split_at(mid);
345 fn partition<T: PartialOrd + Send>(v: &mut [T]) -> usize {
346 let pivot = v.len() - 1;
349 if v[j] <= v[pivot] {
358 #[warn(clippy::needless_range_loop)]
359 pub fn manual_copy_same_destination(dst: &mut [i32], d: usize, s: usize) {
360 // Same source and destination - don't trigger lint
361 for i in 0..dst.len() {
362 dst[d + i] = dst[s + i];
368 fn new_for_index(index: usize) -> Self;
369 fn index(&self) -> usize;
372 pub fn test<H: Handle>() -> H {
374 let next_handle = H::new_for_index(x);
375 println!("{}", next_handle.index());