diff --git a/clippy_lints/src/derive.rs b/clippy_lints/src/derive.rs index 99347ebad..a98060fe9 100644 --- a/clippy_lints/src/derive.rs +++ b/clippy_lints/src/derive.rs @@ -11,7 +11,9 @@ use rustc_hir::{ use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::nested_filter; use rustc_middle::ty::subst::GenericArg; -use rustc_middle::ty::{self, BoundConstness, ImplPolarity, ParamEnv, PredicateKind, TraitPredicate, TraitRef, Ty}; +use rustc_middle::ty::{ + self, BoundConstness, ImplPolarity, ParamEnv, PredicateKind, TraitPredicate, TraitRef, Ty, Visibility, +}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Span; use rustc_span::sym; @@ -459,6 +461,7 @@ impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> { fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_ref: &hir::TraitRef<'_>, ty: Ty<'tcx>) { if_chain! { if let ty::Adt(adt, substs) = ty.kind(); + if cx.tcx.visibility(adt.did()) == Visibility::Public; if let Some(eq_trait_def_id) = cx.tcx.get_diagnostic_item(sym::Eq); if let Some(peq_trait_def_id) = cx.tcx.get_diagnostic_item(sym::PartialEq); if let Some(def_id) = trait_ref.trait_def_id(); diff --git a/tests/ui/derive_partial_eq_without_eq.fixed b/tests/ui/derive_partial_eq_without_eq.fixed index 012780258..25e7050d0 100644 --- a/tests/ui/derive_partial_eq_without_eq.fixed +++ b/tests/ui/derive_partial_eq_without_eq.fixed @@ -4,28 +4,28 @@ #![warn(clippy::derive_partial_eq_without_eq)] // Don't warn on structs that aren't PartialEq -struct NotPartialEq { +pub struct NotPartialEq { foo: u32, bar: String, } // Eq can be derived but is missing #[derive(Debug, PartialEq, Eq)] -struct MissingEq { +pub struct MissingEq { foo: u32, bar: String, } // Eq is derived #[derive(PartialEq, Eq)] -struct NotMissingEq { +pub struct NotMissingEq { foo: u32, bar: String, } // Eq is manually implemented #[derive(PartialEq)] -struct ManualEqImpl { +pub struct ManualEqImpl { foo: u32, bar: String, } @@ -34,13 +34,13 @@ impl Eq for ManualEqImpl {} // Cannot be Eq because f32 isn't Eq #[derive(PartialEq)] -struct CannotBeEq { +pub struct CannotBeEq { foo: u32, bar: f32, } // Don't warn if PartialEq is manually implemented -struct ManualPartialEqImpl { +pub struct ManualPartialEqImpl { foo: u32, bar: String, } @@ -53,52 +53,74 @@ impl PartialEq for ManualPartialEqImpl { // Generic fields should be properly checked for Eq-ness #[derive(PartialEq)] -struct GenericNotEq { +pub struct GenericNotEq { foo: T, bar: U, } #[derive(PartialEq, Eq)] -struct GenericEq { +pub struct GenericEq { foo: T, bar: U, } #[derive(PartialEq, Eq)] -struct TupleStruct(u32); +pub struct TupleStruct(u32); #[derive(PartialEq, Eq)] -struct GenericTupleStruct(T); +pub struct GenericTupleStruct(T); #[derive(PartialEq)] -struct TupleStructNotEq(f32); +pub struct TupleStructNotEq(f32); #[derive(PartialEq, Eq)] -enum Enum { +pub enum Enum { Foo(u32), Bar { a: String, b: () }, } #[derive(PartialEq, Eq)] -enum GenericEnum { +pub enum GenericEnum { Foo(T), Bar { a: U, b: V }, } #[derive(PartialEq)] -enum EnumNotEq { +pub enum EnumNotEq { Foo(u32), Bar { a: String, b: f32 }, } // Ensure that rustfix works properly when `PartialEq` has other derives on either side #[derive(Debug, PartialEq, Eq, Clone)] -struct RustFixWithOtherDerives; +pub struct RustFixWithOtherDerives; #[derive(PartialEq)] -struct Generic(T); +pub struct Generic(T); #[derive(PartialEq, Eq)] -struct GenericPhantom(core::marker::PhantomData); +pub struct GenericPhantom(core::marker::PhantomData); + +mod _hidden { + #[derive(PartialEq, Eq)] + pub struct Reexported; + + #[derive(PartialEq, Eq)] + pub struct InPubFn; + + #[derive(PartialEq)] + pub(crate) struct PubCrate; + + #[derive(PartialEq)] + pub(super) struct PubSuper; +} + +pub use _hidden::Reexported; +pub fn _from_mod() -> _hidden::InPubFn { + _hidden::InPubFn +} + +#[derive(PartialEq)] +struct InternalTy; fn main() {} diff --git a/tests/ui/derive_partial_eq_without_eq.rs b/tests/ui/derive_partial_eq_without_eq.rs index fc8285b0c..88d6fbd1a 100644 --- a/tests/ui/derive_partial_eq_without_eq.rs +++ b/tests/ui/derive_partial_eq_without_eq.rs @@ -4,28 +4,28 @@ #![warn(clippy::derive_partial_eq_without_eq)] // Don't warn on structs that aren't PartialEq -struct NotPartialEq { +pub struct NotPartialEq { foo: u32, bar: String, } // Eq can be derived but is missing #[derive(Debug, PartialEq)] -struct MissingEq { +pub struct MissingEq { foo: u32, bar: String, } // Eq is derived #[derive(PartialEq, Eq)] -struct NotMissingEq { +pub struct NotMissingEq { foo: u32, bar: String, } // Eq is manually implemented #[derive(PartialEq)] -struct ManualEqImpl { +pub struct ManualEqImpl { foo: u32, bar: String, } @@ -34,13 +34,13 @@ impl Eq for ManualEqImpl {} // Cannot be Eq because f32 isn't Eq #[derive(PartialEq)] -struct CannotBeEq { +pub struct CannotBeEq { foo: u32, bar: f32, } // Don't warn if PartialEq is manually implemented -struct ManualPartialEqImpl { +pub struct ManualPartialEqImpl { foo: u32, bar: String, } @@ -53,52 +53,74 @@ impl PartialEq for ManualPartialEqImpl { // Generic fields should be properly checked for Eq-ness #[derive(PartialEq)] -struct GenericNotEq { +pub struct GenericNotEq { foo: T, bar: U, } #[derive(PartialEq)] -struct GenericEq { +pub struct GenericEq { foo: T, bar: U, } #[derive(PartialEq)] -struct TupleStruct(u32); +pub struct TupleStruct(u32); #[derive(PartialEq)] -struct GenericTupleStruct(T); +pub struct GenericTupleStruct(T); #[derive(PartialEq)] -struct TupleStructNotEq(f32); +pub struct TupleStructNotEq(f32); #[derive(PartialEq)] -enum Enum { +pub enum Enum { Foo(u32), Bar { a: String, b: () }, } #[derive(PartialEq)] -enum GenericEnum { +pub enum GenericEnum { Foo(T), Bar { a: U, b: V }, } #[derive(PartialEq)] -enum EnumNotEq { +pub enum EnumNotEq { Foo(u32), Bar { a: String, b: f32 }, } // Ensure that rustfix works properly when `PartialEq` has other derives on either side #[derive(Debug, PartialEq, Clone)] -struct RustFixWithOtherDerives; +pub struct RustFixWithOtherDerives; #[derive(PartialEq)] -struct Generic(T); +pub struct Generic(T); #[derive(PartialEq, Eq)] -struct GenericPhantom(core::marker::PhantomData); +pub struct GenericPhantom(core::marker::PhantomData); + +mod _hidden { + #[derive(PartialEq)] + pub struct Reexported; + + #[derive(PartialEq)] + pub struct InPubFn; + + #[derive(PartialEq)] + pub(crate) struct PubCrate; + + #[derive(PartialEq)] + pub(super) struct PubSuper; +} + +pub use _hidden::Reexported; +pub fn _from_mod() -> _hidden::InPubFn { + _hidden::InPubFn +} + +#[derive(PartialEq)] +struct InternalTy; fn main() {} diff --git a/tests/ui/derive_partial_eq_without_eq.stderr b/tests/ui/derive_partial_eq_without_eq.stderr index bf5516589..b762efc47 100644 --- a/tests/ui/derive_partial_eq_without_eq.stderr +++ b/tests/ui/derive_partial_eq_without_eq.stderr @@ -42,5 +42,17 @@ error: you are deriving `PartialEq` and can implement `Eq` LL | #[derive(Debug, PartialEq, Clone)] | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` -error: aborting due to 7 previous errors +error: you are deriving `PartialEq` and can implement `Eq` + --> $DIR/derive_partial_eq_without_eq.rs:105:14 + | +LL | #[derive(PartialEq)] + | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` + +error: you are deriving `PartialEq` and can implement `Eq` + --> $DIR/derive_partial_eq_without_eq.rs:108:14 + | +LL | #[derive(PartialEq)] + | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` + +error: aborting due to 9 previous errors