mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-16 10:35:22 +00:00
3323ff7145
Suggest using `or_insert_with` when possible
108 lines
2.0 KiB
Rust
108 lines
2.0 KiB
Rust
// run-rustfix
|
|
|
|
#![allow(unused, clippy::needless_pass_by_value, clippy::collapsible_if)]
|
|
#![warn(clippy::map_entry)]
|
|
|
|
use std::collections::{BTreeMap, HashMap};
|
|
use std::hash::Hash;
|
|
|
|
macro_rules! m {
|
|
($e:expr) => {{ $e }};
|
|
}
|
|
|
|
fn foo() {}
|
|
|
|
fn hash_map<K: Eq + Hash + Copy, V: Copy>(m: &mut HashMap<K, V>, k: K, v: V, v2: V) {
|
|
m.entry(k).or_insert(v);
|
|
|
|
m.entry(k).or_insert_with(|| {
|
|
if true {
|
|
v
|
|
} else {
|
|
v2
|
|
}
|
|
});
|
|
|
|
m.entry(k).or_insert_with(|| {
|
|
if true {
|
|
v
|
|
} else {
|
|
v2
|
|
}
|
|
});
|
|
|
|
if let std::collections::hash_map::Entry::Vacant(e) = m.entry(k) {
|
|
if true {
|
|
e.insert(v);
|
|
} else {
|
|
e.insert(v2);
|
|
return;
|
|
}
|
|
}
|
|
|
|
m.entry(k).or_insert_with(|| {
|
|
foo();
|
|
v
|
|
});
|
|
|
|
m.entry(k).or_insert_with(|| {
|
|
match 0 {
|
|
1 if true => {
|
|
v
|
|
},
|
|
_ => {
|
|
v2
|
|
},
|
|
}
|
|
});
|
|
|
|
if let std::collections::hash_map::Entry::Vacant(e) = m.entry(k) {
|
|
match 0 {
|
|
0 => {},
|
|
1 => {
|
|
e.insert(v);
|
|
},
|
|
_ => {
|
|
e.insert(v2);
|
|
},
|
|
};
|
|
}
|
|
|
|
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
|
|
},
|
|
}
|
|
});
|
|
|
|
m.entry(m!(k)).or_insert_with(|| m!(v));
|
|
}
|
|
|
|
fn btree_map<K: Eq + Ord + Copy, V: Copy>(m: &mut BTreeMap<K, V>, k: K, v: V, v2: V) {
|
|
if let std::collections::btree_map::Entry::Vacant(e) = m.entry(k) {
|
|
e.insert(v);
|
|
foo();
|
|
}
|
|
}
|
|
|
|
fn main() {}
|