]> git.lizzy.rs Git - rust.git/blob - tests/ui/excessive_for_each.rs
New Lint: excessive_for_each
[rust.git] / tests / ui / excessive_for_each.rs
1 #![warn(clippy::excessive_for_each)]
2 #![allow(clippy::needless_return)]
3
4 use std::collections::*;
5
6 fn main() {
7     // Should trigger this lint: Vec.
8     let vec: Vec<i32> = Vec::new();
9     vec.iter().for_each(|v| println!("{}", v));
10
11     // Should trigger this lint: &Vec.
12     let vec_ref = &vec;
13     vec_ref.iter().for_each(|v| println!("{}", v));
14
15     // Should trigger this lint: VecDeque.
16     let vec_deq: VecDeque<i32> = VecDeque::new();
17     vec_deq.iter().for_each(|v| println!("{}", v));
18
19     // Should trigger this lint: LinkedList.
20     let list: LinkedList<i32> = LinkedList::new();
21     list.iter().for_each(|v| println!("{}", v));
22
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));
29
30     // Should trigger this lint: HashSet.
31     let hash_set: HashSet<i32> = HashSet::new();
32     hash_set.iter().for_each(|v| println!("{}", v));
33
34     // Should trigger this lint: BTreeSet.
35     let btree_set: BTreeSet<i32> = BTreeSet::new();
36     btree_set.iter().for_each(|v| println!("{}", v));
37
38     // Should trigger this lint: BinaryHeap.
39     let binary_heap: BinaryHeap<i32> = BinaryHeap::new();
40     binary_heap.iter().for_each(|v| println!("{}", v));
41
42     // Should trigger this lint: Array.
43     let s = [1, 2, 3];
44     s.iter().for_each(|v| println!("{}", v));
45
46     // Should trigger this lint. Slice.
47     vec.as_slice().iter().for_each(|v| println!("{}", v));
48
49     // Should trigger this lint with notes that say "change `return` to `continue`".
50     vec.iter().for_each(|v| {
51         if *v == 10 {
52             return;
53         } else {
54             println!("{}", v);
55         }
56     });
57
58     // Should trigger this lint with notes that say "change `return` to `continue 'outer`".
59     vec.iter().for_each(|v| {
60         for i in 0..*v {
61             if i == 10 {
62                 return;
63             } else {
64                 println!("{}", v);
65             }
66         }
67         if *v == 20 {
68             return;
69         } else {
70             println!("{}", v);
71         }
72     });
73
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));
76
77     // Should NOT trigger this lint in case a `for_each` argument is not closure.
78     fn print(x: &i32) {
79         println!("{}", x);
80     }
81     vec.iter().for_each(print);
82
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));
86 }
87
88 struct MyCollection {
89     v: Vec<i32>,
90 }
91
92 impl MyCollection {
93     fn iter(&self) -> impl Iterator<Item = &i32> {
94         self.v.iter()
95     }
96 }