@@ -15,7 +15,7 @@ use rustc_hir::{Node, PatKind, TyKind};
1515use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
1616use rustc_middle:: middle:: privacy:: Level ;
1717use rustc_middle:: query:: Providers ;
18- use rustc_middle:: ty:: { self , TyCtxt } ;
18+ use rustc_middle:: ty:: { self , AssocItemContainer , TyCtxt } ;
1919use rustc_middle:: { bug, span_bug} ;
2020use rustc_session:: lint;
2121use rustc_session:: lint:: builtin:: DEAD_CODE ;
@@ -44,16 +44,25 @@ fn should_explore(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
4444 )
4545}
4646
47+ fn struct_all_fields_are_public ( tcx : TyCtxt < ' _ > , id : DefId ) -> bool {
48+ tcx. adt_def ( id) . all_fields ( ) . all ( |field| field. vis . is_public ( ) )
49+ }
50+
4751fn ty_ref_to_pub_struct ( tcx : TyCtxt < ' _ > , ty : & hir:: Ty < ' _ > ) -> bool {
4852 if let TyKind :: Path ( hir:: QPath :: Resolved ( _, path) ) = ty. kind
4953 && let Res :: Def ( def_kind, def_id) = path. res
5054 && def_id. is_local ( )
51- && matches ! ( def_kind, DefKind :: Struct | DefKind :: Enum | DefKind :: Union )
5255 {
53- tcx. visibility ( def_id) . is_public ( )
54- } else {
55- true
56+ return match def_kind {
57+ DefKind :: Enum | DefKind :: Union => tcx. visibility ( def_id) . is_public ( ) ,
58+ DefKind :: Struct => {
59+ tcx. visibility ( def_id) . is_public ( ) && struct_all_fields_are_public ( tcx, def_id)
60+ }
61+ _ => true ,
62+ } ;
5663 }
64+
65+ true
5766}
5867
5968/// Determine if a work from the worklist is coming from the a `#[allow]`
@@ -841,6 +850,18 @@ fn create_and_seed_worklist(
841850 effective_vis
842851 . is_public_at_level ( Level :: Reachable )
843852 . then_some ( id)
853+ . filter ( |& id|
854+ // checks impl-of-traits, impl-item-of-trait-items and pub structs with all public fields later
855+ match tcx. def_kind ( id) {
856+ DefKind :: Impl { of_trait : true } => false ,
857+ DefKind :: AssocFn => {
858+ let assoc_item = tcx. associated_item ( id) ;
859+ !matches ! ( assoc_item. container, AssocItemContainer :: ImplContainer )
860+ || assoc_item. trait_item_def_id . is_none ( )
861+ }
862+ DefKind :: Struct => struct_all_fields_are_public ( tcx, id. to_def_id ( ) ) ,
863+ _ => true
864+ } )
844865 . map ( |id| ( id, ComesFromAllowExpect :: No ) )
845866 } )
846867 // Seed entry point
0 commit comments