Skip to content

Commit 33beaba

Browse files
committed
Always consider const _ items as live for dead code analysis
1 parent 8072811 commit 33beaba

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

compiler/rustc_passes/src/dead.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_middle::ty::{self, TyCtxt};
2222
use rustc_middle::{bug, span_bug};
2323
use rustc_session::lint::builtin::DEAD_CODE;
2424
use rustc_session::lint::{self, LintExpectationId};
25-
use rustc_span::{Symbol, sym};
25+
use rustc_span::{Symbol, kw, sym};
2626

2727
use crate::errors::{
2828
ChangeFields, IgnoredDerivedImpls, MultipleDeadCodes, ParentInfo, UselessAssignment,
@@ -793,6 +793,17 @@ fn check_item<'tcx>(
793793
// global_asm! is always live.
794794
worklist.push((id.owner_id.def_id, ComesFromAllowExpect::No));
795795
}
796+
DefKind::Const => {
797+
let item = tcx.hir_item(id);
798+
if let hir::ItemKind::Const(ident, ..) = item.kind
799+
&& ident.name == kw::Underscore
800+
{
801+
// `const _` is always live, as that syntax only exists for the side effects
802+
// of type checking and evaluating the constant expression, and marking them
803+
// as dead code would defeat that purpose.
804+
worklist.push((id.owner_id.def_id, ComesFromAllowExpect::No));
805+
}
806+
}
796807
_ => {}
797808
}
798809
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ check-pass
2+
3+
// This test makes sure we always considers `const _` items as live for dead code analysis.
4+
5+
#![deny(dead_code)]
6+
7+
const fn is_nonzero(x: u8) -> bool {
8+
x != 0
9+
}
10+
11+
const _: () = {
12+
assert!(is_nonzero(2));
13+
};
14+
15+
fn main() {}

0 commit comments

Comments
 (0)