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,
26 #[allow(clippy::linkedlist, clippy::unnecessary_mut_passed, clippy::similar_names)]
27 #[allow(unused_variables)]
29 let mut vec = vec![1, 2, 3, 4];
33 // no error, id_col does not exist outside the loop
34 let mut id_col = vec![0f64; 10];
42 let out_vec = vec![1, 2, 3];
45 for _v in &vec {} // these are fine
46 for _v in &mut vec {} // these are fine
48 for _v in &[1, 2, 3] {}
50 for _v in (&mut [1, 2, 3]).iter() {} // no error
54 for _v in [0; 33].iter() {} // no error
56 let ll: LinkedList<()> = LinkedList::new();
59 let vd: VecDeque<()> = VecDeque::new();
62 let bh: BinaryHeap<()> = BinaryHeap::new();
65 let hm: HashMap<(), ()> = HashMap::new();
68 let bt: BTreeMap<(), ()> = BTreeMap::new();
71 let hs: HashSet<()> = HashSet::new();
74 let bs: BTreeSet<()> = BTreeSet::new();
77 let u = Unrelated(vec![]);
78 for _v in u.next() {} // no error
79 for _v in u.iter() {} // no error
82 vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>();
83 let _y = vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
85 // Loop with explicit counter variable
87 // Potential false positives
178 let mut _x = &mut index;
188 println!("index: {}", index);
190 fn f<T>(_: &T, _: &T) -> bool {
193 fn g<T>(_: &mut [T], _: usize, _: usize) {
196 for i in 1..vec.len() {
197 if f(&vec[i - 1], &vec[i]) {
198 g(&mut vec, i - 1, i);
202 for mid in 1..vec.len() {
203 let (_, _) = vec.split_at(mid);
207 fn partition<T: PartialOrd + Send>(v: &mut [T]) -> usize {
208 let pivot = v.len() - 1;
211 if v[j] <= v[pivot] {
220 #[warn(clippy::needless_range_loop)]
221 pub fn manual_copy_same_destination(dst: &mut [i32], d: usize, s: usize) {
222 // Same source and destination - don't trigger lint
223 for i in 0..dst.len() {
224 dst[d + i] = dst[s + i];
230 fn new_for_index(index: usize) -> Self;
231 fn index(&self) -> usize;
234 pub fn test<H: Handle>() -> H {
236 let next_handle = H::new_for_index(x);
237 println!("{}", next_handle.index());
243 // explicit_into_iter_loop bad suggestions
244 #[warn(clippy::explicit_into_iter_loop, clippy::explicit_iter_loop)]
246 fn takes_iterator<T>(iterator: &T)
248 for<'a> &'a T: IntoIterator<Item = &'a String>,
256 impl IntoIterator for &T {
258 type IntoIter = std::vec::IntoIter<Self::Item>;
259 fn into_iter(self) -> Self::IntoIter {
269 // This case is handled by `explicit_iter_loop`. No idea why.
274 // No suggestion for this.
275 // We'd have to suggest `for _ in *rr {}` which is less clear.
276 for _ in rr.into_iter() {}
280 // explicit_into_iter_loop
281 #[warn(clippy::explicit_into_iter_loop)]
285 #[allow(clippy::should_implement_trait)]
286 pub fn into_iter<T>(self) -> I<T> {
292 impl<T> Iterator for I<T> {
294 fn next(&mut self) -> Option<Self::Item> {
300 for _ in S.into_iter::<u32>() {