@@ -15,7 +15,7 @@ use rustc_hir::{Node, PatKind, TyKind};
15
15
use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
16
16
use rustc_middle:: middle:: privacy:: Level ;
17
17
use rustc_middle:: query:: Providers ;
18
- use rustc_middle:: ty:: { self , TyCtxt } ;
18
+ use rustc_middle:: ty:: { self , AssocItemContainer , TyCtxt } ;
19
19
use rustc_middle:: { bug, span_bug} ;
20
20
use rustc_session:: lint;
21
21
use rustc_session:: lint:: builtin:: DEAD_CODE ;
@@ -44,16 +44,25 @@ fn should_explore(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
44
44
)
45
45
}
46
46
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
+
47
51
fn ty_ref_to_pub_struct ( tcx : TyCtxt < ' _ > , ty : & hir:: Ty < ' _ > ) -> bool {
48
52
if let TyKind :: Path ( hir:: QPath :: Resolved ( _, path) ) = ty. kind
49
53
&& let Res :: Def ( def_kind, def_id) = path. res
50
54
&& def_id. is_local ( )
51
- && matches ! ( def_kind, DefKind :: Struct | DefKind :: Enum | DefKind :: Union )
52
55
{
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
+ } ;
56
63
}
64
+
65
+ true
57
66
}
58
67
59
68
/// Determine if a work from the worklist is coming from the a `#[allow]`
@@ -841,6 +850,18 @@ fn create_and_seed_worklist(
841
850
effective_vis
842
851
. is_public_at_level ( Level :: Reachable )
843
852
. 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
+ } )
844
865
. map ( |id| ( id, ComesFromAllowExpect :: No ) )
845
866
} )
846
867
// Seed entry point
0 commit comments