rust-lang / rust-lang/rust-clippy

Idea: Allow `#[allow(clippy::mutable_key_type)]` on type definitions

Open
#13,865 3 comments 2 reactions 1 assignee View on GitHub

@ada4a is already working on this.

Since Oct 7, 2025.

C-enhancement
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Description

Problem

I am getting false positives from mutable_key_type on a type I own. (Illustrative Playground.) The current options to silence them are:

  • Label each site at which it is used, with #[allow(clippy::mutable_key_type)].

    • Downside: The annotations add lots of noise if there are many use-sites. And since the actual type definition has no such annotations, it is possible someone might modify it to be mutable without realising that it's important not to.
  • Add the type to clippy.toml or other config, under the key ignore-interior-mutability.

    • Downside: Nothing in the code warns about the expected invariant. You have to realise to look in the clippy configuration file.

My solution so far has been to do the latter, and write a comment on the key type's definition, explaining the situation and referring to the clippy config file.

Idea

What if type definitions could also be annotated with #[allow(clippy::mutable_key_type)]?

It feels a lot more intuitive than the current options, and would simultaneously

  • make the lint ignore that type,
  • document the expected invariant for other contributors at the right location,
  • cause no noise at the call site where the code is already correct, and
  • fit into the lint tooling ecosystem, allowing the reasoning to be listed attached to the allow as a lint reason, instead of an impromptu comment.
Version
rustc 1.85.0-nightly (6d9f6ae36 2024-12-16)
binary: rustc
commit-hash: 6d9f6ae36ae1299d6126ba40c15191f7aa3b79d8
commit-date: 2024-12-16
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.5
Additional Labels

@rustbot label +C-enhancement

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.