diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index fa9d0c7b1b7d4..7dc780721b1a2 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -759,6 +759,15 @@ fn maybe_record_as_seed<'tcx>( match tcx.def_kind(parent) { DefKind::Impl { of_trait: false } | DefKind::Trait => {} DefKind::Impl { of_trait: true } => { + if let Some(trait_item_def_id) = + tcx.associated_item(owner_id.def_id).trait_item_def_id + && let Some(trait_item_local_def_id) = trait_item_def_id.as_local() + && let Some(comes_from_allow) = + has_allow_dead_code_or_lang_attr(tcx, trait_item_local_def_id) + { + worklist.push((owner_id.def_id, comes_from_allow)); + } + // We only care about associated items of traits, // because they cannot be visited directly, // so we later mark them as live if their corresponding traits @@ -772,6 +781,15 @@ fn maybe_record_as_seed<'tcx>( } DefKind::Impl { of_trait: true } => { if allow_dead_code.is_none() { + if let Some(trait_def_id) = tcx + .impl_trait_ref(owner_id.def_id) + .and_then(|trait_ref| trait_ref.skip_binder().def_id.as_local()) + && let Some(comes_from_allow) = + has_allow_dead_code_or_lang_attr(tcx, trait_def_id) + { + worklist.push((owner_id.def_id, comes_from_allow)); + } + unsolved_items.push(owner_id.def_id); } } diff --git a/tests/ui/lint/dead-code/allow-unused-trait.rs b/tests/ui/lint/dead-code/allow-unused-trait.rs new file mode 100644 index 0000000000000..4eb63bd4d27ab --- /dev/null +++ b/tests/ui/lint/dead-code/allow-unused-trait.rs @@ -0,0 +1,29 @@ +//@ check-pass + +#![deny(dead_code)] + +#[allow(dead_code)] +trait Foo { + const FOO: u32; + type Baz; + fn foobar(); +} + +const fn bar(x: u32) -> u32 { + x +} + +struct Qux; + +struct FooBar; + +impl Foo for u32 { + const FOO: u32 = bar(0); + type Baz = Qux; + + fn foobar() { + let _ = FooBar; + } +} + +fn main() {}