about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorEzra Shaw <ezrasure@outlook.com>2023-04-13 22:42:47 +1200
committerEzra Shaw <ezrasure@outlook.com>2023-04-13 22:42:47 +1200
commitc41dcac8e869160184fc2d80f643bc74601a45ef (patch)
tree72960db856ef92ccc695c49fb3fa1b86786e8372 /compiler
parent03cf0e949fc1537f4a626eb0a925f23cb9010cb3 (diff)
downloadrust-c41dcac8e869160184fc2d80f643bc74601a45ef.tar.gz
rust-c41dcac8e869160184fc2d80f643bc74601a45ef.zip
dead-code-lint: de-dup multiple unused assoc fns
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_passes/src/dead.rs54
1 files changed, 37 insertions, 17 deletions
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;
         }