rust-lang / rust-lang/rust-clippy

clippy::implicit_hasher shouldn't warn on From<Thing> for HashMap<K, V>

Open
#3,899 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-suggestion-causes-error L-suggestion
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

And probably others too. AFAICT fixing this is impossible.

For example, if I have

struct MyThing(Vec<(String, String)>);
impl From<MyThing> for HashMap<String, String> {
    fn from(t: MyThing) -> HashMap<String, String> {
        t.0.into_iter().collect()
    }
}

then I get clippy::implicit hasher with the suggestion

  |
5 | impl From<MyThing> for HashMap<String, String> {
  |                        ^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(clippy::implicit_hasher)] on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#implicit_hasher
help: consider adding a type parameter
  |
5 | impl<S: ::std::hash::BuildHasher> From<MyThing> for HashMap<String, String, S> {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                   ^^^^^^^^^^^^^^^^^^^^^^^^^^

But I can't use that suggestion, I get E210.

error[E0210]: type parameter `S` must be used as the type parameter for some local type (e.g., `MyStruct<S>`)
  --> src/main.rs:12:1
   |
12 | impl<S: std::hash::BuildHasher> From<MyThing> for HashMap<String, String, S> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type parameter `S` must be used as the type parameter for some local type
   |
   = note: only traits defined in the current crate can be implemented for a type parameter

Link to repro in playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=eeb25dc899a8ac4d201740279be2744b

> cargo clippy -V
clippy 0.0.212 (1b89724b 2019-01-15)

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 linked Rust Playground reproduction and the clippy::implicit_hasher lint behavior shown in the issue. Confirm whether the lint can avoid this warning or provide a valid suggestion for the From for HashMap<String, String> case, then verify the result with cargo clippy.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.