]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/entry_unfixable.rs
Rollup merge of #76718 - poliorcetics:vec-ui-to-unit-test, r=jyn514
[rust.git] / src / tools / clippy / tests / ui / entry_unfixable.rs
1 #![allow(unused, clippy::needless_pass_by_value)]
2 #![warn(clippy::map_entry)]
3
4 use std::collections::{BTreeMap, HashMap};
5 use std::hash::Hash;
6
7 fn foo() {}
8
9 fn insert_if_absent2<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
10     if !m.contains_key(&k) {
11         m.insert(k, v)
12     } else {
13         None
14     };
15 }
16
17 fn insert_if_present2<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
18     if m.contains_key(&k) {
19         None
20     } else {
21         m.insert(k, v)
22     };
23 }
24
25 fn insert_if_absent3<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
26     if !m.contains_key(&k) {
27         foo();
28         m.insert(k, v)
29     } else {
30         None
31     };
32 }
33
34 fn insert_if_present3<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
35     if m.contains_key(&k) {
36         None
37     } else {
38         foo();
39         m.insert(k, v)
40     };
41 }
42
43 fn insert_in_btreemap<K: Ord, V>(m: &mut BTreeMap<K, V>, k: K, v: V) {
44     if !m.contains_key(&k) {
45         foo();
46         m.insert(k, v)
47     } else {
48         None
49     };
50 }
51
52 // should not trigger
53 fn insert_other_if_absent<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, o: K, v: V) {
54     if !m.contains_key(&k) {
55         m.insert(o, v);
56     }
57 }
58
59 // should not trigger, because the one uses different HashMap from another one
60 fn insert_from_different_map<K: Eq + Hash, V>(m: HashMap<K, V>, n: &mut HashMap<K, V>, k: K, v: V) {
61     if !m.contains_key(&k) {
62         n.insert(k, v);
63     }
64 }
65
66 // should not trigger, because the one uses different HashMap from another one
67 fn insert_from_different_map2<K: Eq + Hash, V>(m: &mut HashMap<K, V>, n: &mut HashMap<K, V>, k: K, v: V) {
68     if !m.contains_key(&k) {
69         n.insert(k, v);
70     }
71 }
72
73 fn main() {}