rust-lang / rust-lang/rust-clippy
Lint against `HashMap`s where the key is not `Eq + Hash`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
An empty HashMap<K, V> can be constructed with any type of key, but to actually allow inserting values into it, the key type must implement Eq and Hash.
This lint would warn if it encounters any type that does not implement Eq + Hash used as a HashMap key and, if possible, suggest deriving the missing traits (PartialEq, Eq, Hash).
Advantage
- The
HashMapis fully usable.
Drawbacks
- There might be a legitimate use case for a
HashMapwithoutK: Eq + Hash(I don't know if there is one).
Example
use std::collections::HashMap;
struct Key(u32);
struct Other(HashMap<Key, String>);
Could be written as:
use std::collections::HashMap;
#[derive(PartialEq, Eq, Hash)]
struct Key(u32);
struct Other(HashMap<Key, String>);
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start from the issue's Rust example involving HashMap<Key, String> and determine how Clippy can identify key types lacking Eq and Hash. Review existing HashMap-related lint patterns and tests, then define completion as a warning for unusable key types with a valid derive suggestion where possible.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools, tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100