rust-lang / rust-lang/rust-clippy

Suggest using enum-map instead of HashMap with enum variant keys

Open
#7,329 1 comment 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

The naive_bytecount lint suggests to use a crate to perform an operation. So we could suggest to use a crate like:
https://crates.io/crates/enum-map

When code uses a HashMap with payload-free enum variants as keys, as here:
https://blog.frankel.ch/start-rust/2/

pub fn group_sidekicks_by_alignment<'a>(supers: &'a Vec<Super<'a>>) -> HashMap<Alignment, Vec<&'a Super<'a>>>  {
    let mut map = HashMap::new();                             
    map.insert(Good, Vec::new());                             
    map.insert(Evil, Vec::new());                             
    supers
        .iter()
        .filter(|&s| s.sidekick.is_some())                     
        .fold(map, |mut map, s| {                             
            let value = map.entry(s.alignment).or_default();  
            value.push(&s.sidekick.unwrap());                 
            map                                               
        })
}

(In Ada language this problems doesn't exist, because arrays are allowed to have enumeration keys).

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 with the naive_bytecount lint and compare the linked HashMap example with the enum-map crate. Define the supported payload-free enum-key pattern and verify the expected suggestion behavior with focused lint tests; done means the intended cases are recognized without affecting unrelated HashMap uses.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.