rust-lang / rust-lang/rust-clippy
Suggest using enum-map instead of HashMap with enum variant keys
Nobody has claimed this yet.
- 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
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 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