Skip to content

Commit f75d8c7

Browse files
committed
Auto merge of rust-lang#12393 - J-ZhengLi:issue9413, r=dswij
fix [`derive_partial_eq_without_eq`] FP on trait projection fixes: rust-lang#9413 rust-lang#9319 --- changelog: fix [`derive_partial_eq_without_eq`] FP on trait projection Well, this is awkward, it works but I don't understand why, why `clippy_utils::ty::implements_trait` couldn't detects the existance of `Eq` trait, even thought it's obviously present in the derive attribute.
2 parents c2dd413 + dde2552 commit f75d8c7

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

clippy_lints/src/derive.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_r
451451
&& let Some(def_id) = trait_ref.trait_def_id()
452452
&& cx.tcx.is_diagnostic_item(sym::PartialEq, def_id)
453453
&& !has_non_exhaustive_attr(cx.tcx, *adt)
454+
&& !ty_implements_eq_trait(cx.tcx, ty, eq_trait_def_id)
454455
&& let param_env = param_env_for_derived_eq(cx.tcx, adt.did(), eq_trait_def_id)
455-
&& !implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, None, &[])
456456
// If all of our fields implement `Eq`, we can implement `Eq` too
457457
&& adt
458458
.all_fields()
@@ -471,6 +471,10 @@ fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_r
471471
}
472472
}
473473

474+
fn ty_implements_eq_trait<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, eq_trait_id: DefId) -> bool {
475+
tcx.non_blanket_impls_for_ty(eq_trait_id, ty).next().is_some()
476+
}
477+
474478
/// Creates the `ParamEnv` used for the give type's derived `Eq` impl.
475479
fn param_env_for_derived_eq(tcx: TyCtxt<'_>, did: DefId, eq_trait_id: DefId) -> ParamEnv<'_> {
476480
// Initial map from generic index to param def.

tests/ui/derive_partial_eq_without_eq.fixed

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,30 @@ pub enum MissingEqNonExhaustive3 {
153153
Bar,
154154
}
155155

156+
mod struct_gen {
157+
// issue 9413
158+
pub trait Group {
159+
type Element: Eq + PartialEq;
160+
}
161+
162+
pub trait Suite {
163+
type Group: Group;
164+
}
165+
166+
#[derive(PartialEq, Eq)]
167+
//~^ ERROR: you are deriving `PartialEq` and can implement `Eq`
168+
pub struct Foo<C: Suite>(<C::Group as Group>::Element);
169+
170+
#[derive(PartialEq, Eq)]
171+
pub struct Bar<C: Suite>(i32, <C::Group as Group>::Element);
172+
173+
// issue 9319
174+
#[derive(PartialEq, Eq)]
175+
//~^ ERROR: you are deriving `PartialEq` and can implement `Eq`
176+
pub struct Oof<T: Fn()>(T);
177+
178+
#[derive(PartialEq, Eq)]
179+
pub struct Rab<T: Fn()>(T);
180+
}
181+
156182
fn main() {}

tests/ui/derive_partial_eq_without_eq.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,30 @@ pub enum MissingEqNonExhaustive3 {
153153
Bar,
154154
}
155155

156+
mod struct_gen {
157+
// issue 9413
158+
pub trait Group {
159+
type Element: Eq + PartialEq;
160+
}
161+
162+
pub trait Suite {
163+
type Group: Group;
164+
}
165+
166+
#[derive(PartialEq)]
167+
//~^ ERROR: you are deriving `PartialEq` and can implement `Eq`
168+
pub struct Foo<C: Suite>(<C::Group as Group>::Element);
169+
170+
#[derive(PartialEq, Eq)]
171+
pub struct Bar<C: Suite>(i32, <C::Group as Group>::Element);
172+
173+
// issue 9319
174+
#[derive(PartialEq)]
175+
//~^ ERROR: you are deriving `PartialEq` and can implement `Eq`
176+
pub struct Oof<T: Fn()>(T);
177+
178+
#[derive(PartialEq, Eq)]
179+
pub struct Rab<T: Fn()>(T);
180+
}
181+
156182
fn main() {}

tests/ui/derive_partial_eq_without_eq.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,17 @@ error: you are deriving `PartialEq` and can implement `Eq`
6767
LL | #[derive(PartialEq)]
6868
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
6969

70-
error: aborting due to 11 previous errors
70+
error: you are deriving `PartialEq` and can implement `Eq`
71+
--> tests/ui/derive_partial_eq_without_eq.rs:166:14
72+
|
73+
LL | #[derive(PartialEq)]
74+
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
75+
76+
error: you are deriving `PartialEq` and can implement `Eq`
77+
--> tests/ui/derive_partial_eq_without_eq.rs:174:14
78+
|
79+
LL | #[derive(PartialEq)]
80+
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
81+
82+
error: aborting due to 13 previous errors
7183

0 commit comments

Comments
 (0)