Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
thomcc opened this issue Mar 21, 2019 · 3 comments
Open
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@thomcc
Copy link
Member

thomcc commented Mar 21, 2019

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)
@flip1995 flip1995 added C-bug Category: Clippy is not doing the correct thing L-suggestion Lint: Improving, adding or fixing lint suggestions I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Mar 21, 2019
@Arnavion
Copy link
Contributor

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()
    }
}

@thomcc
Copy link
Member Author

thomcc commented May 1, 2019

Thanks, even though it's probably not worth it for me to add this (I need to convert to a HashMap to satisfy prost, which doesn't let me use a custom hasher anyway), I don't think I had ever actually seen an example of code that worked with Into but not with From.

@GrantGryczan
Copy link

GrantGryczan commented Jan 13, 2024

Since rust-lang/rfcs#2451 was merged, the suggested code no longer produces error E210. This issue should be either closed or revised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

No branches or pull requests

4 participants