diff options
| author | Trevor Gross <t.gross35@gmail.com> | 2025-06-09 12:17:54 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-09 12:17:54 -0500 |
| commit | facc5da22cea0455e11d8385ab34c8a26787e529 (patch) | |
| tree | d5e40760ad94c80b2f04e0f8f41585048269c705 /compiler | |
| parent | ab87ed150bb3435e8d5487a6e950045f5215cecf (diff) | |
| parent | 33beaba7c8b78e9d4908e98dd1d6b4e15c6ba6f8 (diff) | |
| download | rust-facc5da22cea0455e11d8385ab34c8a26787e529.tar.gz rust-facc5da22cea0455e11d8385ab34c8a26787e529.zip | |
Rollup merge of #142208 - Urgau:dead_code-const_, r=petrochenkov
Always consider `const _` items as live for dead code analysis
This PR alters dead code analysis to always consider `const _: () = { ... };` to be live.
This doesn't address the `_name` pattern from https://github.com/rust-lang/rust/issues/142075.
Fixes https://github.com/rust-lang/rust/issues/142104
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_passes/src/dead.rs | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 6b82252f32c..e597c819a3a 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -22,7 +22,7 @@ use rustc_middle::ty::{self, TyCtxt}; use rustc_middle::{bug, span_bug}; use rustc_session::lint::builtin::DEAD_CODE; use rustc_session::lint::{self, LintExpectationId}; -use rustc_span::{Symbol, sym}; +use rustc_span::{Symbol, kw, sym}; use crate::errors::{ ChangeFields, IgnoredDerivedImpls, MultipleDeadCodes, ParentInfo, UselessAssignment, @@ -793,6 +793,17 @@ fn check_item<'tcx>( // global_asm! is always live. worklist.push((id.owner_id.def_id, ComesFromAllowExpect::No)); } + DefKind::Const => { + let item = tcx.hir_item(id); + if let hir::ItemKind::Const(ident, ..) = item.kind + && ident.name == kw::Underscore + { + // `const _` is always live, as that syntax only exists for the side effects + // of type checking and evaluating the constant expression, and marking them + // as dead code would defeat that purpose. + worklist.push((id.owner_id.def_id, ComesFromAllowExpect::No)); + } + } _ => {} } } |
