3 #![allow(dead_code, unused)]
5 use std::collections::*;
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,
29 clippy::shadow_unrelated,
30 clippy::unnecessary_mut_passed,
31 clippy::cognitive_complexity,
34 #[allow(clippy::many_single_char_names, unused_variables, clippy::into_iter_on_array)]
36 const MAX_LEN: usize = 42;
37 let mut vec = vec![1, 2, 3, 4];
39 for i in (0..10).rev() {
43 for i in (0..=10).rev() {
47 for i in (0..MAX_LEN).rev() {
52 // not an error, this is the range with only one element “5”
57 // not an error, the start index is less than the end index
66 for i in (10..0).map(|x| x * 2) {
67 // not an error, it can't be known what arbitrary methods do to a range
71 // testing that the empty range lint folds constants
72 for i in (5 + 4..10).rev() {
76 for i in ((3 - 1)..(5 + 2)).rev() {
80 for i in (2 * 2)..(2 * 3) {
81 // no error, 4..6 is fine
87 // no error, not constant-foldable
93 // no error, id_col does not exist outside the loop
94 let mut id_col = vec![0f64; 10];
100 for _v in &mut vec {}
102 let out_vec = vec![1, 2, 3];
105 let array = [1, 2, 3];
108 for _v in &vec {} // these are fine
109 for _v in &mut vec {} // these are fine
111 for _v in &[1, 2, 3] {}
113 for _v in (&mut [1, 2, 3]).iter() {} // no error
115 for _v in &[0; 32] {}
117 for _v in [0; 33].iter() {} // no error
119 let ll: LinkedList<()> = LinkedList::new();
122 let vd: VecDeque<()> = VecDeque::new();
125 let bh: BinaryHeap<()> = BinaryHeap::new();
128 let hm: HashMap<(), ()> = HashMap::new();
131 let bt: BTreeMap<(), ()> = BTreeMap::new();
134 let hs: HashSet<()> = HashSet::new();
137 let bs: BTreeSet<()> = BTreeSet::new();
140 let u = Unrelated(vec![]);
141 for _v in u.next() {} // no error
142 for _v in u.iter() {} // no error
144 let mut out = vec![];
145 vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>();
146 let _y = vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
148 // Loop with explicit counter variable
150 // Potential false positives
241 let mut _x = &mut index;
251 println!("index: {}", index);
253 fn f<T>(_: &T, _: &T) -> bool {
256 fn g<T>(_: &mut [T], _: usize, _: usize) {
259 for i in 1..vec.len() {
260 if f(&vec[i - 1], &vec[i]) {
261 g(&mut vec, i - 1, i);
265 for mid in 1..vec.len() {
266 let (_, _) = vec.split_at(mid);
270 fn partition<T: PartialOrd + Send>(v: &mut [T]) -> usize {
271 let pivot = v.len() - 1;
274 if v[j] <= v[pivot] {
283 #[warn(clippy::needless_range_loop)]
284 pub fn manual_copy_same_destination(dst: &mut [i32], d: usize, s: usize) {
285 // Same source and destination - don't trigger lint
286 for i in 0..dst.len() {
287 dst[d + i] = dst[s + i];
293 fn new_for_index(index: usize) -> Self;
294 fn index(&self) -> usize;
297 pub fn test<H: Handle>() -> H {
299 let next_handle = H::new_for_index(x);
300 println!("{}", next_handle.index());