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

mutable_key_type error on tuple hashmap keys with generics #5043

Closed
VictorKoenders opened this issue Jan 12, 2020 · 1 comment · Fixed by #5056
Closed

mutable_key_type error on tuple hashmap keys with generics #5043

VictorKoenders opened this issue Jan 12, 2020 · 1 comment · Fixed by #5056
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@VictorKoenders
Copy link

Clippy seems to give a false positive when a hashmap has a key that consists of a tuple of a struct and a generic:

struct SomeStruct<T: Hash + Eq> {
    map: HashMap<(T, U), ()>,
}

impl<T: Hash + Eq> SomeStruct<T> {
    fn test(&mut self) {
        let _mut_map_ref = &mut self.map;
        // error: mutable key type
        //   = note: `#[deny(clippy::mutable_key_type)]` on by default
        //   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#mutable_key_type
    }
}

Changing the key to be either T or U (not a tuple) removes the error:

// This is fine
struct SomeStruct<T: Hash + Eq> {
    map: HashMap<T, ()>,
}
// This is also fine
struct SomeStruct<T: Hash + Eq> {
    map: HashMap<U, ()>,
    pd: PhantomData<T>,
}

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

This might be related to #5020

@VictorKoenders
Copy link
Author

Semi-related, in some cases the error can be mitigated by destructuring and then calling a mutable method on the hashmap reference.

image

@flip1995 flip1995 added the C-bug Category: Clippy is not doing the correct thing label Jan 14, 2020
@bors bors closed this as completed in f7b3e4f Jan 19, 2020
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants