rust/tests/ui/entry.fixed
Jason Newcomb 3323ff7145
map_entry improvements
Suggest using `or_insert_with` when possible
2021-04-15 08:22:40 -04:00

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() {}