blob: 20dba1798d0d5d17887fc745fe7b44537ec8059e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
### What it does
Checks for uses of `contains_key` + `insert` on `HashMap`
or `BTreeMap`.
### Why is this bad?
Using `entry` is more efficient.
### Known problems
The suggestion may have type inference errors in some cases. e.g.
```
let mut map = std::collections::HashMap::new();
let _ = if !map.contains_key(&0) {
map.insert(0, 0)
} else {
None
};
```
### Example
```
if !map.contains_key(&k) {
map.insert(k, v);
}
```
Use instead:
```
map.entry(k).or_insert(v);
```
|