From c41dcac8e869160184fc2d80f643bc74601a45ef Mon Sep 17 00:00:00 2001 From: Ezra Shaw Date: Thu, 13 Apr 2023 22:42:47 +1200 Subject: dead-code-lint: de-dup multiple unused assoc fns --- compiler/rustc_passes/src/dead.rs | 54 +++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 17 deletions(-) (limited to 'compiler/rustc_passes/src') diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index d08dc4055ec..fc7755d3df8 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -712,12 +712,12 @@ impl<'tcx> DeadVisitor<'tcx> { let parent_info = if let Some(parent_item) = parent_item { let parent_descr = tcx.def_descr(parent_item.to_def_id()); - Some(ParentInfo { - num, - descr, - parent_descr, - span: tcx.def_ident_span(parent_item).unwrap(), - }) + let span = if let DefKind::Impl { .. } = tcx.def_kind(parent_item) { + tcx.def_span(parent_item) + } else { + tcx.def_ident_span(parent_item).unwrap() + }; + Some(ParentInfo { num, descr, parent_descr, span }) } else { None }; @@ -800,16 +800,7 @@ impl<'tcx> DeadVisitor<'tcx> { } fn check_definition(&mut self, def_id: LocalDefId) { - if self.live_symbols.contains(&def_id) { - return; - } - if has_allow_dead_code_or_lang_attr(self.tcx, def_id) { - return; - } - let Some(name) = self.tcx.opt_item_name(def_id.to_def_id()) else { - return - }; - if name.as_str().starts_with('_') { + if self.is_live_code(def_id) { return; } match self.tcx.def_kind(def_id) { @@ -827,6 +818,18 @@ impl<'tcx> DeadVisitor<'tcx> { _ => {} } } + + fn is_live_code(&self, def_id: LocalDefId) -> bool { + // if we cannot get a name for the item, then we just assume that it is + // live. I mean, we can't really emit a lint. + let Some(name) = self.tcx.opt_item_name(def_id.to_def_id()) else { + return true; + }; + + self.live_symbols.contains(&def_id) + || has_allow_dead_code_or_lang_attr(self.tcx, def_id) + || name.as_str().starts_with('_') + } } fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) { @@ -837,9 +840,26 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) { for item in module_items.items() { if let hir::ItemKind::Impl(impl_item) = tcx.hir().item(item).kind { + let mut dead_items = Vec::new(); for item in impl_item.items { - visitor.check_definition(item.id.owner_id.def_id); + 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) + } + } + } } + visitor.warn_multiple_dead_codes( + &dead_items, + "used", + Some(item.owner_id.def_id), + false, + ); continue; } -- cgit 1.4.1-3-g733a5