4 #![allow(unused, clippy::needless_pass_by_value, clippy::collapsible_if)]
5 #![warn(clippy::map_entry)]
8 use std::collections::HashMap;
12 ($e:expr) => {{ $e }};
16 ($map:expr, $key:expr, $val:expr) => {
17 $map.insert($key, $val)
23 fn hash_map<K: Eq + Hash + Copy, V: Copy>(m: &mut HashMap<K, V>, m2: &mut HashMap<K, V>, k: K, k2: K, v: V, v2: V) {
25 m.entry(k).or_insert(v);
27 // semicolon on insert, use or_insert_with(..)
28 m.entry(k).or_insert_with(|| {
36 // semicolon on if, use or_insert_with(..)
37 m.entry(k).or_insert_with(|| {
45 // early return, use if let
46 if let std::collections::hash_map::Entry::Vacant(e) = m.entry(k) {
55 // use or_insert_with(..)
56 m.entry(k).or_insert_with(|| {
61 // semicolon on insert and match, use or_insert_with(..)
62 m.entry(k).or_insert_with(|| {
73 // one branch doesn't insert, use if let
74 if let std::collections::hash_map::Entry::Vacant(e) = m.entry(k) {
84 m.entry(k).or_insert_with(|| {
110 // ok, insert in loop
111 if !m.contains_key(&k) {
117 // macro_expansion test, use or_insert(..)
118 m.entry(m!(k)).or_insert_with(|| m!(v));
120 // ok, map used before insertion
121 if !m.contains_key(&k) {
127 if !m.contains_key(&k) {
128 unsafe { asm!("nop") }
132 // ok, different keys.
133 if !m.contains_key(&k) {
137 // ok, different maps
138 if !m.contains_key(&k) {
142 // ok, insert in macro
143 if !m.contains_key(&k) {
147 // or_insert_with. Partial move of a local declared in the closure is ok.
148 m.entry(k).or_insert_with(|| {
149 let x = (String::new(), String::new());