Skip to content

Commit 3c155dc

Browse files
authored
Rollup merge of #107465 - WaffleLapkin:has_allow_dead_code_or_lang_attr, r=Nilstrieb
`has_allow_dead_code_or_lang_attr` micro refactor r? `@Nilstrieb`
2 parents c3b1f54 + 48af3a9 commit 3c155dc

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

compiler/rustc_passes/src/dead.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -459,30 +459,32 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
459459
}
460460

461461
fn has_allow_dead_code_or_lang_attr(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
462-
if tcx.has_attr(def_id.to_def_id(), sym::lang) {
463-
return true;
462+
fn has_lang_attr(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
463+
tcx.has_attr(def_id.to_def_id(), sym::lang)
464+
// Stable attribute for #[lang = "panic_impl"]
465+
|| tcx.has_attr(def_id.to_def_id(), sym::panic_handler)
464466
}
465467

466-
// Stable attribute for #[lang = "panic_impl"]
467-
if tcx.has_attr(def_id.to_def_id(), sym::panic_handler) {
468-
return true;
468+
fn has_allow_dead_code(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
469+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
470+
tcx.lint_level_at_node(lint::builtin::DEAD_CODE, hir_id).0 == lint::Allow
469471
}
470472

471-
if tcx.def_kind(def_id).has_codegen_attrs() {
472-
let cg_attrs = tcx.codegen_fn_attrs(def_id);
473+
fn has_used_like_attr(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
474+
tcx.def_kind(def_id).has_codegen_attrs() && {
475+
let cg_attrs = tcx.codegen_fn_attrs(def_id);
473476

474-
// #[used], #[no_mangle], #[export_name], etc also keeps the item alive
475-
// forcefully, e.g., for placing it in a specific section.
476-
if cg_attrs.contains_extern_indicator()
477-
|| cg_attrs.flags.contains(CodegenFnAttrFlags::USED)
478-
|| cg_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
479-
{
480-
return true;
477+
// #[used], #[no_mangle], #[export_name], etc also keeps the item alive
478+
// forcefully, e.g., for placing it in a specific section.
479+
cg_attrs.contains_extern_indicator()
480+
|| cg_attrs.flags.contains(CodegenFnAttrFlags::USED)
481+
|| cg_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
481482
}
482483
}
483484

484-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
485-
tcx.lint_level_at_node(lint::builtin::DEAD_CODE, hir_id).0 == lint::Allow
485+
has_allow_dead_code(tcx, def_id)
486+
|| has_used_like_attr(tcx, def_id)
487+
|| has_lang_attr(tcx, def_id)
486488
}
487489

488490
// These check_* functions seeds items that

0 commit comments

Comments
 (0)