rust-lang / rust-lang/rust-clippy

Clippy `contains_key` followed by `insert` suggestion won’t compile

Open
#11,558 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

From https://users.rust-lang.org/t/clippy-contains-key-followed-by-insert-suggestion-wont-compile/100243

Clippy's suggestion of replacement code doesn't compile

See Playground 1

Reproducer

I tried this code:

use std::collections::BTreeMap;

#[derive(PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug)]
struct Pos(usize, usize);

fn can_get_to(start: Pos, end: Pos) -> Option<u32> {
    if start == Pos(1,1) && end == Pos(2,2) {
        Some(5)
    } else {
        None
    }
}

fn main() {
    let mut distance: BTreeMap<Pos, u32> = BTreeMap::new();
   
    let posnear = Pos(1,1);
    let posfar = Pos(2,2);
    
    distance.insert(posnear, 10);
   
    if !distance.contains_key(&posfar) {
        if let Some(dist) = can_get_to(posnear, posfar) {
          distance.insert(posfar, distance[&posnear] + dist);
        }
    }
    
    // Clippy's suggestion for above - won't compile
    // if let std::collections::btree_map::Entry::Vacant(e) = distance.entry(posfar) {
    //     if let Some(dist) = can_get_to(posnear, posfar) {
    //         e.insert(distance[&posnear] + dist);
    //     }
    // }

    dbg!(distance);
}

And the compile error from Clippy's suggestion:

error[E0502]: cannot borrow `distance` as immutable because it is also borrowed as mutable
  --> src/main.rs:31:22
   |
29 |     if let std::collections::btree_map::Entry::Vacant(e) = distance.entry(posfar) {
   |                                                            ---------------------- mutable borrow occurs here
30 |         if let Some(dist) = can_get_to(posnear, posfar) {
31 |             e.insert(distance[&posnear] + dist);
   |               ------ ^^^^^^^^ immutable borrow occurs here
   |               |
   |               mutable borrow later used by call
Version

No response

Additional Labels

@rustbot label +I-suggestion-causes-error

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the Clippy lint that emits the contains_key/insert suggestion, then compile the supplied BTreeMap reproducer to confirm the failure. Done means the suggested replacement compiles while preserving the intended insertion behavior, with a regression test covering the reproducer.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.