@@ -20,12 +20,14 @@ use rustc_middle::middle::privacy::Level;
2020use rustc_middle:: query:: Providers ;
2121use rustc_middle:: ty:: { self , AssocTag , TyCtxt } ;
2222use rustc_middle:: { bug, span_bug} ;
23- use rustc_session:: lint:: builtin:: DEAD_CODE ;
23+ use rustc_session:: config:: CrateType ;
24+ use rustc_session:: lint:: builtin:: { DEAD_CODE , UNUSED_PUB_ITEMS_IN_BINARY } ;
2425use rustc_session:: lint:: { self , Lint , LintExpectationId } ;
2526use rustc_span:: { Symbol , kw} ;
2627
2728use crate :: errors:: {
28- ChangeFields , IgnoredDerivedImpls , MultipleDeadCodes , ParentInfo , UselessAssignment ,
29+ ChangeFields , IgnoredDerivedImpls , MultipleDeadCodes , ParentInfo , UnusedPubItemsInBinaryNote ,
30+ UselessAssignment ,
2931} ;
3032
3133/// Any local definition that may call something in its body block should be explored. For example,
@@ -1086,6 +1088,13 @@ impl<'tcx> DeadVisitor<'tcx> {
10861088 ( level. level , level. lint_id )
10871089 }
10881090
1091+ fn unused_pub_items_in_binary_note ( & self ) -> Option < UnusedPubItemsInBinaryNote > {
1092+ self . target_lint
1093+ . name
1094+ . eq ( UNUSED_PUB_ITEMS_IN_BINARY . name )
1095+ . then_some ( UnusedPubItemsInBinaryNote )
1096+ }
1097+
10891098 // # Panics
10901099 // All `dead_codes` must have the same lint level, otherwise we will intentionally ICE.
10911100 // This is because we emit a multi-spanned lint using the lint level of the `dead_codes`'s
@@ -1197,6 +1206,7 @@ impl<'tcx> DeadVisitor<'tcx> {
11971206 descr,
11981207 participle,
11991208 name_list,
1209+ unused_pub_items_in_binary_note : self . unused_pub_items_in_binary_note ( ) ,
12001210 change_fields_suggestion : fields_suggestion,
12011211 parent_info,
12021212 ignored_derived_impls,
@@ -1233,6 +1243,7 @@ impl<'tcx> DeadVisitor<'tcx> {
12331243 descr,
12341244 participle,
12351245 name_list,
1246+ unused_pub_items_in_binary_note : self . unused_pub_items_in_binary_note ( ) ,
12361247 parent_info,
12371248 ignored_derived_impls,
12381249 enum_variants_with_same_name,
@@ -1308,14 +1319,33 @@ impl<'tcx> DeadVisitor<'tcx> {
13081319}
13091320
13101321fn check_mod_deathness ( tcx : TyCtxt < ' _ > , module : LocalModDefId ) {
1311- let Ok ( DeadCodeLivenessSummary { final_result , .. } ) =
1322+ let Ok ( DeadCodeLivenessSummary { pre_deferred_seeding , final_result } ) =
13121323 tcx. live_symbols_and_ignored_derived_traits ( ( ) ) . as_ref ( )
13131324 else {
13141325 return ;
13151326 } ;
13161327
13171328 let module_items = tcx. hir_module_items ( module) ;
13181329
1330+ if tcx. crate_types ( ) . contains ( & CrateType :: Executable ) {
1331+ let is_unused_pub = |def_id : LocalDefId | {
1332+ tcx. effective_visibilities ( ( ) ) . is_public_at_level ( def_id, Level :: Reachable )
1333+ && !pre_deferred_seeding. live_symbols . contains ( & def_id)
1334+ } ;
1335+
1336+ lint_dead_code_or_unused_pub_items_in_binary (
1337+ tcx,
1338+ UNUSED_PUB_ITEMS_IN_BINARY ,
1339+ module,
1340+ & pre_deferred_seeding. live_symbols ,
1341+ & pre_deferred_seeding. ignored_derived_traits ,
1342+ module_items. free_items ( ) . filter ( |free_item| is_unused_pub ( free_item. owner_id . def_id ) ) ,
1343+ module_items
1344+ . foreign_items ( )
1345+ . filter ( |foreign_item| is_unused_pub ( foreign_item. owner_id . def_id ) ) ,
1346+ ) ;
1347+ }
1348+
13191349 lint_dead_code_or_unused_pub_items_in_binary (
13201350 tcx,
13211351 DEAD_CODE ,
0 commit comments