From 7a9e2e2650b8fa76d3e55bce5f625c9cdd257826 Mon Sep 17 00:00:00 2001 From: Mu001999 Date: Fri, 18 Jul 2025 13:12:27 +0800 Subject: [PATCH] Impls and impl items inherit lint levels of the corresponding traits and trait items --- compiler/rustc_passes/src/dead.rs | 35 ++++++++++++++----- tests/ui/lint/dead-code/allow-unused-trait.rs | 29 +++++++++++++++ 2 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 tests/ui/lint/dead-code/allow-unused-trait.rs diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index d987041fe0e09..739f1eb94b15b 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -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)); } @@ -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 @@ -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)); + } + } _ => {} } } @@ -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)); } } 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() {}