Extend rules of dead code analysis for impls for adts to impls for ty… · rust-lang/rust@8449d10 (original) (raw)
`@@ -54,7 +54,24 @@ impl Publicness {
`
54
54
`}
`
55
55
`}
`
56
56
``
57
``
`-
fn struct_all_fields_are_public(tcx: TyCtxt<'_>, id: DefId) -> bool {
`
``
57
`+
fn adt_of<'tcx>(ty: &hir::Ty<'tcx>) -> Option<(LocalDefId, DefKind)> {
`
``
58
`+
match ty.kind {
`
``
59
`+
TyKind::Path(hir::QPath::Resolved(_, path)) => {
`
``
60
`+
if let Res::Def(def_kind, def_id) = path.res
`
``
61
`+
&& let Some(local_def_id) = def_id.as_local()
`
``
62
`+
{
`
``
63
`+
Some((local_def_id, def_kind))
`
``
64
`+
} else {
`
``
65
`+
None
`
``
66
`+
}
`
``
67
`+
}
`
``
68
`+
TyKind::Slice(ty) | TyKind::Array(ty, _) => adt_of(ty),
`
``
69
`+
TyKind::Ptr(ty) | TyKind::Ref(_, ty) => adt_of(ty.ty),
`
``
70
`+
_ => None,
`
``
71
`+
}
`
``
72
`+
}
`
``
73
+
``
74
`+
fn struct_all_fields_are_public(tcx: TyCtxt<'_>, id: LocalDefId) -> bool {
`
58
75
`// treat PhantomData and positional ZST as public,
`
59
76
`// we don't want to lint types which only have them,
`
60
77
`// cause it's a common way to use such types to check things like well-formedness
`
`@@ -79,10 +96,7 @@ fn struct_all_fields_are_public(tcx: TyCtxt<'_>, id: DefId) -> bool {
`
79
96
`/// for enum and union, just check they are public,
`
80
97
`/// and doesn't solve types like &T for now, just skip them
`
81
98
`fn ty_ref_to_pub_struct(tcx: TyCtxt<'_>, ty: &hir::Ty<'_>) -> Publicness {
`
82
``
`-
if let TyKind::Path(hir::QPath::Resolved(_, path)) = ty.kind
`
83
``
`-
&& let Res::Def(def_kind, def_id) = path.res
`
84
``
`-
&& def_id.is_local()
`
85
``
`-
{
`
``
99
`+
if let Some((def_id, def_kind)) = adt_of(ty) {
`
86
100
`return match def_kind {
`
87
101
`DefKind::Enum | DefKind::Union => {
`
88
102
`let ty_is_public = tcx.visibility(def_id).is_public();
`
`@@ -584,10 +598,8 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
`
584
598
`}
`
585
599
``
586
600
`fn impl_item_with_used_self(&mut self, impl_id: hir::ItemId, impl_item_id: LocalDefId) -> bool {
`
587
``
`-
if let TyKind::Path(hir::QPath::Resolved(_, path)) =
`
588
``
`-
self.tcx.hir().item(impl_id).expect_impl().self_ty.kind
`
589
``
`-
&& let Res::Def(def_kind, def_id) = path.res
`
590
``
`-
&& let Some(local_def_id) = def_id.as_local()
`
``
601
`+
if let Some((local_def_id, def_kind)) =
`
``
602
`+
adt_of(self.tcx.hir().item(impl_id).expect_impl().self_ty)
`
591
603
` && matches!(def_kind, DefKind::Struct | DefKind::Enum | DefKind::Union)
`
592
604
`{
`
593
605
`if let Some(trait_item_id) = self.tcx.associated_item(impl_item_id).trait_item_def_id
`
`@@ -931,7 +943,7 @@ fn create_and_seed_worklist(
`
931
943
`match tcx.def_kind(id) {
`
932
944
`DefKind::Impl { .. } => false,
`
933
945
`DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn => !matches!(tcx.associated_item(id).container, AssocItemContainer::ImplContainer),
`
934
``
`-
DefKind::Struct => struct_all_fields_are_public(tcx, id.to_def_id()) || has_allow_dead_code_or_lang_attr(tcx, id).is_some(),
`
``
946
`+
DefKind::Struct => struct_all_fields_are_public(tcx, id) || has_allow_dead_code_or_lang_attr(tcx, id).is_some(),
`
935
947
` _ => true
`
936
948
`})
`
937
949
`.map(|id| (id, ComesFromAllowExpect::No))
`