Skip to content

Commit 7a9e2e2

Browse files
committed
Impls and impl items inherit lint levels of the corresponding traits and trait items
1 parent 8c12d76 commit 7a9e2e2

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

compiler/rustc_passes/src/dead.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,15 @@ fn check_item<'tcx>(
762762
{
763763
worklist.push((id.owner_id.def_id, comes_from_allow));
764764
} else if of_trait {
765+
if let Some(trait_def_id) = tcx
766+
.impl_trait_ref(id.owner_id.def_id)
767+
.and_then(|trait_ref| trait_ref.skip_binder().def_id.as_local())
768+
&& let Some(comes_from_allow) =
769+
has_allow_dead_code_or_lang_attr(tcx, trait_def_id)
770+
{
771+
worklist.push((id.owner_id.def_id, comes_from_allow));
772+
}
773+
765774
unsolved_items.push((id, id.owner_id.def_id));
766775
}
767776

@@ -772,6 +781,14 @@ fn check_item<'tcx>(
772781
{
773782
worklist.push((local_def_id, comes_from_allow));
774783
} else if of_trait {
784+
if let Some(trait_item_def_id) = tcx.associated_item(def_id).trait_item_def_id
785+
&& let Some(trait_item_local_def_id) = trait_item_def_id.as_local()
786+
&& let Some(comes_from_allow) =
787+
has_allow_dead_code_or_lang_attr(tcx, trait_item_local_def_id)
788+
{
789+
worklist.push((local_def_id, comes_from_allow));
790+
}
791+
775792
// We only care about associated items of traits,
776793
// because they cannot be visited directly,
777794
// so we later mark them as live if their corresponding traits
@@ -804,6 +821,13 @@ fn check_item<'tcx>(
804821
worklist.push((id.owner_id.def_id, ComesFromAllowExpect::No));
805822
}
806823
}
824+
DefKind::Trait => {
825+
if let Some(comes_from_allow) =
826+
has_allow_dead_code_or_lang_attr(tcx, id.owner_id.def_id)
827+
{
828+
worklist.push((id.owner_id.def_id, comes_from_allow));
829+
}
830+
}
807831
_ => {}
808832
}
809833
}
@@ -813,14 +837,9 @@ fn check_trait_item(
813837
worklist: &mut Vec<(LocalDefId, ComesFromAllowExpect)>,
814838
id: hir::TraitItemId,
815839
) {
816-
use hir::TraitItemKind::{Const, Fn, Type};
817-
818-
let trait_item = tcx.hir_trait_item(id);
819-
if matches!(trait_item.kind, Const(_, Some(_)) | Type(_, Some(_)) | Fn(..))
820-
&& let Some(comes_from_allow) =
821-
has_allow_dead_code_or_lang_attr(tcx, trait_item.owner_id.def_id)
822-
{
823-
worklist.push((trait_item.owner_id.def_id, comes_from_allow));
840+
let trait_item_id = tcx.hir_trait_item(id).owner_id.def_id;
841+
if let Some(comes_from_allow) = has_allow_dead_code_or_lang_attr(tcx, trait_item_id) {
842+
worklist.push((trait_item_id, comes_from_allow));
824843
}
825844
}
826845

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@ check-pass
2+
3+
#![deny(dead_code)]
4+
5+
#[allow(dead_code)]
6+
trait Foo {
7+
const FOO: u32;
8+
type Baz;
9+
fn foobar();
10+
}
11+
12+
const fn bar(x: u32) -> u32 {
13+
x
14+
}
15+
16+
struct Qux;
17+
18+
struct FooBar;
19+
20+
impl Foo for u32 {
21+
const FOO: u32 = bar(0);
22+
type Baz = Qux;
23+
24+
fn foobar() {
25+
let _ = FooBar;
26+
}
27+
}
28+
29+
fn main() {}

0 commit comments

Comments
 (0)