Skip to content

A lint to detect when a type implements all three of Hash, Borrow<str> and Borrow<[u8]> #11710

Closed
@thomcc

Description

@thomcc

What it does

Detects when a type which implements Hash also implements both Borrow<str> and Borrow<[u8]>.

Advantage

It's impossible to implement both of these correctly, as str and [u8] hash differently in incompatible ways, and implementing Borrow<T> for your type guaratees that you hash equivalent to T.

This is worth adding because this impl is tempting to add, as the distinction between Borrow and AsRef is very subtle, and the AsRef impl for both is correct (as is implementing both for types which do not implement Hash).

See BurntSushi/bstr#168 for some discussion (As this would not quite have caught the problem in that issue, it's possible my formulation of it is too narrow. For the most part, I am chucking this lint proposal over the wall, and am not tied to the specifics here).

Drawbacks

It's rather niche.

Example

impl Hash for MyType {
    // ...
}

impl Borrow<str> for MyType {
    // ...
}

impl Borrow<[u8]> for MyType {
    // ...
}

is wrong, either remove any one of those impls to be correct.

Metadata

Metadata

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions