Skip to content
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

map_entry false positive when map needs to be used while still borrowed by the entry #4674

Closed
yanchith opened this issue Oct 15, 2019 · 0 comments · Fixed by #6937
Closed
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@yanchith
Copy link

Hello, thanks for developing clippy! Ran into this (I believe) false positive in the map_entry lint:

The following function triggers the lint, but the suggestion (using entry) correctly triggers a borrowchecker error due to the map being borrowed by the entry.

/// Does compile, but triggers clippy::map_entry false positive
fn main() {
    let mut m: HashMap<u32, u32> = HashMap::new();
    let k: u32 = 0;
    if !m.contains_key(&k) {
        let value = compute_the_answer_possibly_with_map(&m);
        m.insert(k, value);
    }
}
/// Does not compile due to the entry having long lived
/// mutable borrow
fn main() {
    let mut m: HashMap<u32, u32> = HashMap::new();
    let k: u32 = 0;
    if let Entry::Vacant(vacant) = m.entry(k) {
        // Doesn't matter whether mutable or shared, this borrow is rejected!
        let value = compute_the_answer_possibly_with_map(&m);
        vacant.insert(value);
    }
}

The value computation can not be moved before the lookup - the point of the lookup is to prevent the computation from happening.

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=16a29ebce3e1f3ce4fdd5676052e6914

Clippy version: clippy 0.0.212 (3aea8603 2019-09-03)

@flip1995 flip1995 added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Oct 16, 2019
@camsteffen camsteffen 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 E-medium Call for participation: Medium difficulty level problem and requires some initial experience. labels Jan 26, 2021
@bors bors closed this as completed in 1e0a3ff Apr 16, 2021
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 E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants