diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index fc7755d3df8..5cfe691df17 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -700,6 +700,13 @@ fn warn_multiple_dead_codes( .collect(); let descr = tcx.def_descr(first_id.to_def_id()); + // `impl` blocks are "batched" and (unlike other batching) might + // contain different kinds of associated items. + let descr = if dead_codes.iter().any(|did| tcx.def_descr(did.to_def_id()) != descr) { + "associated item" + } else { + descr + }; let num = dead_codes.len(); let multiple = num > 6; let name_list = names.into(); @@ -842,16 +849,9 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) { if let hir::ItemKind::Impl(impl_item) = tcx.hir().item(item).kind { let mut dead_items = Vec::new(); for item in impl_item.items { - match item.kind { - hir::AssocItemKind::Const | hir::AssocItemKind::Type => { - visitor.check_definition(item.id.owner_id.def_id) - } - hir::AssocItemKind::Fn { .. } => { - let did = item.id.owner_id.def_id; - if !visitor.is_live_code(did) { - dead_items.push(did) - } - } + let did = item.id.owner_id.def_id; + if !visitor.is_live_code(did) { + dead_items.push(did) } } visitor.warn_multiple_dead_codes( @@ -860,7 +860,6 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) { Some(item.owner_id.def_id), false, ); - continue; } if !live_symbols.contains(&item.owner_id.def_id) { diff --git a/tests/ui/lint/dead-code/unused-assoc-fns.rs b/tests/ui/lint/dead-code/unused-assoc-fns.rs index 9e3ad85390a..b111f4b9463 100644 --- a/tests/ui/lint/dead-code/unused-assoc-fns.rs +++ b/tests/ui/lint/dead-code/unused-assoc-fns.rs @@ -1,19 +1,29 @@ +#![feature(inherent_associated_types)] +#![allow(incomplete_features)] #![deny(unused)] struct Foo; impl Foo { fn one() {} - //~^ ERROR associated functions `one`, `two`, and `three` are never used [dead_code] + //~^ ERROR associated items `one`, `two`, `CONSTANT`, `Type`, and `three` are never used [dead_code] fn two(&self) {} - // seperation between functions + // seperation between items // ... // ... fn used() {} + const CONSTANT: usize = 5; + + // more seperation + // ... + // ... + + type Type = usize; + fn three(&self) { Foo::one(); // ... diff --git a/tests/ui/lint/dead-code/unused-assoc-fns.stderr b/tests/ui/lint/dead-code/unused-assoc-fns.stderr index 71174a1c3de..6344a70ea3a 100644 --- a/tests/ui/lint/dead-code/unused-assoc-fns.stderr +++ b/tests/ui/lint/dead-code/unused-assoc-fns.stderr @@ -1,19 +1,25 @@ -error: associated functions `one`, `two`, and `three` are never used - --> $DIR/unused-assoc-fns.rs:6:8 +error: associated items `one`, `two`, `CONSTANT`, `Type`, and `three` are never used + --> $DIR/unused-assoc-fns.rs:8:8 | LL | impl Foo { - | -------- associated functions in this implementation + | -------- associated items in this implementation LL | fn one() {} | ^^^ ... LL | fn two(&self) {} | ^^^ ... +LL | const CONSTANT: usize = 5; + | ^^^^^^^^ +... +LL | type Type = usize; + | ^^^^ +LL | LL | fn three(&self) { | ^^^^^ | note: the lint level is defined here - --> $DIR/unused-assoc-fns.rs:1:9 + --> $DIR/unused-assoc-fns.rs:3:9 | LL | #![deny(unused)] | ^^^^^^