Skip to content

Commit a0821fb

Browse files
committed
Don't lint derive_partial_eq_without_eq on private types
1 parent d9ddce8 commit a0821fb

File tree

4 files changed

+95
-36
lines changed

4 files changed

+95
-36
lines changed

clippy_lints/src/derive.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use rustc_hir::{
1111
use rustc_lint::{LateContext, LateLintPass};
1212
use rustc_middle::hir::nested_filter;
1313
use rustc_middle::ty::subst::GenericArg;
14-
use rustc_middle::ty::{self, BoundConstness, ImplPolarity, ParamEnv, PredicateKind, TraitPredicate, TraitRef, Ty};
14+
use rustc_middle::ty::{
15+
self, BoundConstness, ImplPolarity, ParamEnv, PredicateKind, TraitPredicate, TraitRef, Ty, Visibility,
16+
};
1517
use rustc_session::{declare_lint_pass, declare_tool_lint};
1618
use rustc_span::source_map::Span;
1719
use rustc_span::sym;
@@ -459,6 +461,7 @@ impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> {
459461
fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_ref: &hir::TraitRef<'_>, ty: Ty<'tcx>) {
460462
if_chain! {
461463
if let ty::Adt(adt, substs) = ty.kind();
464+
if cx.tcx.visibility(adt.did()) == Visibility::Public;
462465
if let Some(eq_trait_def_id) = cx.tcx.get_diagnostic_item(sym::Eq);
463466
if let Some(peq_trait_def_id) = cx.tcx.get_diagnostic_item(sym::PartialEq);
464467
if let Some(def_id) = trait_ref.trait_def_id();

tests/ui/derive_partial_eq_without_eq.fixed

Lines changed: 39 additions & 17 deletions
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
}
@@ -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, 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

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, 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

Lines changed: 39 additions & 17 deletions
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() {}

tests/ui/derive_partial_eq_without_eq.stderr

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

45-
error: aborting due to 7 previous errors
45+
error: you are deriving `PartialEq` and can implement `Eq`
46+
--> $DIR/derive_partial_eq_without_eq.rs:105:14
47+
|
48+
LL | #[derive(PartialEq)]
49+
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
50+
51+
error: you are deriving `PartialEq` and can implement `Eq`
52+
--> $DIR/derive_partial_eq_without_eq.rs:108:14
53+
|
54+
LL | #[derive(PartialEq)]
55+
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
56+
57+
error: aborting due to 9 previous errors
4658

0 commit comments

Comments
 (0)