2022-10-19 13:34:00 -05:00
|
|
|
// needs-asm-support
|
2021-04-22 04:31:13 -05:00
|
|
|
// run-rustfix
|
|
|
|
|
|
|
|
#![allow(unused, clippy::needless_pass_by_value, clippy::collapsible_if)]
|
|
|
|
#![warn(clippy::map_entry)]
|
|
|
|
|
2021-12-12 18:00:51 -06:00
|
|
|
use std::arch::asm;
|
2021-09-08 09:31:47 -05:00
|
|
|
use std::collections::HashMap;
|
2021-04-22 04:31:13 -05:00
|
|
|
use std::hash::Hash;
|
|
|
|
|
|
|
|
macro_rules! m {
|
|
|
|
($e:expr) => {{ $e }};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! insert {
|
|
|
|
($map:expr, $key:expr, $val:expr) => {
|
|
|
|
$map.insert($key, $val)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
// or_insert(v)
|
|
|
|
m.entry(k).or_insert(v);
|
|
|
|
|
|
|
|
// semicolon on insert, use or_insert_with(..)
|
|
|
|
m.entry(k).or_insert_with(|| {
|
|
|
|
if true {
|
|
|
|
v
|
|
|
|
} else {
|
|
|
|
v2
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// semicolon on if, use or_insert_with(..)
|
|
|
|
m.entry(k).or_insert_with(|| {
|
|
|
|
if true {
|
|
|
|
v
|
|
|
|
} else {
|
|
|
|
v2
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// early return, use if let
|
|
|
|
if let std::collections::hash_map::Entry::Vacant(e) = m.entry(k) {
|
|
|
|
if true {
|
|
|
|
e.insert(v);
|
|
|
|
} else {
|
|
|
|
e.insert(v2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// use or_insert_with(..)
|
|
|
|
m.entry(k).or_insert_with(|| {
|
|
|
|
foo();
|
|
|
|
v
|
|
|
|
});
|
|
|
|
|
|
|
|
// semicolon on insert and match, use or_insert_with(..)
|
|
|
|
m.entry(k).or_insert_with(|| {
|
|
|
|
match 0 {
|
|
|
|
1 if true => {
|
|
|
|
v
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
v2
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// one branch doesn't insert, use if let
|
|
|
|
if let std::collections::hash_map::Entry::Vacant(e) = m.entry(k) {
|
|
|
|
match 0 {
|
|
|
|
0 => foo(),
|
|
|
|
_ => {
|
|
|
|
e.insert(v2);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// use or_insert_with
|
|
|
|
m.entry(k).or_insert_with(|| {
|
|
|
|
foo();
|
|
|
|
match 0 {
|
|
|
|
0 if false => {
|
|
|
|
v
|
|
|
|
},
|
|
|
|
1 => {
|
|
|
|
foo();
|
|
|
|
v
|
|
|
|
},
|
|
|
|
2 | 3 => {
|
|
|
|
for _ in 0..2 {
|
|
|
|
foo();
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
v
|
|
|
|
} else {
|
|
|
|
v2
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
v2
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// ok, insert in loop
|
|
|
|
if !m.contains_key(&k) {
|
|
|
|
for _ in 0..2 {
|
|
|
|
m.insert(k, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// macro_expansion test, use or_insert(..)
|
|
|
|
m.entry(m!(k)).or_insert_with(|| m!(v));
|
|
|
|
|
|
|
|
// ok, map used before insertion
|
|
|
|
if !m.contains_key(&k) {
|
|
|
|
let _ = m.len();
|
|
|
|
m.insert(k, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, inline asm
|
|
|
|
if !m.contains_key(&k) {
|
|
|
|
unsafe { asm!("nop") }
|
|
|
|
m.insert(k, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, different keys.
|
|
|
|
if !m.contains_key(&k) {
|
|
|
|
m.insert(k2, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, different maps
|
|
|
|
if !m.contains_key(&k) {
|
|
|
|
m2.insert(k, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, insert in macro
|
|
|
|
if !m.contains_key(&k) {
|
|
|
|
insert!(m, k, v);
|
|
|
|
}
|
|
|
|
|
2021-09-08 09:31:47 -05:00
|
|
|
// or_insert_with. Partial move of a local declared in the closure is ok.
|
|
|
|
m.entry(k).or_insert_with(|| {
|
|
|
|
let x = (String::new(), String::new());
|
|
|
|
let _ = x.0;
|
|
|
|
v
|
|
|
|
});
|
2021-04-22 04:31:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|