Skip to content

Consider suggesting using HashMap<String, String> instead of HashSet<String, String> when PartialEq derive macro is used #147147

Description

@zlepper

Code

use std::collections::HashSet;

#[derive(PartialEq)]
pub struct MyStruct {
    pub parameters: HashSet<String, String>,
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0369]: binary operation `==` cannot be applied to type `HashSet<String, String>`
   --> src/lib.rs:5:5
    |
  3 | #[derive(PartialEq)]
    |          --------- in this derive macro expansion
  4 | pub struct MyStruct {
  5 |     pub parameters: HashSet<String, String>,
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: the foreign item type `String` doesn't implement `BuildHasher`
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/string.rs:360:1
    |
360 | pub struct String {
    | ^^^^^^^^^^^^^^^^^ not implement `BuildHasher`

For more information about this error, try `rustc --explain E0369`.
error: could not compile `playground` (lib) due to 1 previous error

Desired output

Compiling playground v0.0.1 (/playground)
error[E0369]: binary operation `==` cannot be applied to type `HashSet<String, String>`
   --> src/lib.rs:5:5
    |
  3 | #[derive(PartialEq)]
    |          --------- in this derive macro expansion
  4 | pub struct MyStruct {
  5 |     pub parameters: HashSet<String, String>,
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: the foreign item type `String` doesn't implement `BuildHasher`
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/string.rs:360:1
    |
360 | pub struct String {
    | ^^^^^^^^^^^^^^^^^ not implement `BuildHasher`

Suggestion: Did you perhaps intend to use `HashMap<String, String>` instead?

For more information about this error, try `rustc --explain E0369`.
error: could not compile `playground` (lib) due to 1 previous error

Rationale and extra context

I have now messed this up several times. The first time it keep me about an hour to notice that I had used the wrong hash collection. The third time this morning it thankfully only took me 5 minutes.

The current compiler error is absolutely correct, and i'm the fool who can't properly articulate what I thought i was doing, and what I was actually doing.

When I expect to have typed HashMap, and i'm like 99.99% sure that strings as supported as keys in HashMaps, then it absolutely confuses by brain enough that I don't catch the difference between HashSet<String, T> and HashMap<String, T>, considering there are only 3 letters difference in the middle.

A way of catching this issue and emitting diagnostics could be look for HashSet declarations that specifies both T and S, where T doesn't implement BuildHasher.

Other cases

Rust Version

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7

Anything else?

Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=4aa004ed6a442e7b902c1d30d5800aa0 (1.90 as of the making of this issue)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions