]> git.lizzy.rs Git - rust.git/blob - tests/ui/entry.rs
Auto merge of #3635 - matthiaskrgr:revert_random_state_3603, r=xfix
[rust.git] / tests / ui / entry.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![allow(unused, clippy::needless_pass_by_value)]
11 #![warn(clippy::map_entry)]
12
13 use std::collections::{BTreeMap, HashMap};
14 use std::hash::Hash;
15
16 fn foo() {}
17
18 fn insert_if_absent0<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
19     if !m.contains_key(&k) {
20         m.insert(k, v);
21     }
22 }
23
24 fn insert_if_absent1<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
25     if !m.contains_key(&k) {
26         foo();
27         m.insert(k, v);
28     }
29 }
30
31 fn insert_if_absent2<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
32     if !m.contains_key(&k) {
33         m.insert(k, v)
34     } else {
35         None
36     };
37 }
38
39 fn insert_if_present2<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
40     if m.contains_key(&k) {
41         None
42     } else {
43         m.insert(k, v)
44     };
45 }
46
47 fn insert_if_absent3<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
48     if !m.contains_key(&k) {
49         foo();
50         m.insert(k, v)
51     } else {
52         None
53     };
54 }
55
56 fn insert_if_present3<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
57     if m.contains_key(&k) {
58         None
59     } else {
60         foo();
61         m.insert(k, v)
62     };
63 }
64
65 fn insert_in_btreemap<K: Ord, V>(m: &mut BTreeMap<K, V>, k: K, v: V) {
66     if !m.contains_key(&k) {
67         foo();
68         m.insert(k, v)
69     } else {
70         None
71     };
72 }
73
74 fn insert_other_if_absent<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, o: K, v: V) {
75     if !m.contains_key(&k) {
76         m.insert(o, v);
77     }
78 }
79
80 fn main() {}