rust-lang / rust-lang/rust-clippy

Lint against `HashMap`s where the key is not `Eq + Hash`

Open
#12,866 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
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 HashMap is fully usable.
Drawbacks
  • There might be a legitimate use case for a HashMap without K: 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

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.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.