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)]
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 for _v in &vec {} // these are fine
106 for _v in &mut vec {} // these are fine
108 for _v in &[1, 2, 3] {}
110 for _v in (&mut [1, 2, 3]).iter() {} // no error
112 for _v in &[0; 32] {}
114 for _v in [0; 33].iter() {} // no error
116 let ll: LinkedList<()> = LinkedList::new();
119 let vd: VecDeque<()> = VecDeque::new();
122 let bh: BinaryHeap<()> = BinaryHeap::new();
125 let hm: HashMap<(), ()> = HashMap::new();
128 let bt: BTreeMap<(), ()> = BTreeMap::new();
131 let hs: HashSet<()> = HashSet::new();
134 let bs: BTreeSet<()> = BTreeSet::new();
137 let u = Unrelated(vec![]);
138 for _v in u.next() {} // no error
139 for _v in u.iter() {} // no error
141 let mut out = vec![];
142 vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>();
143 let _y = vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
145 // Loop with explicit counter variable
147 // Potential false positives
238 let mut _x = &mut index;
248 println!("index: {}", index);
250 fn f<T>(_: &T, _: &T) -> bool {
253 fn g<T>(_: &mut [T], _: usize, _: usize) {
256 for i in 1..vec.len() {
257 if f(&vec[i - 1], &vec[i]) {
258 g(&mut vec, i - 1, i);
262 for mid in 1..vec.len() {
263 let (_, _) = vec.split_at(mid);
267 fn partition<T: PartialOrd + Send>(v: &mut [T]) -> usize {
268 let pivot = v.len() - 1;
271 if v[j] <= v[pivot] {
280 #[warn(clippy::needless_range_loop)]
281 pub fn manual_copy_same_destination(dst: &mut [i32], d: usize, s: usize) {
282 // Same source and destination - don't trigger lint
283 for i in 0..dst.len() {
284 dst[d + i] = dst[s + i];
290 fn new_for_index(index: usize) -> Self;
291 fn index(&self) -> usize;
294 pub fn test<H: Handle>() -> H {
296 let next_handle = H::new_for_index(x);
297 println!("{}", next_handle.index());
303 // explicit_into_iter_loop bad suggestions
304 #[warn(clippy::explicit_into_iter_loop, clippy::explicit_iter_loop)]
306 fn takes_iterator<T>(iterator: &T)
308 for<'a> &'a T: IntoIterator<Item = &'a String>,
316 impl IntoIterator for &T {
318 type IntoIter = std::vec::IntoIter<Self::Item>;
319 fn into_iter(self) -> Self::IntoIter {
329 // This case is handled by `explicit_iter_loop`. No idea why.
334 // No suggestion for this.
335 // We'd have to suggest `for _ in *rr {}` which is less clear.
336 for _ in rr.into_iter() {}