profile
viewpoint

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

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)
rust-lang/rust-clippy

Answer questions Arnavion

Clippy's suggestion is wrong, but FWIW the spirit of the lint (supporting HashMap with non-default hashers) can be met by impling Into instead of From:

impl<S> Into<HashMap<String, String, S>> for MyThing where S: std::hash::BuildHasher + Default {
    fn into(self) -> HashMap<String, String, S> {
        self.0.into_iter().collect()
    }
}
useful!
source:https://uonfu.com/
Github User Rank List