Skip to content

Commit 50541b9

Browse files
committed
Auto merge of #8950 - Jarcho:derive_non_pub, r=dswij
Fixes for `derive_partial_eq_without_eq` fixes #8875 changelog: Don't lint `derive_partial_eq_without_eq` on non-public types changelog: Better handle generics in `derive_partial_eq_without_eq`
2 parents ab8c8c6 + 648dbeb commit 50541b9

File tree

4 files changed

+159
-80
lines changed

4 files changed

+159
-80
lines changed

clippy_lints/src/derive.rs

+54-43
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ use clippy_utils::ty::{implements_trait, implements_trait_with_env, is_copy};
44
use clippy_utils::{is_lint_allowed, match_def_path};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
7+
use rustc_hir::def_id::DefId;
78
use rustc_hir::intravisit::{walk_expr, walk_fn, walk_item, FnKind, Visitor};
89
use rustc_hir::{
9-
self as hir, BlockCheckMode, BodyId, Expr, ExprKind, FnDecl, HirId, Impl, Item, ItemKind, UnsafeSource, Unsafety,
10+
self as hir, BlockCheckMode, BodyId, Constness, Expr, ExprKind, FnDecl, HirId, Impl, Item, ItemKind, UnsafeSource,
11+
Unsafety,
1012
};
1113
use rustc_lint::{LateContext, LateLintPass};
1214
use rustc_middle::hir::nested_filter;
13-
use rustc_middle::ty::subst::GenericArg;
14-
use rustc_middle::ty::{self, BoundConstness, ImplPolarity, ParamEnv, PredicateKind, TraitPredicate, TraitRef, Ty};
15+
use rustc_middle::traits::Reveal;
16+
use rustc_middle::ty::{
17+
self, Binder, BoundConstness, GenericParamDefKind, ImplPolarity, ParamEnv, PredicateKind, TraitPredicate, TraitRef,
18+
Ty, TyCtxt, Visibility,
19+
};
1520
use rustc_session::{declare_lint_pass, declare_tool_lint};
1621
use rustc_span::source_map::Span;
1722
use rustc_span::sym;
@@ -459,50 +464,18 @@ impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> {
459464
fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_ref: &hir::TraitRef<'_>, ty: Ty<'tcx>) {
460465
if_chain! {
461466
if let ty::Adt(adt, substs) = ty.kind();
467+
if cx.tcx.visibility(adt.did()) == Visibility::Public;
462468
if let Some(eq_trait_def_id) = cx.tcx.get_diagnostic_item(sym::Eq);
463-
if let Some(peq_trait_def_id) = cx.tcx.get_diagnostic_item(sym::PartialEq);
464469
if let Some(def_id) = trait_ref.trait_def_id();
465470
if cx.tcx.is_diagnostic_item(sym::PartialEq, def_id);
466-
// New `ParamEnv` replacing `T: PartialEq` with `T: Eq`
467-
let param_env = ParamEnv::new(
468-
cx.tcx.mk_predicates(cx.param_env.caller_bounds().iter().map(|p| {
469-
let kind = p.kind();
470-
match kind.skip_binder() {
471-
PredicateKind::Trait(p)
472-
if p.trait_ref.def_id == peq_trait_def_id
473-
&& p.trait_ref.substs.get(0) == p.trait_ref.substs.get(1)
474-
&& matches!(p.trait_ref.self_ty().kind(), ty::Param(_))
475-
&& p.constness == BoundConstness::NotConst
476-
&& p.polarity == ImplPolarity::Positive =>
477-
{
478-
cx.tcx.mk_predicate(kind.rebind(PredicateKind::Trait(TraitPredicate {
479-
trait_ref: TraitRef::new(
480-
eq_trait_def_id,
481-
cx.tcx.mk_substs([GenericArg::from(p.trait_ref.self_ty())].into_iter()),
482-
),
483-
constness: BoundConstness::NotConst,
484-
polarity: ImplPolarity::Positive,
485-
})))
486-
},
487-
_ => p,
488-
}
489-
})),
490-
cx.param_env.reveal(),
491-
cx.param_env.constness(),
492-
);
493-
if !implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, substs);
471+
let param_env = param_env_for_derived_eq(cx.tcx, adt.did(), eq_trait_def_id);
472+
if !implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, &[]);
473+
// If all of our fields implement `Eq`, we can implement `Eq` too
474+
if adt
475+
.all_fields()
476+
.map(|f| f.ty(cx.tcx, substs))
477+
.all(|ty| implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, &[]));
494478
then {
495-
// If all of our fields implement `Eq`, we can implement `Eq` too
496-
for variant in adt.variants() {
497-
for field in &variant.fields {
498-
let ty = field.ty(cx.tcx, substs);
499-
500-
if !implements_trait(cx, ty, eq_trait_def_id, substs) {
501-
return;
502-
}
503-
}
504-
}
505-
506479
span_lint_and_sugg(
507480
cx,
508481
DERIVE_PARTIAL_EQ_WITHOUT_EQ,
@@ -515,3 +488,41 @@ fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_r
515488
}
516489
}
517490
}
491+
492+
/// Creates the `ParamEnv` used for the give type's derived `Eq` impl.
493+
fn param_env_for_derived_eq(tcx: TyCtxt<'_>, did: DefId, eq_trait_id: DefId) -> ParamEnv<'_> {
494+
// Initial map from generic index to param def.
495+
// Vec<(param_def, needs_eq)>
496+
let mut params = tcx
497+
.generics_of(did)
498+
.params
499+
.iter()
500+
.map(|p| (p, matches!(p.kind, GenericParamDefKind::Type { .. })))
501+
.collect::<Vec<_>>();
502+
503+
let ty_predicates = tcx.predicates_of(did).predicates;
504+
for (p, _) in ty_predicates {
505+
if let PredicateKind::Trait(p) = p.kind().skip_binder()
506+
&& p.trait_ref.def_id == eq_trait_id
507+
&& let ty::Param(self_ty) = p.trait_ref.self_ty().kind()
508+
&& p.constness == BoundConstness::NotConst
509+
{
510+
// Flag types which already have an `Eq` bound.
511+
params[self_ty.index as usize].1 = false;
512+
}
513+
}
514+
515+
ParamEnv::new(
516+
tcx.mk_predicates(ty_predicates.iter().map(|&(p, _)| p).chain(
517+
params.iter().filter(|&&(_, needs_eq)| needs_eq).map(|&(param, _)| {
518+
tcx.mk_predicate(Binder::dummy(PredicateKind::Trait(TraitPredicate {
519+
trait_ref: TraitRef::new(eq_trait_id, tcx.mk_substs([tcx.mk_param_from_def(param)].into_iter())),
520+
constness: BoundConstness::NotConst,
521+
polarity: ImplPolarity::Positive,
522+
})))
523+
}),
524+
)),
525+
Reveal::UserFacing,
526+
Constness::NotConst,
527+
)
528+
}

tests/ui/derive_partial_eq_without_eq.fixed

+41-19
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@
44
#![warn(clippy::derive_partial_eq_without_eq)]
55

66
// Don't warn on structs that aren't PartialEq
7-
struct NotPartialEq {
7+
pub struct NotPartialEq {
88
foo: u32,
99
bar: String,
1010
}
1111

1212
// Eq can be derived but is missing
1313
#[derive(Debug, PartialEq, Eq)]
14-
struct MissingEq {
14+
pub struct MissingEq {
1515
foo: u32,
1616
bar: String,
1717
}
1818

1919
// Eq is derived
2020
#[derive(PartialEq, Eq)]
21-
struct NotMissingEq {
21+
pub struct NotMissingEq {
2222
foo: u32,
2323
bar: String,
2424
}
2525

2626
// Eq is manually implemented
2727
#[derive(PartialEq)]
28-
struct ManualEqImpl {
28+
pub struct ManualEqImpl {
2929
foo: u32,
3030
bar: String,
3131
}
@@ -34,13 +34,13 @@ impl Eq for ManualEqImpl {}
3434

3535
// Cannot be Eq because f32 isn't Eq
3636
#[derive(PartialEq)]
37-
struct CannotBeEq {
37+
pub struct CannotBeEq {
3838
foo: u32,
3939
bar: f32,
4040
}
4141

4242
// Don't warn if PartialEq is manually implemented
43-
struct ManualPartialEqImpl {
43+
pub struct ManualPartialEqImpl {
4444
foo: u32,
4545
bar: String,
4646
}
@@ -52,53 +52,75 @@ impl PartialEq for ManualPartialEqImpl {
5252
}
5353

5454
// Generic fields should be properly checked for Eq-ness
55-
#[derive(PartialEq)]
56-
struct GenericNotEq<T: Eq, U: PartialEq> {
55+
#[derive(PartialEq, Eq)]
56+
pub struct GenericNotEq<T: Eq, U: PartialEq> {
5757
foo: T,
5858
bar: U,
5959
}
6060

6161
#[derive(PartialEq, Eq)]
62-
struct GenericEq<T: Eq, U: Eq> {
62+
pub struct GenericEq<T: Eq, U: Eq> {
6363
foo: T,
6464
bar: U,
6565
}
6666

6767
#[derive(PartialEq, Eq)]
68-
struct TupleStruct(u32);
68+
pub struct TupleStruct(u32);
6969

7070
#[derive(PartialEq, Eq)]
71-
struct GenericTupleStruct<T: Eq>(T);
71+
pub struct GenericTupleStruct<T: Eq>(T);
7272

7373
#[derive(PartialEq)]
74-
struct TupleStructNotEq(f32);
74+
pub struct TupleStructNotEq(f32);
7575

7676
#[derive(PartialEq, Eq)]
77-
enum Enum {
77+
pub enum Enum {
7878
Foo(u32),
7979
Bar { a: String, b: () },
8080
}
8181

8282
#[derive(PartialEq, Eq)]
83-
enum GenericEnum<T: Eq, U: Eq, V: Eq> {
83+
pub enum GenericEnum<T: Eq, U: Eq, V: Eq> {
8484
Foo(T),
8585
Bar { a: U, b: V },
8686
}
8787

8888
#[derive(PartialEq)]
89-
enum EnumNotEq {
89+
pub enum EnumNotEq {
9090
Foo(u32),
9191
Bar { a: String, b: f32 },
9292
}
9393

9494
// Ensure that rustfix works properly when `PartialEq` has other derives on either side
9595
#[derive(Debug, PartialEq, Eq, Clone)]
96-
struct RustFixWithOtherDerives;
96+
pub struct RustFixWithOtherDerives;
9797

98-
#[derive(PartialEq)]
99-
struct Generic<T>(T);
98+
#[derive(PartialEq, Eq)]
99+
pub struct Generic<T>(T);
100100

101101
#[derive(PartialEq, Eq)]
102-
struct GenericPhantom<T>(core::marker::PhantomData<T>);
102+
pub struct GenericPhantom<T>(core::marker::PhantomData<T>);
103+
104+
mod _hidden {
105+
#[derive(PartialEq, Eq)]
106+
pub struct Reexported;
107+
108+
#[derive(PartialEq, Eq)]
109+
pub struct InPubFn;
110+
111+
#[derive(PartialEq)]
112+
pub(crate) struct PubCrate;
113+
114+
#[derive(PartialEq)]
115+
pub(super) struct PubSuper;
116+
}
117+
118+
pub use _hidden::Reexported;
119+
pub fn _from_mod() -> _hidden::InPubFn {
120+
_hidden::InPubFn
121+
}
122+
123+
#[derive(PartialEq)]
124+
struct InternalTy;
103125

104126
fn main() {}

tests/ui/derive_partial_eq_without_eq.rs

+39-17
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@
44
#![warn(clippy::derive_partial_eq_without_eq)]
55

66
// Don't warn on structs that aren't PartialEq
7-
struct NotPartialEq {
7+
pub struct NotPartialEq {
88
foo: u32,
99
bar: String,
1010
}
1111

1212
// Eq can be derived but is missing
1313
#[derive(Debug, PartialEq)]
14-
struct MissingEq {
14+
pub struct MissingEq {
1515
foo: u32,
1616
bar: String,
1717
}
1818

1919
// Eq is derived
2020
#[derive(PartialEq, Eq)]
21-
struct NotMissingEq {
21+
pub struct NotMissingEq {
2222
foo: u32,
2323
bar: String,
2424
}
2525

2626
// Eq is manually implemented
2727
#[derive(PartialEq)]
28-
struct ManualEqImpl {
28+
pub struct ManualEqImpl {
2929
foo: u32,
3030
bar: String,
3131
}
@@ -34,13 +34,13 @@ impl Eq for ManualEqImpl {}
3434

3535
// Cannot be Eq because f32 isn't Eq
3636
#[derive(PartialEq)]
37-
struct CannotBeEq {
37+
pub struct CannotBeEq {
3838
foo: u32,
3939
bar: f32,
4040
}
4141

4242
// Don't warn if PartialEq is manually implemented
43-
struct ManualPartialEqImpl {
43+
pub struct ManualPartialEqImpl {
4444
foo: u32,
4545
bar: String,
4646
}
@@ -53,52 +53,74 @@ impl PartialEq for ManualPartialEqImpl {
5353

5454
// Generic fields should be properly checked for Eq-ness
5555
#[derive(PartialEq)]
56-
struct GenericNotEq<T: Eq, U: PartialEq> {
56+
pub struct GenericNotEq<T: Eq, U: PartialEq> {
5757
foo: T,
5858
bar: U,
5959
}
6060

6161
#[derive(PartialEq)]
62-
struct GenericEq<T: Eq, U: Eq> {
62+
pub struct GenericEq<T: Eq, U: Eq> {
6363
foo: T,
6464
bar: U,
6565
}
6666

6767
#[derive(PartialEq)]
68-
struct TupleStruct(u32);
68+
pub struct TupleStruct(u32);
6969

7070
#[derive(PartialEq)]
71-
struct GenericTupleStruct<T: Eq>(T);
71+
pub struct GenericTupleStruct<T: Eq>(T);
7272

7373
#[derive(PartialEq)]
74-
struct TupleStructNotEq(f32);
74+
pub struct TupleStructNotEq(f32);
7575

7676
#[derive(PartialEq)]
77-
enum Enum {
77+
pub enum Enum {
7878
Foo(u32),
7979
Bar { a: String, b: () },
8080
}
8181

8282
#[derive(PartialEq)]
83-
enum GenericEnum<T: Eq, U: Eq, V: Eq> {
83+
pub enum GenericEnum<T: Eq, U: Eq, V: Eq> {
8484
Foo(T),
8585
Bar { a: U, b: V },
8686
}
8787

8888
#[derive(PartialEq)]
89-
enum EnumNotEq {
89+
pub enum EnumNotEq {
9090
Foo(u32),
9191
Bar { a: String, b: f32 },
9292
}
9393

9494
// Ensure that rustfix works properly when `PartialEq` has other derives on either side
9595
#[derive(Debug, PartialEq, Clone)]
96-
struct RustFixWithOtherDerives;
96+
pub struct RustFixWithOtherDerives;
9797

9898
#[derive(PartialEq)]
99-
struct Generic<T>(T);
99+
pub struct Generic<T>(T);
100100

101101
#[derive(PartialEq, Eq)]
102-
struct GenericPhantom<T>(core::marker::PhantomData<T>);
102+
pub struct GenericPhantom<T>(core::marker::PhantomData<T>);
103+
104+
mod _hidden {
105+
#[derive(PartialEq)]
106+
pub struct Reexported;
107+
108+
#[derive(PartialEq)]
109+
pub struct InPubFn;
110+
111+
#[derive(PartialEq)]
112+
pub(crate) struct PubCrate;
113+
114+
#[derive(PartialEq)]
115+
pub(super) struct PubSuper;
116+
}
117+
118+
pub use _hidden::Reexported;
119+
pub fn _from_mod() -> _hidden::InPubFn {
120+
_hidden::InPubFn
121+
}
122+
123+
#[derive(PartialEq)]
124+
struct InternalTy;
103125

104126
fn main() {}

0 commit comments

Comments
 (0)