From 9bc0e5b23c4ae397a6dbc32c3917605f17df2910 Mon Sep 17 00:00:00 2001 From: r0cky Date: Sun, 26 May 2024 23:11:36 +0800 Subject: [PATCH] Detect unused associated constants in traits --- compiler/rustc_passes/src/dead.rs | 23 ++++++------------- tests/ui/lint/dead-code/unused_assoc_const.rs | 17 ++++++++++++++ .../lint/dead-code/unused_assoc_const.stderr | 16 +++++++++++++ 3 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 tests/ui/lint/dead-code/unused_assoc_const.rs create mode 100644 tests/ui/lint/dead-code/unused_assoc_const.stderr diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index f1e6528581ed..88c335435434 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -786,18 +786,9 @@ fn check_item<'tcx>( // for trait impl blocks, // mark the method live if the self_ty is public, // or the method is public and may construct self - if of_trait - && (!matches!(tcx.def_kind(local_def_id), DefKind::AssocFn) - || tcx.visibility(local_def_id).is_public() - && (ty_is_pure_pub || may_construct_self)) - { - // if the impl item is public, - // and the ty may be constructed or can be constructed in foreign crates, - // mark the impl item live - worklist.push((local_def_id, ComesFromAllowExpect::No)); - } else if !of_trait - && tcx.visibility(local_def_id).is_public() - && (ty_is_pure_pub || may_construct_self) + if of_trait && matches!(tcx.def_kind(local_def_id), DefKind::AssocTy) + || tcx.visibility(local_def_id).is_public() + && (ty_is_pure_pub || may_construct_self) { // if the impl item is public, // and the ty may be constructed or can be constructed in foreign crates, @@ -882,7 +873,7 @@ fn create_and_seed_worklist( // checks impls, impl-items and pub structs with all public fields later match tcx.def_kind(id) { DefKind::Impl { .. } => false, - DefKind::AssocFn => !matches!(tcx.associated_item(id).container, AssocItemContainer::ImplContainer), + DefKind::AssocConst | DefKind::AssocFn => !matches!(tcx.associated_item(id).container, AssocItemContainer::ImplContainer), DefKind::Struct => struct_all_fields_are_public(tcx, id.to_def_id()), _ => true }) @@ -1158,13 +1149,13 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) { || (def_kind == DefKind::Trait && live_symbols.contains(&item.owner_id.def_id)) { for &def_id in tcx.associated_item_def_ids(item.owner_id.def_id) { - // We have diagnosed unused methods in traits + // We have diagnosed unused assoc consts and fns in traits if matches!(def_kind, DefKind::Impl { of_trait: true }) - && tcx.def_kind(def_id) == DefKind::AssocFn + && matches!(tcx.def_kind(def_id), DefKind::AssocConst | DefKind::AssocFn) // skip unused public inherent methods, // cause we have diagnosed unconstructed struct || matches!(def_kind, DefKind::Impl { of_trait: false }) && tcx.visibility(def_id).is_public() && ty_ref_to_pub_struct(tcx, tcx.hir().item(item).expect_impl().self_ty) - || def_kind == DefKind::Trait && tcx.def_kind(def_id) != DefKind::AssocFn + || def_kind == DefKind::Trait && tcx.def_kind(def_id) == DefKind::AssocTy { continue; } diff --git a/tests/ui/lint/dead-code/unused_assoc_const.rs b/tests/ui/lint/dead-code/unused_assoc_const.rs new file mode 100644 index 000000000000..ab911a0d0acf --- /dev/null +++ b/tests/ui/lint/dead-code/unused_assoc_const.rs @@ -0,0 +1,17 @@ +#![deny(dead_code)] + +trait Trait { + const I: i32; //~ ERROR associated constant `I` is never used + + fn foo(&self) {} +} + +pub struct T(()); + +impl Trait for T { + const I: i32 = 0; +} + +fn main() { + T(()).foo(); +} diff --git a/tests/ui/lint/dead-code/unused_assoc_const.stderr b/tests/ui/lint/dead-code/unused_assoc_const.stderr new file mode 100644 index 000000000000..3b15e5f8c31c --- /dev/null +++ b/tests/ui/lint/dead-code/unused_assoc_const.stderr @@ -0,0 +1,16 @@ +error: associated constant `I` is never used + --> $DIR/unused_assoc_const.rs:4:11 + | +LL | trait Trait { + | ----- associated constant in this trait +LL | const I: i32; + | ^ + | +note: the lint level is defined here + --> $DIR/unused_assoc_const.rs:1:9 + | +LL | #![deny(dead_code)] + | ^^^^^^^^^ + +error: aborting due to 1 previous error +