Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,57 @@ impl<'tcx> DeadVisitor<'tcx> {
(level_spec.level(), level_spec.lint_id())
}

fn fulfill_dead_code_expectations(&self, def_id: LocalDefId, include: bool) {
let fulfill_if_expected = |node: DefId| {
// Only consider local, dead symbols where lint level carries an expectation
if let Some(node) = node.as_local()
&& !self.live_symbols.contains(&node)
&& let (_, Some(expectation)) = self.def_lint_level_plus(node)
{
// Same mechanism as LintContext::fulfill_expectation.
self.tcx
.dcx()
.struct_expect(
"this is a dummy diagnostic, to submit and store an expectation",
expectation.into(),
)
.emit();
}
};

if include {
fulfill_if_expected(def_id.to_def_id());
}

match self.tcx.def_kind(def_id) {
DefKind::Struct | DefKind::Union | DefKind::Enum => {

@JonathanBrouwer JonathanBrouwer Sep 18, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of the amount of special-casing needed here (and afraid we missed something) but this seems to be an existing issue of how the dead code lint works, starting from free items, and not something that is worsened significantly by this PR

View changes since the review

let adt = self.tcx.adt_def(def_id);
for variant in adt.variants() {
if variant.def_id != def_id.to_def_id() {
fulfill_if_expected(variant.def_id);
}
for field in &variant.fields {
fulfill_if_expected(field.did);
}
}
}
DefKind::Variant => {
let parent_enum = self.tcx.local_parent(def_id);
let variant = self.tcx.adt_def(parent_enum).variant_with_id(def_id.to_def_id());
//Check to see if fields carry expectation
for field in &variant.fields {
fulfill_if_expected(field.did);
}
}
DefKind::Trait => {
for &assoc_def_id in self.tcx.associated_item_def_ids(def_id) {
fulfill_if_expected(assoc_def_id);
}
}
_ => {}
}
}

fn dead_code_pub_in_binary_note(&self) -> Option<DeadCodePubInBinaryNote> {
self.target_lint.name.eq(DEAD_CODE_PUB_IN_BINARY.name).then_some(DeadCodePubInBinaryNote)
}
Expand Down Expand Up @@ -1405,10 +1456,12 @@ fn lint_dead_codes<'tcx>(
if !live_symbols.contains(&item.owner_id.def_id) {
let parent = tcx.local_parent(item.owner_id.def_id);
if parent != module.to_local_def_id() && !live_symbols.contains(&parent) {
// We already have diagnosed something.
// We already have diagnosed something, but check parent's field for #[expect]
visitor.fulfill_dead_code_expectations(item.owner_id.def_id, true);
continue;
}
visitor.check_definition(item.owner_id.def_id);
visitor.fulfill_dead_code_expectations(item.owner_id.def_id, false);
continue;
}

Expand All @@ -1422,6 +1475,7 @@ fn lint_dead_codes<'tcx>(
// Record to group diagnostics.
let level_plus = visitor.def_lint_level_plus(def_id);
dead_variants.push(DeadItem { def_id, name: variant.name, level_plus });
visitor.fulfill_dead_code_expectations(def_id, false);
continue;
}

Expand Down
56 changes: 56 additions & 0 deletions tests/ui/lint/rfc-2383-lint-reason/expect_inside_dead_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//@ check-pass
// Expectations on dead code covered by a dead parent's diagnostic must still be fulfilled.

#[cfg_attr(all(), expect(unused))]
struct Foo {
#[expect(unused)]
x: u32,
}

#[expect(dead_code)]
struct Direct {
#[expect(dead_code)]
x: u32,
}

#[expect(unused)]
enum DeadEnum {
Variant {
#[expect(unused)]
x: u32,
},
}

enum PartiallyDead {
Used,
#[expect(unused)]
Dead {
#[expect(unused)]
x: u32,
},
}

#[expect(unused)]
fn dead_outer() {
#[expect(unused)]
fn dead_inner() {}
}

#[expect(unused)]
trait DeadTrait {
#[expect(unused)]
fn dead_method(&self);
}

// `y` is read, so this expectation must stay unfulfilled.
struct Live {
#[expect(unused)]
//~^ WARNING this lint expectation is unfulfilled
//~| NOTE `#[warn(unfulfilled_lint_expectations)]` on by default
y: u32,
}

fn main() {
let _ = PartiallyDead::Used;
let _ = Live { y: 1 }.y;
}
10 changes: 10 additions & 0 deletions tests/ui/lint/rfc-2383-lint-reason/expect_inside_dead_code.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
warning: this lint expectation is unfulfilled
--> $DIR/expect_inside_dead_code.rs:47:14
|
LL | #[expect(unused)]
| ^^^^^^
|
= note: `#[warn(unfulfilled_lint_expectations)]` on by default

warning: 1 warning emitted

Loading