From 1a97d1460b785faa83bf37cc5085b3162b40a64b Mon Sep 17 00:00:00 2001 From: J-ZhengLi Date: Sat, 2 Mar 2024 00:37:35 +0800 Subject: [PATCH 1/2] fix [`derive_partial_eq_without_eq`] FP on trait projection --- clippy_lints/src/derive.rs | 6 +++++- tests/ui/derive_partial_eq_without_eq.fixed | 17 +++++++++++++++++ tests/ui/derive_partial_eq_without_eq.rs | 17 +++++++++++++++++ tests/ui/derive_partial_eq_without_eq.stderr | 8 +++++++- 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/derive.rs b/clippy_lints/src/derive.rs index 6144ec7b3ca..f0f2c7d6658 100644 --- a/clippy_lints/src/derive.rs +++ b/clippy_lints/src/derive.rs @@ -451,8 +451,8 @@ fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_r && let Some(def_id) = trait_ref.trait_def_id() && cx.tcx.is_diagnostic_item(sym::PartialEq, def_id) && !has_non_exhaustive_attr(cx.tcx, *adt) + && !ty_implements_eq_trait(cx.tcx, ty, eq_trait_def_id) && let param_env = param_env_for_derived_eq(cx.tcx, adt.did(), eq_trait_def_id) - && !implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, None, &[]) // If all of our fields implement `Eq`, we can implement `Eq` too && adt .all_fields() @@ -471,6 +471,10 @@ fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_r } } +fn ty_implements_eq_trait<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, eq_trait_id: DefId) -> bool { + tcx.non_blanket_impls_for_ty(eq_trait_id, ty).next().is_some() +} + /// Creates the `ParamEnv` used for the give type's derived `Eq` impl. fn param_env_for_derived_eq(tcx: TyCtxt<'_>, did: DefId, eq_trait_id: DefId) -> ParamEnv<'_> { // Initial map from generic index to param def. diff --git a/tests/ui/derive_partial_eq_without_eq.fixed b/tests/ui/derive_partial_eq_without_eq.fixed index aa7a95405e0..62d8e2ee0c1 100644 --- a/tests/ui/derive_partial_eq_without_eq.fixed +++ b/tests/ui/derive_partial_eq_without_eq.fixed @@ -153,4 +153,21 @@ pub enum MissingEqNonExhaustive3 { Bar, } +mod issue_9413 { + pub trait Group { + type Element: Eq + PartialEq; + } + + pub trait Suite { + type Group: Group; + } + + #[derive(PartialEq, Eq)] + //~^ ERROR: you are deriving `PartialEq` and can implement `Eq` + pub struct Foo(::Element); + + #[derive(PartialEq, Eq)] + pub struct Bar(i32, ::Element); +} + fn main() {} diff --git a/tests/ui/derive_partial_eq_without_eq.rs b/tests/ui/derive_partial_eq_without_eq.rs index 90ac7a7989e..024915dd566 100644 --- a/tests/ui/derive_partial_eq_without_eq.rs +++ b/tests/ui/derive_partial_eq_without_eq.rs @@ -153,4 +153,21 @@ pub enum MissingEqNonExhaustive3 { Bar, } +mod issue_9413 { + pub trait Group { + type Element: Eq + PartialEq; + } + + pub trait Suite { + type Group: Group; + } + + #[derive(PartialEq)] + //~^ ERROR: you are deriving `PartialEq` and can implement `Eq` + pub struct Foo(::Element); + + #[derive(PartialEq, Eq)] + pub struct Bar(i32, ::Element); +} + fn main() {} diff --git a/tests/ui/derive_partial_eq_without_eq.stderr b/tests/ui/derive_partial_eq_without_eq.stderr index 42b9895121f..e0ee857dd7a 100644 --- a/tests/ui/derive_partial_eq_without_eq.stderr +++ b/tests/ui/derive_partial_eq_without_eq.stderr @@ -67,5 +67,11 @@ error: you are deriving `PartialEq` and can implement `Eq` LL | #[derive(PartialEq)] | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` -error: aborting due to 11 previous errors +error: you are deriving `PartialEq` and can implement `Eq` + --> tests/ui/derive_partial_eq_without_eq.rs:165:14 + | +LL | #[derive(PartialEq)] + | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` + +error: aborting due to 12 previous errors From dde2552b117a65b74d082136bcb1196455e7572c Mon Sep 17 00:00:00 2001 From: J-ZhengLi Date: Sat, 2 Mar 2024 01:06:59 +0800 Subject: [PATCH 2/2] add test cases for #9319 --- tests/ui/derive_partial_eq_without_eq.fixed | 11 ++++++++++- tests/ui/derive_partial_eq_without_eq.rs | 11 ++++++++++- tests/ui/derive_partial_eq_without_eq.stderr | 10 ++++++++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/tests/ui/derive_partial_eq_without_eq.fixed b/tests/ui/derive_partial_eq_without_eq.fixed index 62d8e2ee0c1..6f42487bbf4 100644 --- a/tests/ui/derive_partial_eq_without_eq.fixed +++ b/tests/ui/derive_partial_eq_without_eq.fixed @@ -153,7 +153,8 @@ pub enum MissingEqNonExhaustive3 { Bar, } -mod issue_9413 { +mod struct_gen { + // issue 9413 pub trait Group { type Element: Eq + PartialEq; } @@ -168,6 +169,14 @@ mod issue_9413 { #[derive(PartialEq, Eq)] pub struct Bar(i32, ::Element); + + // issue 9319 + #[derive(PartialEq, Eq)] + //~^ ERROR: you are deriving `PartialEq` and can implement `Eq` + pub struct Oof(T); + + #[derive(PartialEq, Eq)] + pub struct Rab(T); } fn main() {} diff --git a/tests/ui/derive_partial_eq_without_eq.rs b/tests/ui/derive_partial_eq_without_eq.rs index 024915dd566..24f687c6c9d 100644 --- a/tests/ui/derive_partial_eq_without_eq.rs +++ b/tests/ui/derive_partial_eq_without_eq.rs @@ -153,7 +153,8 @@ pub enum MissingEqNonExhaustive3 { Bar, } -mod issue_9413 { +mod struct_gen { + // issue 9413 pub trait Group { type Element: Eq + PartialEq; } @@ -168,6 +169,14 @@ pub trait Suite { #[derive(PartialEq, Eq)] pub struct Bar(i32, ::Element); + + // issue 9319 + #[derive(PartialEq)] + //~^ ERROR: you are deriving `PartialEq` and can implement `Eq` + pub struct Oof(T); + + #[derive(PartialEq, Eq)] + pub struct Rab(T); } fn main() {} diff --git a/tests/ui/derive_partial_eq_without_eq.stderr b/tests/ui/derive_partial_eq_without_eq.stderr index e0ee857dd7a..3d92112dc36 100644 --- a/tests/ui/derive_partial_eq_without_eq.stderr +++ b/tests/ui/derive_partial_eq_without_eq.stderr @@ -68,10 +68,16 @@ LL | #[derive(PartialEq)] | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` error: you are deriving `PartialEq` and can implement `Eq` - --> tests/ui/derive_partial_eq_without_eq.rs:165:14 + --> tests/ui/derive_partial_eq_without_eq.rs:166:14 | LL | #[derive(PartialEq)] | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` -error: aborting due to 12 previous errors +error: you are deriving `PartialEq` and can implement `Eq` + --> tests/ui/derive_partial_eq_without_eq.rs:174:14 + | +LL | #[derive(PartialEq)] + | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` + +error: aborting due to 13 previous errors