Skip to content

Commit 93d84d7

Browse files
authored
Rollup merge of rust-lang#4766 - phansch:fix_fp_in_derive_hash_xor_eq, r=flip1995
Fix false positive in derive_hash_xor_eq This fixes a false positive in derive_hash_xor_eq where the lint was triggering on user-defined traits called `Hash`. changelog: Fix false positive in `derive_hash_xor_eq` Fixes rust-lang#4658
2 parents 016857d + 78b7e85 commit 93d84d7

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

clippy_lints/src/derive.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ fn check_hash_peq<'a, 'tcx>(
9090
if_chain! {
9191
if match_path(&trait_ref.path, &paths::HASH);
9292
if let Some(peq_trait_def_id) = cx.tcx.lang_items().eq_trait();
93+
if !&trait_ref.trait_def_id().is_local();
9394
then {
9495
// Look for the PartialEq implementations for `ty`
9596
cx.tcx.for_each_relevant_impl(peq_trait_def_id, ty, |impl_id| {

tests/ui/derive_hash_xor_eq.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::hash::{Hash, Hasher};
2-
31
#[derive(PartialEq, Hash)]
42
struct Foo;
53

@@ -30,8 +28,27 @@ impl PartialEq<Baz> for Baz {
3028
#[derive(PartialEq)]
3129
struct Bah;
3230

33-
impl Hash for Bah {
34-
fn hash<H: Hasher>(&self, _: &mut H) {}
31+
impl std::hash::Hash for Bah {
32+
fn hash<H: std::hash::Hasher>(&self, _: &mut H) {}
33+
}
34+
35+
#[derive(PartialEq)]
36+
struct Foo2;
37+
38+
trait Hash {}
39+
40+
// We don't want to lint on user-defined traits called `Hash`
41+
impl Hash for Foo2 {}
42+
43+
mod use_hash {
44+
use std::hash::{Hash, Hasher};
45+
46+
#[derive(PartialEq)]
47+
struct Foo3;
48+
49+
impl Hash for Foo3 {
50+
fn hash<H: std::hash::Hasher>(&self, _: &mut H) {}
51+
}
3552
}
3653

3754
fn main() {}

tests/ui/derive_hash_xor_eq.stderr

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
error: you are deriving `Hash` but have implemented `PartialEq` explicitly
2-
--> $DIR/derive_hash_xor_eq.rs:12:10
2+
--> $DIR/derive_hash_xor_eq.rs:10:10
33
|
44
LL | #[derive(Hash)]
55
| ^^^^
66
|
77
= note: `#[deny(clippy::derive_hash_xor_eq)]` on by default
88
note: `PartialEq` implemented here
9-
--> $DIR/derive_hash_xor_eq.rs:15:1
9+
--> $DIR/derive_hash_xor_eq.rs:13:1
1010
|
1111
LL | / impl PartialEq for Bar {
1212
LL | | fn eq(&self, _: &Bar) -> bool {
@@ -16,13 +16,13 @@ LL | | }
1616
| |_^
1717

1818
error: you are deriving `Hash` but have implemented `PartialEq` explicitly
19-
--> $DIR/derive_hash_xor_eq.rs:21:10
19+
--> $DIR/derive_hash_xor_eq.rs:19:10
2020
|
2121
LL | #[derive(Hash)]
2222
| ^^^^
2323
|
2424
note: `PartialEq` implemented here
25-
--> $DIR/derive_hash_xor_eq.rs:24:1
25+
--> $DIR/derive_hash_xor_eq.rs:22:1
2626
|
2727
LL | / impl PartialEq<Baz> for Baz {
2828
LL | | fn eq(&self, _: &Baz) -> bool {
@@ -32,18 +32,32 @@ LL | | }
3232
| |_^
3333

3434
error: you are implementing `Hash` explicitly but have derived `PartialEq`
35-
--> $DIR/derive_hash_xor_eq.rs:33:1
35+
--> $DIR/derive_hash_xor_eq.rs:31:1
3636
|
37-
LL | / impl Hash for Bah {
38-
LL | | fn hash<H: Hasher>(&self, _: &mut H) {}
37+
LL | / impl std::hash::Hash for Bah {
38+
LL | | fn hash<H: std::hash::Hasher>(&self, _: &mut H) {}
3939
LL | | }
4040
| |_^
4141
|
4242
note: `PartialEq` implemented here
43-
--> $DIR/derive_hash_xor_eq.rs:30:10
43+
--> $DIR/derive_hash_xor_eq.rs:28:10
4444
|
4545
LL | #[derive(PartialEq)]
4646
| ^^^^^^^^^
4747

48-
error: aborting due to 3 previous errors
48+
error: you are implementing `Hash` explicitly but have derived `PartialEq`
49+
--> $DIR/derive_hash_xor_eq.rs:49:5
50+
|
51+
LL | / impl Hash for Foo3 {
52+
LL | | fn hash<H: std::hash::Hasher>(&self, _: &mut H) {}
53+
LL | | }
54+
| |_____^
55+
|
56+
note: `PartialEq` implemented here
57+
--> $DIR/derive_hash_xor_eq.rs:46:14
58+
|
59+
LL | #[derive(PartialEq)]
60+
| ^^^^^^^^^
61+
62+
error: aborting due to 4 previous errors
4963

0 commit comments

Comments
 (0)