1 #![warn(clippy::excessive_for_each)]
2 #![allow(clippy::needless_return)]
4 use std::collections::*;
7 // Should trigger this lint: Vec.
8 let vec: Vec<i32> = Vec::new();
9 vec.iter().for_each(|v| println!("{}", v));
11 // Should trigger this lint: &Vec.
13 vec_ref.iter().for_each(|v| println!("{}", v));
15 // Should trigger this lint: VecDeque.
16 let vec_deq: VecDeque<i32> = VecDeque::new();
17 vec_deq.iter().for_each(|v| println!("{}", v));
19 // Should trigger this lint: LinkedList.
20 let list: LinkedList<i32> = LinkedList::new();
21 list.iter().for_each(|v| println!("{}", v));
23 // Should trigger this lint: HashMap.
24 let mut hash_map: HashMap<i32, i32> = HashMap::new();
25 hash_map.iter().for_each(|(k, v)| println!("{}: {}", k, v));
26 hash_map.iter_mut().for_each(|(k, v)| println!("{}: {}", k, v));
27 hash_map.keys().for_each(|k| println!("{}", k));
28 hash_map.values().for_each(|v| println!("{}", v));
30 // Should trigger this lint: HashSet.
31 let hash_set: HashSet<i32> = HashSet::new();
32 hash_set.iter().for_each(|v| println!("{}", v));
34 // Should trigger this lint: BTreeSet.
35 let btree_set: BTreeSet<i32> = BTreeSet::new();
36 btree_set.iter().for_each(|v| println!("{}", v));
38 // Should trigger this lint: BinaryHeap.
39 let binary_heap: BinaryHeap<i32> = BinaryHeap::new();
40 binary_heap.iter().for_each(|v| println!("{}", v));
42 // Should trigger this lint: Array.
44 s.iter().for_each(|v| println!("{}", v));
46 // Should trigger this lint. Slice.
47 vec.as_slice().iter().for_each(|v| println!("{}", v));
49 // Should trigger this lint with notes that say "change `return` to `continue`".
50 vec.iter().for_each(|v| {
58 // Should trigger this lint with notes that say "change `return` to `continue 'outer`".
59 vec.iter().for_each(|v| {
74 // Should NOT trigger this lint in case `for_each` follows long iterator chain.
75 vec.iter().chain(vec.iter()).for_each(|v| println!("{}", v));
77 // Should NOT trigger this lint in case a `for_each` argument is not closure.
81 vec.iter().for_each(print);
83 // Should NOT trigger this lint in case the receiver of `iter` is a user defined type.
84 let my_collection = MyCollection { v: vec![] };
85 my_collection.iter().for_each(|v| println!("{}", v));
93 fn iter(&self) -> impl Iterator<Item = &i32> {