rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.09k stars 1.49k forks source link

`mutable_key_type` doesn't mention the type which makes it confusing #10619

Open Nilstrieb opened 1 year ago

Nilstrieb commented 1 year ago

Summary

I'm not sure whether this is a false positive, there are lots of maps involved here and clippy doesn't tell me what it's complaining about. At least I have no clues where it could be coming from.

warning: mutable key type
   --> compiler/rustc_codegen_ssa/src/back/symbol_export.rs:406:13
    |
406 |             let substs_map = instances.entry(def_id).or_default();
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#mutable_key_type
    = note: `-D clippy::mutable-key-type` implied by `-D clippy::suspicious`

instances is of type UnordMap<DefId, HashMap<&List<GenericArg>, CrateNum, BuildHasherDefault<FxHasher>>>

where

pub struct UnordMap<K: Eq + Hash, V> {
    inner: FxHashMap<K, V>, /* FxHashMap is just a type alias for Hashmap */
}

pub struct DefId {
    /* contains two newtyped integers */
}

pub struct List<T> {
    len: usize,
    data: [T; 0],
    opaque: OpaqueListContents,
}

extern "C" {
    type OpaqueListContents;
}

pub struct GenericArg<'tcx> {
    ptr: NonZeroUsize,
    marker: PhantomData<(Ty<'tcx>, ty::Region<'tcx>, ty::Const<'tcx>)>,
}

/* Ty, Region and Const are references to more nested things, see the rustc docs */

Lint Name

mutable_key_type

Reproducer

I sadly don't have a simple reproducer, but I ran clippy in rust-lang/rust 1c39afb37536e7e2ea974734aa65ff025b47f7b9

Version

rustc 1.70.0-nightly (af06dce64 2023-04-08)
binary: rustc
commit-hash: af06dce64bf87ea9206bdf6cff61c144b9ce8458
commit-date: 2023-04-08
host: x86_64-unknown-linux-gnu
release: 1.70.0-nightly
LLVM version: 16.0.2

Additional Labels

No response

jyn514 commented 1 year ago

I sadly don't have a simple reproducer, but I can clippy in rust-lang/rust 1c39afb37536e7e2ea974734aa65ff025b47f7b9

x clippy compiler -A clippy::all -D clippy::perf

kpreid commented 1 month ago

I just hit this issue when testing with nightly clippy, presumably due to #12691 expanding the set of things that are counted as interior mutable. In my own case, I was able to configure ignore-interior-mutability based on my knowledge of the types in my project, but it could have been very difficult to track down what type clippy was actually objecting to (or more precisely, what type is the most appropriate to allow).