Skip to content
Open
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
18 changes: 18 additions & 0 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
Expand Down
29 changes: 29 additions & 0 deletions tests/ui/lint/dead-code/allow-unused-trait.rs
Original file line number Diff line number Diff line change
@@ -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() {}
Loading