diff options
| author | Urgau <urgau@numericable.fr> | 2025-06-08 20:36:47 +0200 |
|---|---|---|
| committer | Urgau <urgau@numericable.fr> | 2025-06-09 15:48:06 +0200 |
| commit | 33beaba7c8b78e9d4908e98dd1d6b4e15c6ba6f8 (patch) | |
| tree | 5aea6d87da7df129f8e0c8038d90e6b4272a853e | |
| parent | 8072811356a178dbdf8ca09b1635cfafd4661971 (diff) | |
| download | rust-33beaba7c8b78e9d4908e98dd1d6b4e15c6ba6f8.tar.gz rust-33beaba7c8b78e9d4908e98dd1d6b4e15c6ba6f8.zip | |
Always consider `const _` items as live for dead code analysis
| -rw-r--r-- | compiler/rustc_passes/src/dead.rs | 13 | ||||
| -rw-r--r-- | tests/ui/lint/dead-code/const-underscore-issue-142104.rs | 15 |
2 files changed, 27 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)); + } + } _ => {} } } diff --git a/tests/ui/lint/dead-code/const-underscore-issue-142104.rs b/tests/ui/lint/dead-code/const-underscore-issue-142104.rs new file mode 100644 index 00000000000..b255027e0f1 --- /dev/null +++ b/tests/ui/lint/dead-code/const-underscore-issue-142104.rs @@ -0,0 +1,15 @@ +//@ check-pass + +// This test makes sure we always considers `const _` items as live for dead code analysis. + +#![deny(dead_code)] + +const fn is_nonzero(x: u8) -> bool { + x != 0 +} + +const _: () = { + assert!(is_nonzero(2)); +}; + +fn main() {} |
