Skip to content

Impls and impl items inherit lint levels of the corresponding traits and trait items #144113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
35 changes: 27 additions & 8 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,15 @@ fn check_item<'tcx>(
{
worklist.push((id.owner_id.def_id, comes_from_allow));
} else if of_trait {
if let Some(trait_def_id) = tcx
.impl_trait_ref(id.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((id.owner_id.def_id, comes_from_allow));
}

unsolved_items.push((id, id.owner_id.def_id));
}

Expand All @@ -772,6 +781,14 @@ fn check_item<'tcx>(
{
worklist.push((local_def_id, comes_from_allow));
} else if of_trait {
if let Some(trait_item_def_id) = tcx.associated_item(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((local_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 Down Expand Up @@ -804,6 +821,13 @@ fn check_item<'tcx>(
worklist.push((id.owner_id.def_id, ComesFromAllowExpect::No));
}
}
DefKind::Trait => {
if let Some(comes_from_allow) =
has_allow_dead_code_or_lang_attr(tcx, id.owner_id.def_id)
{
worklist.push((id.owner_id.def_id, comes_from_allow));
}
}
_ => {}
}
}
Expand All @@ -813,14 +837,9 @@ fn check_trait_item(
worklist: &mut Vec<(LocalDefId, ComesFromAllowExpect)>,
id: hir::TraitItemId,
) {
use hir::TraitItemKind::{Const, Fn, Type};

let trait_item = tcx.hir_trait_item(id);
if matches!(trait_item.kind, Const(_, Some(_)) | Type(_, Some(_)) | Fn(..))
&& let Some(comes_from_allow) =
has_allow_dead_code_or_lang_attr(tcx, trait_item.owner_id.def_id)
{
worklist.push((trait_item.owner_id.def_id, comes_from_allow));
let trait_item_id = tcx.hir_trait_item(id).owner_id.def_id;
if let Some(comes_from_allow) = has_allow_dead_code_or_lang_attr(tcx, trait_item_id) {
worklist.push((trait_item_id, comes_from_allow));
}
}

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);
Copy link
Member

Choose a reason for hiding this comment

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

This test should probably also demonstrate the analogous behavior for fn items. And do we lint for unused structs in associated items?

type Baz = Qux;

fn foobar() {
let _ = FooBar;
}
}

fn main() {}
Loading