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
This commit is contained in:
commit
facc5da22c
2 changed files with 27 additions and 1 deletions
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
15
tests/ui/lint/dead-code/const-underscore-issue-142104.rs
Normal file
15
tests/ui/lint/dead-code/const-underscore-issue-142104.rs
Normal file
|
|
@ -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() {}
|
||||
Loading…
Add table
Add a link
Reference in a new issue