-impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
- type NestedFilter = nested_filter::All;
-
- /// Walk nested items in place so that we don't report dead-code
- /// on inner functions when the outer function is already getting
- /// an error. We could do this also by checking the parents, but
- /// this is how the code is setup and it seems harmless enough.
- fn nested_visit_map(&mut self) -> Self::Map {
- self.tcx.hir()
- }
-
- fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
- if self.should_warn_about_item(item) {
- // For most items, we want to highlight its identifier
- let span = match item.kind {
- hir::ItemKind::Fn(..)
- | hir::ItemKind::Mod(..)
- | hir::ItemKind::Enum(..)
- | hir::ItemKind::Struct(..)
- | hir::ItemKind::Union(..)
- | hir::ItemKind::Trait(..)
- | hir::ItemKind::Impl { .. } => {
- // FIXME(66095): Because item.span is annotated with things
- // like expansion data, and ident.span isn't, we use the
- // def_span method if it's part of a macro invocation
- // (and thus has a source_callee set).
- // We should probably annotate ident.span with the macro
- // context, but that's a larger change.
- if item.span.source_callee().is_some() {
- self.tcx.sess.source_map().guess_head_span(item.span)
- } else {
- item.ident.span
- }
- }
- _ => item.span,
- };
- let participle = match item.kind {
- hir::ItemKind::Struct(..) => "constructed", // Issue #52325
- _ => "used",
- };
- self.warn_dead_code(item.hir_id(), span, item.ident.name, participle);
- } else {
- // Only continue if we didn't warn
- intravisit::walk_item(self, item);
+ fn check_definition(&mut self, def_id: LocalDefId) {
+ if self.live_symbols.contains(&def_id) {
+ return;
+ }
+ let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
+ if has_allow_dead_code_or_lang_attr(self.tcx, hir_id) {
+ return;
+ }
+ let Some(name) = self.tcx.opt_item_name(def_id.to_def_id()) else {
+ return
+ };
+ if name.as_str().starts_with('_') {
+ return;
+ }
+ match self.tcx.def_kind(def_id) {
+ DefKind::AssocConst
+ | DefKind::AssocFn
+ | DefKind::Fn
+ | DefKind::Static(_)
+ | DefKind::Const
+ | DefKind::TyAlias
+ | DefKind::Enum
+ | DefKind::Union
+ | DefKind::ForeignTy => self.warn_dead_code(def_id, "used"),
+ DefKind::Struct => self.warn_dead_code(def_id, "constructed"),
+ DefKind::Variant | DefKind::Field => bug!("should be handled specially"),
+ _ => {}