Skip to content

Commit 9bc0e5b

Browse files
committed
Detect unused associated constants in traits
1 parent 5a08af0 commit 9bc0e5b

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

compiler/rustc_passes/src/dead.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -786,18 +786,9 @@ fn check_item<'tcx>(
786786
// for trait impl blocks,
787787
// mark the method live if the self_ty is public,
788788
// or the method is public and may construct self
789-
if of_trait
790-
&& (!matches!(tcx.def_kind(local_def_id), DefKind::AssocFn)
791-
|| tcx.visibility(local_def_id).is_public()
792-
&& (ty_is_pure_pub || may_construct_self))
793-
{
794-
// if the impl item is public,
795-
// and the ty may be constructed or can be constructed in foreign crates,
796-
// mark the impl item live
797-
worklist.push((local_def_id, ComesFromAllowExpect::No));
798-
} else if !of_trait
799-
&& tcx.visibility(local_def_id).is_public()
800-
&& (ty_is_pure_pub || may_construct_self)
789+
if of_trait && matches!(tcx.def_kind(local_def_id), DefKind::AssocTy)
790+
|| tcx.visibility(local_def_id).is_public()
791+
&& (ty_is_pure_pub || may_construct_self)
801792
{
802793
// if the impl item is public,
803794
// and the ty may be constructed or can be constructed in foreign crates,
@@ -882,7 +873,7 @@ fn create_and_seed_worklist(
882873
// checks impls, impl-items and pub structs with all public fields later
883874
match tcx.def_kind(id) {
884875
DefKind::Impl { .. } => false,
885-
DefKind::AssocFn => !matches!(tcx.associated_item(id).container, AssocItemContainer::ImplContainer),
876+
DefKind::AssocConst | DefKind::AssocFn => !matches!(tcx.associated_item(id).container, AssocItemContainer::ImplContainer),
886877
DefKind::Struct => struct_all_fields_are_public(tcx, id.to_def_id()),
887878
_ => true
888879
})
@@ -1158,13 +1149,13 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {
11581149
|| (def_kind == DefKind::Trait && live_symbols.contains(&item.owner_id.def_id))
11591150
{
11601151
for &def_id in tcx.associated_item_def_ids(item.owner_id.def_id) {
1161-
// We have diagnosed unused methods in traits
1152+
// We have diagnosed unused assoc consts and fns in traits
11621153
if matches!(def_kind, DefKind::Impl { of_trait: true })
1163-
&& tcx.def_kind(def_id) == DefKind::AssocFn
1154+
&& matches!(tcx.def_kind(def_id), DefKind::AssocConst | DefKind::AssocFn)
11641155
// skip unused public inherent methods,
11651156
// cause we have diagnosed unconstructed struct
11661157
|| 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)
1167-
|| def_kind == DefKind::Trait && tcx.def_kind(def_id) != DefKind::AssocFn
1158+
|| def_kind == DefKind::Trait && tcx.def_kind(def_id) == DefKind::AssocTy
11681159
{
11691160
continue;
11701161
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![deny(dead_code)]
2+
3+
trait Trait {
4+
const I: i32; //~ ERROR associated constant `I` is never used
5+
6+
fn foo(&self) {}
7+
}
8+
9+
pub struct T(());
10+
11+
impl Trait for T {
12+
const I: i32 = 0;
13+
}
14+
15+
fn main() {
16+
T(()).foo();
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: associated constant `I` is never used
2+
--> $DIR/unused_assoc_const.rs:4:11
3+
|
4+
LL | trait Trait {
5+
| ----- associated constant in this trait
6+
LL | const I: i32;
7+
| ^
8+
|
9+
note: the lint level is defined here
10+
--> $DIR/unused_assoc_const.rs:1:9
11+
|
12+
LL | #![deny(dead_code)]
13+
| ^^^^^^^^^
14+
15+
error: aborting due to 1 previous error
16+

0 commit comments

Comments
 (0)