Resolving clippy warning

Hi!

For this code:

fn x(..) -> bool {

    [...]

    if !vertices.1.contains_key(&new_vertex) {
        vertices.0.push(new_vertex);
        vertices.1.insert(new_vertex, next_index.cnext());
        return true;
    }
    return false;
}

I'm getting the following clippy warning:

warning: usage of `contains_key` followed by `insert` on a `HashMap`
   --> src/subgraph_sampling/sampling_algo.rs:138:5
    |
138 | /     if !vertices.1.contains_key(&new_vertex) {
139 | |         vertices.0.push(new_vertex);
140 | |         vertices.1.insert(new_vertex, next_index.cnext());
141 | |         return true;
142 | |     }
    | |_____^ help: consider using: `vertices.1.entry(new_vertex)`
    |
    = note: #[warn(map_entry)] on by default
    = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.210/index.html#map_entry 

I don't see an abvious way to refactor the code using .entry(). Am I missing something, or is clippy being too pedantic here?

Something like:

if let VacantEntry(e) = vertices.1.entry(new_vertex) {
    vertices.0.push(new_vertex);
    e.insert(next_index.cnext());
    return true;
}
1 Like