Skip to content

Compile error after applying clippy::map_entry #13934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hwright opened this issue Jan 3, 2025 · 2 comments · Fixed by #14151
Closed

Compile error after applying clippy::map_entry #13934

hwright opened this issue Jan 3, 2025 · 2 comments · Fixed by #14151
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@hwright
Copy link

hwright commented Jan 3, 2025

Summary

After applying a clippy::map_entry suggested fix, the generated code fails to compile. Clippy appears unable to see uses of the given map from within method calls on the same struct.

Lint Name

clippy::map_entry

Reproducer

I tried this code:

use std::collections::HashMap;

struct Member {}

pub struct Foo {
    members: HashMap<u8, Member>,
}

impl Foo {
    pub fn bar(&mut self, input: u8) -> Result<(), String> {
        if self.members.contains_key(&input) {
            Err("already exists".to_string())
        } else {
            self.other();
            self.members.insert(input, Member {});
            Ok(())
        }
    }

    fn other(&self) {
        for key in self.members.keys() {
            println!("key: {}", key);
        }
    }
}

I saw this happen:

    Checking playground v0.0.1 (/playground)
warning: usage of `contains_key` followed by `insert` on a `HashMap`
  --> src/lib.rs:11:9
   |
11 | /         if self.members.contains_key(&input) {
12 | |             Err("already exists".to_string())
13 | |         } else {
14 | |             self.other();
15 | |             self.members.insert(input, Member {});
16 | |             Ok(())
17 | |         }
   | |_________^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_entry
   = note: `#[warn(clippy::map_entry)]` on by default
help: try
   |
11 ~         if let std::collections::hash_map::Entry::Vacant(e) = self.members.entry(input) {
12 +             self.other();
13 +             e.insert(Member {});
14 +             Ok(())
15 +         } else {
16 +             Err("already exists".to_string())
17 +         }
   |

warning: `playground` (lib) generated 1 warning (run `cargo clippy --fix --lib -p playground` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.46s

When applied to the original code, this suggestion fails to compile with this error:

Compiling playground v0.0.1 (/playground)
error[E0502]: cannot borrow `*self` as immutable because it is also borrowed as mutable
  --> src/lib.rs:12:14
   |
11 |         if let std::collections::hash_map::Entry::Vacant(e) = self.members.entry(input) {
   |                                                               ------------ mutable borrow occurs here
12 |              self.other();
   |              ^^^^ immutable borrow occurs here
13 |              e.insert(Member {});
   |              - mutable borrow later used here

For more information about this error, try `rustc --explain E0502`.
error: could not compile `playground` (lib) due to 1 previous error

I expected to see this happen:
I expect clippy to not make this suggestion at all.

Version

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: aarch64-apple-darwin
release: 1.83.0
LLVM version: 19.1.1

Additional Labels

@rustbot label +l-suggestion-causes-error

@hwright hwright added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jan 3, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jan 3, 2025

Error: Parsing relabel command in comment failed: ...' label +' | error: empty label at >| ' l-suggest'...

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@profetia
Copy link
Contributor

profetia commented Feb 5, 2025

@rustbot claim

@mwylde mwylde marked this as a duplicate of #14154 Feb 5, 2025
github-merge-queue bot pushed a commit that referenced this issue Feb 19, 2025
fixes #13934

I modified the part for checking if the map is used so that it can check
field and index exprs.

changelog: [`map_entry`]: fix FP on struct member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants