]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_passes/src/dead.rs
Clean up surprising borrow
[rust.git] / compiler / rustc_passes / src / dead.rs
1 // This implements the dead-code warning pass. It follows middle::reachable
2 // closely. The idea is that all reachable symbols are live, codes called
3 // from live codes are live, and everything else is dead.
4
5 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
6 use rustc_hir as hir;
7 use rustc_hir::def::{CtorOf, DefKind, Res};
8 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
9 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
10 use rustc_hir::itemlikevisit::ItemLikeVisitor;
11 use rustc_hir::{Node, PatKind, TyKind};
12 use rustc_middle::hir::map::Map;
13 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
14 use rustc_middle::middle::privacy;
15 use rustc_middle::ty::{self, DefIdTree, TyCtxt};
16 use rustc_session::lint;
17
18 use rustc_ast as ast;
19 use rustc_span::symbol::{sym, Symbol};
20
21 // Any local node that may call something in its body block should be
22 // explored. For example, if it's a live Node::Item that is a
23 // function, then we should explore its block to check for codes that
24 // may need to be marked as live.
25 fn should_explore(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
26     match tcx.hir().find(hir_id) {
27         Some(
28             Node::Item(..)
29             | Node::ImplItem(..)
30             | Node::ForeignItem(..)
31             | Node::TraitItem(..)
32             | Node::Variant(..)
33             | Node::AnonConst(..)
34             | Node::Pat(..),
35         ) => true,
36         _ => false,
37     }
38 }
39
40 struct MarkSymbolVisitor<'tcx> {
41     worklist: Vec<hir::HirId>,
42     tcx: TyCtxt<'tcx>,
43     maybe_typeck_results: Option<&'tcx ty::TypeckResults<'tcx>>,
44     live_symbols: FxHashSet<hir::HirId>,
45     repr_has_repr_c: bool,
46     in_pat: bool,
47     inherited_pub_visibility: bool,
48     ignore_variant_stack: Vec<DefId>,
49     // maps from tuple struct constructors to tuple struct items
50     struct_constructors: FxHashMap<hir::HirId, hir::HirId>,
51 }
52
53 impl<'tcx> MarkSymbolVisitor<'tcx> {
54     /// Gets the type-checking results for the current body.
55     /// As this will ICE if called outside bodies, only call when working with
56     /// `Expr` or `Pat` nodes (they are guaranteed to be found only in bodies).
57     #[track_caller]
58     fn typeck_results(&self) -> &'tcx ty::TypeckResults<'tcx> {
59         self.maybe_typeck_results
60             .expect("`MarkSymbolVisitor::typeck_results` called outside of body")
61     }
62
63     fn check_def_id(&mut self, def_id: DefId) {
64         if let Some(def_id) = def_id.as_local() {
65             let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
66             if should_explore(self.tcx, hir_id) || self.struct_constructors.contains_key(&hir_id) {
67                 self.worklist.push(hir_id);
68             }
69             self.live_symbols.insert(hir_id);
70         }
71     }
72
73     fn insert_def_id(&mut self, def_id: DefId) {
74         if let Some(def_id) = def_id.as_local() {
75             let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
76             debug_assert!(!should_explore(self.tcx, hir_id));
77             self.live_symbols.insert(hir_id);
78         }
79     }
80
81     fn handle_res(&mut self, res: Res) {
82         match res {
83             Res::Def(DefKind::Const | DefKind::AssocConst | DefKind::TyAlias, _) => {
84                 self.check_def_id(res.def_id());
85             }
86             _ if self.in_pat => {}
87             Res::PrimTy(..) | Res::SelfCtor(..) | Res::Local(..) => {}
88             Res::Def(DefKind::Ctor(CtorOf::Variant, ..), ctor_def_id) => {
89                 let variant_id = self.tcx.parent(ctor_def_id).unwrap();
90                 let enum_id = self.tcx.parent(variant_id).unwrap();
91                 self.check_def_id(enum_id);
92                 if !self.ignore_variant_stack.contains(&ctor_def_id) {
93                     self.check_def_id(variant_id);
94                 }
95             }
96             Res::Def(DefKind::Variant, variant_id) => {
97                 let enum_id = self.tcx.parent(variant_id).unwrap();
98                 self.check_def_id(enum_id);
99                 if !self.ignore_variant_stack.contains(&variant_id) {
100                     self.check_def_id(variant_id);
101                 }
102             }
103             Res::SelfTy(t, i) => {
104                 if let Some(t) = t {
105                     self.check_def_id(t);
106                 }
107                 if let Some((i, _)) = i {
108                     self.check_def_id(i);
109                 }
110             }
111             Res::ToolMod | Res::NonMacroAttr(..) | Res::Err => {}
112             _ => {
113                 self.check_def_id(res.def_id());
114             }
115         }
116     }
117
118     fn lookup_and_handle_method(&mut self, id: hir::HirId) {
119         if let Some(def_id) = self.typeck_results().type_dependent_def_id(id) {
120             self.check_def_id(def_id);
121         } else {
122             bug!("no type-dependent def for method");
123         }
124     }
125
126     fn handle_field_access(&mut self, lhs: &hir::Expr<'_>, hir_id: hir::HirId) {
127         match self.typeck_results().expr_ty_adjusted(lhs).kind() {
128             ty::Adt(def, _) => {
129                 let index = self.tcx.field_index(hir_id, self.typeck_results());
130                 self.insert_def_id(def.non_enum_variant().fields[index].did);
131             }
132             ty::Tuple(..) => {}
133             _ => span_bug!(lhs.span, "named field access on non-ADT"),
134         }
135     }
136
137     fn handle_field_pattern_match(
138         &mut self,
139         lhs: &hir::Pat<'_>,
140         res: Res,
141         pats: &[hir::FieldPat<'_>],
142     ) {
143         let variant = match self.typeck_results().node_type(lhs.hir_id).kind() {
144             ty::Adt(adt, _) => adt.variant_of_res(res),
145             _ => span_bug!(lhs.span, "non-ADT in struct pattern"),
146         };
147         for pat in pats {
148             if let PatKind::Wild = pat.pat.kind {
149                 continue;
150             }
151             let index = self.tcx.field_index(pat.hir_id, self.typeck_results());
152             self.insert_def_id(variant.fields[index].did);
153         }
154     }
155
156     fn mark_live_symbols(&mut self) {
157         let mut scanned = FxHashSet::default();
158         while let Some(id) = self.worklist.pop() {
159             if !scanned.insert(id) {
160                 continue;
161             }
162
163             // in the case of tuple struct constructors we want to check the item, not the generated
164             // tuple struct constructor function
165             let id = self.struct_constructors.get(&id).cloned().unwrap_or(id);
166
167             if let Some(node) = self.tcx.hir().find(id) {
168                 self.live_symbols.insert(id);
169                 self.visit_node(node);
170             }
171         }
172     }
173
174     fn visit_node(&mut self, node: Node<'tcx>) {
175         let had_repr_c = self.repr_has_repr_c;
176         self.repr_has_repr_c = false;
177         let had_inherited_pub_visibility = self.inherited_pub_visibility;
178         self.inherited_pub_visibility = false;
179         match node {
180             Node::Item(item) => match item.kind {
181                 hir::ItemKind::Struct(..) | hir::ItemKind::Union(..) => {
182                     let def_id = self.tcx.hir().local_def_id(item.hir_id);
183                     let def = self.tcx.adt_def(def_id);
184                     self.repr_has_repr_c = def.repr.c();
185
186                     intravisit::walk_item(self, &item);
187                 }
188                 hir::ItemKind::Enum(..) => {
189                     self.inherited_pub_visibility = item.vis.node.is_pub();
190
191                     intravisit::walk_item(self, &item);
192                 }
193                 hir::ItemKind::ForeignMod(..) => {}
194                 _ => {
195                     intravisit::walk_item(self, &item);
196                 }
197             },
198             Node::TraitItem(trait_item) => {
199                 intravisit::walk_trait_item(self, trait_item);
200             }
201             Node::ImplItem(impl_item) => {
202                 intravisit::walk_impl_item(self, impl_item);
203             }
204             Node::ForeignItem(foreign_item) => {
205                 intravisit::walk_foreign_item(self, &foreign_item);
206             }
207             _ => {}
208         }
209         self.repr_has_repr_c = had_repr_c;
210         self.inherited_pub_visibility = had_inherited_pub_visibility;
211     }
212
213     fn mark_as_used_if_union(&mut self, adt: &ty::AdtDef, fields: &[hir::Field<'_>]) {
214         if adt.is_union() && adt.non_enum_variant().fields.len() > 1 && adt.did.is_local() {
215             for field in fields {
216                 let index = self.tcx.field_index(field.hir_id, self.typeck_results());
217                 self.insert_def_id(adt.non_enum_variant().fields[index].did);
218             }
219         }
220     }
221 }
222
223 impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
224     type Map = intravisit::ErasedMap<'tcx>;
225
226     fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
227         NestedVisitorMap::None
228     }
229
230     fn visit_nested_body(&mut self, body: hir::BodyId) {
231         let old_maybe_typeck_results =
232             self.maybe_typeck_results.replace(self.tcx.typeck_body(body));
233         let body = self.tcx.hir().body(body);
234         self.visit_body(body);
235         self.maybe_typeck_results = old_maybe_typeck_results;
236     }
237
238     fn visit_variant_data(
239         &mut self,
240         def: &'tcx hir::VariantData<'tcx>,
241         _: Symbol,
242         _: &hir::Generics<'_>,
243         _: hir::HirId,
244         _: rustc_span::Span,
245     ) {
246         let has_repr_c = self.repr_has_repr_c;
247         let inherited_pub_visibility = self.inherited_pub_visibility;
248         let live_fields = def
249             .fields()
250             .iter()
251             .filter(|f| has_repr_c || inherited_pub_visibility || f.vis.node.is_pub());
252         self.live_symbols.extend(live_fields.map(|f| f.hir_id));
253
254         intravisit::walk_struct_def(self, def);
255     }
256
257     fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
258         match expr.kind {
259             hir::ExprKind::Path(ref qpath @ hir::QPath::TypeRelative(..)) => {
260                 let res = self.typeck_results().qpath_res(qpath, expr.hir_id);
261                 self.handle_res(res);
262             }
263             hir::ExprKind::MethodCall(..) => {
264                 self.lookup_and_handle_method(expr.hir_id);
265             }
266             hir::ExprKind::Field(ref lhs, ..) => {
267                 self.handle_field_access(&lhs, expr.hir_id);
268             }
269             hir::ExprKind::Struct(ref qpath, ref fields, _) => {
270                 let res = self.typeck_results().qpath_res(qpath, expr.hir_id);
271                 self.handle_res(res);
272                 if let ty::Adt(ref adt, _) = self.typeck_results().expr_ty(expr).kind() {
273                     self.mark_as_used_if_union(adt, fields);
274                 }
275             }
276             _ => (),
277         }
278
279         intravisit::walk_expr(self, expr);
280     }
281
282     fn visit_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) {
283         // Inside the body, ignore constructions of variants
284         // necessary for the pattern to match. Those construction sites
285         // can't be reached unless the variant is constructed elsewhere.
286         let len = self.ignore_variant_stack.len();
287         self.ignore_variant_stack.extend(arm.pat.necessary_variants());
288         intravisit::walk_arm(self, arm);
289         self.ignore_variant_stack.truncate(len);
290     }
291
292     fn visit_pat(&mut self, pat: &'tcx hir::Pat<'tcx>) {
293         match pat.kind {
294             PatKind::Struct(ref path, ref fields, _) => {
295                 let res = self.typeck_results().qpath_res(path, pat.hir_id);
296                 self.handle_field_pattern_match(pat, res, fields);
297             }
298             PatKind::Path(ref qpath) => {
299                 let res = self.typeck_results().qpath_res(qpath, pat.hir_id);
300                 self.handle_res(res);
301             }
302             _ => (),
303         }
304
305         self.in_pat = true;
306         intravisit::walk_pat(self, pat);
307         self.in_pat = false;
308     }
309
310     fn visit_path(&mut self, path: &'tcx hir::Path<'tcx>, _: hir::HirId) {
311         self.handle_res(path.res);
312         intravisit::walk_path(self, path);
313     }
314
315     fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx>) {
316         if let TyKind::OpaqueDef(item_id, _) = ty.kind {
317             let item = self.tcx.hir().expect_item(item_id.id);
318             intravisit::walk_item(self, item);
319         }
320         intravisit::walk_ty(self, ty);
321     }
322
323     fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) {
324         self.live_symbols.insert(c.hir_id);
325         intravisit::walk_anon_const(self, c);
326     }
327 }
328
329 fn has_allow_dead_code_or_lang_attr(
330     tcx: TyCtxt<'_>,
331     id: hir::HirId,
332     attrs: &[ast::Attribute],
333 ) -> bool {
334     if tcx.sess.contains_name(attrs, sym::lang) {
335         return true;
336     }
337
338     // Stable attribute for #[lang = "panic_impl"]
339     if tcx.sess.contains_name(attrs, sym::panic_handler) {
340         return true;
341     }
342
343     // (To be) stable attribute for #[lang = "oom"]
344     if tcx.sess.contains_name(attrs, sym::alloc_error_handler) {
345         return true;
346     }
347
348     let def_id = tcx.hir().local_def_id(id);
349     let cg_attrs = tcx.codegen_fn_attrs(def_id);
350
351     // #[used], #[no_mangle], #[export_name], etc also keeps the item alive
352     // forcefully, e.g., for placing it in a specific section.
353     if cg_attrs.contains_extern_indicator() || cg_attrs.flags.contains(CodegenFnAttrFlags::USED) {
354         return true;
355     }
356
357     tcx.lint_level_at_node(lint::builtin::DEAD_CODE, id).0 == lint::Allow
358 }
359
360 // This visitor seeds items that
361 //   1) We want to explicitly consider as live:
362 //     * Item annotated with #[allow(dead_code)]
363 //         - This is done so that if we want to suppress warnings for a
364 //           group of dead functions, we only have to annotate the "root".
365 //           For example, if both `f` and `g` are dead and `f` calls `g`,
366 //           then annotating `f` with `#[allow(dead_code)]` will suppress
367 //           warning for both `f` and `g`.
368 //     * Item annotated with #[lang=".."]
369 //         - This is because lang items are always callable from elsewhere.
370 //   or
371 //   2) We are not sure to be live or not
372 //     * Implementations of traits and trait methods
373 struct LifeSeeder<'k, 'tcx> {
374     worklist: Vec<hir::HirId>,
375     krate: &'k hir::Crate<'k>,
376     tcx: TyCtxt<'tcx>,
377     // see `MarkSymbolVisitor::struct_constructors`
378     struct_constructors: FxHashMap<hir::HirId, hir::HirId>,
379 }
380
381 impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> {
382     fn visit_item(&mut self, item: &hir::Item<'_>) {
383         let allow_dead_code = has_allow_dead_code_or_lang_attr(self.tcx, item.hir_id, &item.attrs);
384         if allow_dead_code {
385             self.worklist.push(item.hir_id);
386         }
387         match item.kind {
388             hir::ItemKind::Enum(ref enum_def, _) => {
389                 if allow_dead_code {
390                     self.worklist.extend(enum_def.variants.iter().map(|variant| variant.id));
391                 }
392
393                 for variant in enum_def.variants {
394                     if let Some(ctor_hir_id) = variant.data.ctor_hir_id() {
395                         self.struct_constructors.insert(ctor_hir_id, variant.id);
396                     }
397                 }
398             }
399             hir::ItemKind::Trait(.., trait_item_refs) => {
400                 for trait_item_ref in trait_item_refs {
401                     let trait_item = self.krate.trait_item(trait_item_ref.id);
402                     match trait_item.kind {
403                         hir::TraitItemKind::Const(_, Some(_))
404                         | hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => {
405                             if has_allow_dead_code_or_lang_attr(
406                                 self.tcx,
407                                 trait_item.hir_id,
408                                 &trait_item.attrs,
409                             ) {
410                                 self.worklist.push(trait_item.hir_id);
411                             }
412                         }
413                         _ => {}
414                     }
415                 }
416             }
417             hir::ItemKind::Impl { ref of_trait, items, .. } => {
418                 if of_trait.is_some() {
419                     self.worklist.push(item.hir_id);
420                 }
421                 for impl_item_ref in items {
422                     let impl_item = self.krate.impl_item(impl_item_ref.id);
423                     if of_trait.is_some()
424                         || has_allow_dead_code_or_lang_attr(
425                             self.tcx,
426                             impl_item.hir_id,
427                             &impl_item.attrs,
428                         )
429                     {
430                         self.worklist.push(impl_item_ref.id.hir_id);
431                     }
432                 }
433             }
434             hir::ItemKind::Struct(ref variant_data, _) => {
435                 if let Some(ctor_hir_id) = variant_data.ctor_hir_id() {
436                     self.struct_constructors.insert(ctor_hir_id, item.hir_id);
437                 }
438             }
439             _ => (),
440         }
441     }
442
443     fn visit_trait_item(&mut self, _item: &hir::TraitItem<'_>) {
444         // ignore: we are handling this in `visit_item` above
445     }
446
447     fn visit_impl_item(&mut self, _item: &hir::ImplItem<'_>) {
448         // ignore: we are handling this in `visit_item` above
449     }
450 }
451
452 fn create_and_seed_worklist<'tcx>(
453     tcx: TyCtxt<'tcx>,
454     access_levels: &privacy::AccessLevels,
455     krate: &hir::Crate<'_>,
456 ) -> (Vec<hir::HirId>, FxHashMap<hir::HirId, hir::HirId>) {
457     let worklist = access_levels
458         .map
459         .iter()
460         .filter_map(
461             |(&id, &level)| {
462                 if level >= privacy::AccessLevel::Reachable { Some(id) } else { None }
463             },
464         )
465         .chain(
466             // Seed entry point
467             tcx.entry_fn(LOCAL_CRATE).map(|(def_id, _)| tcx.hir().local_def_id_to_hir_id(def_id)),
468         )
469         .collect::<Vec<_>>();
470
471     // Seed implemented trait items
472     let mut life_seeder =
473         LifeSeeder { worklist, krate, tcx, struct_constructors: Default::default() };
474     krate.visit_all_item_likes(&mut life_seeder);
475
476     (life_seeder.worklist, life_seeder.struct_constructors)
477 }
478
479 fn find_live<'tcx>(
480     tcx: TyCtxt<'tcx>,
481     access_levels: &privacy::AccessLevels,
482     krate: &hir::Crate<'_>,
483 ) -> FxHashSet<hir::HirId> {
484     let (worklist, struct_constructors) = create_and_seed_worklist(tcx, access_levels, krate);
485     let mut symbol_visitor = MarkSymbolVisitor {
486         worklist,
487         tcx,
488         maybe_typeck_results: None,
489         live_symbols: Default::default(),
490         repr_has_repr_c: false,
491         in_pat: false,
492         inherited_pub_visibility: false,
493         ignore_variant_stack: vec![],
494         struct_constructors,
495     };
496     symbol_visitor.mark_live_symbols();
497     symbol_visitor.live_symbols
498 }
499
500 struct DeadVisitor<'tcx> {
501     tcx: TyCtxt<'tcx>,
502     live_symbols: FxHashSet<hir::HirId>,
503 }
504
505 impl DeadVisitor<'tcx> {
506     fn should_warn_about_item(&mut self, item: &hir::Item<'_>) -> bool {
507         let should_warn = match item.kind {
508             hir::ItemKind::Static(..)
509             | hir::ItemKind::Const(..)
510             | hir::ItemKind::Fn(..)
511             | hir::ItemKind::TyAlias(..)
512             | hir::ItemKind::Enum(..)
513             | hir::ItemKind::Struct(..)
514             | hir::ItemKind::Union(..) => true,
515             _ => false,
516         };
517         should_warn && !self.symbol_is_live(item.hir_id)
518     }
519
520     fn should_warn_about_field(&mut self, field: &hir::StructField<'_>) -> bool {
521         let field_type = self.tcx.type_of(self.tcx.hir().local_def_id(field.hir_id));
522         !field.is_positional()
523             && !self.symbol_is_live(field.hir_id)
524             && !field_type.is_phantom_data()
525             && !has_allow_dead_code_or_lang_attr(self.tcx, field.hir_id, &field.attrs)
526     }
527
528     fn should_warn_about_variant(&mut self, variant: &hir::Variant<'_>) -> bool {
529         !self.symbol_is_live(variant.id)
530             && !has_allow_dead_code_or_lang_attr(self.tcx, variant.id, &variant.attrs)
531     }
532
533     fn should_warn_about_foreign_item(&mut self, fi: &hir::ForeignItem<'_>) -> bool {
534         !self.symbol_is_live(fi.hir_id)
535             && !has_allow_dead_code_or_lang_attr(self.tcx, fi.hir_id, &fi.attrs)
536     }
537
538     // id := HIR id of an item's definition.
539     fn symbol_is_live(&mut self, id: hir::HirId) -> bool {
540         if self.live_symbols.contains(&id) {
541             return true;
542         }
543         // If it's a type whose items are live, then it's live, too.
544         // This is done to handle the case where, for example, the static
545         // method of a private type is used, but the type itself is never
546         // called directly.
547         let def_id = self.tcx.hir().local_def_id(id);
548         let inherent_impls = self.tcx.inherent_impls(def_id);
549         for &impl_did in inherent_impls.iter() {
550             for &item_did in &self.tcx.associated_item_def_ids(impl_did)[..] {
551                 if let Some(did) = item_did.as_local() {
552                     let item_hir_id = self.tcx.hir().local_def_id_to_hir_id(did);
553                     if self.live_symbols.contains(&item_hir_id) {
554                         return true;
555                     }
556                 }
557             }
558         }
559         false
560     }
561
562     fn warn_dead_code(
563         &mut self,
564         id: hir::HirId,
565         span: rustc_span::Span,
566         name: Symbol,
567         participle: &str,
568     ) {
569         if !name.as_str().starts_with('_') {
570             self.tcx.struct_span_lint_hir(lint::builtin::DEAD_CODE, id, span, |lint| {
571                 let def_id = self.tcx.hir().local_def_id(id);
572                 let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
573                 lint.build(&format!("{} is never {}: `{}`", descr, participle, name)).emit()
574             });
575         }
576     }
577 }
578
579 impl Visitor<'tcx> for DeadVisitor<'tcx> {
580     type Map = Map<'tcx>;
581
582     /// Walk nested items in place so that we don't report dead-code
583     /// on inner functions when the outer function is already getting
584     /// an error. We could do this also by checking the parents, but
585     /// this is how the code is setup and it seems harmless enough.
586     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
587         NestedVisitorMap::All(self.tcx.hir())
588     }
589
590     fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
591         if self.should_warn_about_item(item) {
592             // For most items, we want to highlight its identifier
593             let span = match item.kind {
594                 hir::ItemKind::Fn(..)
595                 | hir::ItemKind::Mod(..)
596                 | hir::ItemKind::Enum(..)
597                 | hir::ItemKind::Struct(..)
598                 | hir::ItemKind::Union(..)
599                 | hir::ItemKind::Trait(..)
600                 | hir::ItemKind::Impl { .. } => {
601                     // FIXME(66095): Because item.span is annotated with things
602                     // like expansion data, and ident.span isn't, we use the
603                     // def_span method if it's part of a macro invocation
604                     // (and thus has a source_callee set).
605                     // We should probably annotate ident.span with the macro
606                     // context, but that's a larger change.
607                     if item.span.source_callee().is_some() {
608                         self.tcx.sess.source_map().guess_head_span(item.span)
609                     } else {
610                         item.ident.span
611                     }
612                 }
613                 _ => item.span,
614             };
615             let participle = match item.kind {
616                 hir::ItemKind::Struct(..) => "constructed", // Issue #52325
617                 _ => "used",
618             };
619             self.warn_dead_code(item.hir_id, span, item.ident.name, participle);
620         } else {
621             // Only continue if we didn't warn
622             intravisit::walk_item(self, item);
623         }
624     }
625
626     fn visit_variant(
627         &mut self,
628         variant: &'tcx hir::Variant<'tcx>,
629         g: &'tcx hir::Generics<'tcx>,
630         id: hir::HirId,
631     ) {
632         if self.should_warn_about_variant(&variant) {
633             self.warn_dead_code(variant.id, variant.span, variant.ident.name, "constructed");
634         } else {
635             intravisit::walk_variant(self, variant, g, id);
636         }
637     }
638
639     fn visit_foreign_item(&mut self, fi: &'tcx hir::ForeignItem<'tcx>) {
640         if self.should_warn_about_foreign_item(fi) {
641             self.warn_dead_code(fi.hir_id, fi.span, fi.ident.name, "used");
642         }
643         intravisit::walk_foreign_item(self, fi);
644     }
645
646     fn visit_struct_field(&mut self, field: &'tcx hir::StructField<'tcx>) {
647         if self.should_warn_about_field(&field) {
648             self.warn_dead_code(field.hir_id, field.span, field.ident.name, "read");
649         }
650         intravisit::walk_struct_field(self, field);
651     }
652
653     fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
654         match impl_item.kind {
655             hir::ImplItemKind::Const(_, body_id) => {
656                 if !self.symbol_is_live(impl_item.hir_id) {
657                     self.warn_dead_code(
658                         impl_item.hir_id,
659                         impl_item.span,
660                         impl_item.ident.name,
661                         "used",
662                     );
663                 }
664                 self.visit_nested_body(body_id)
665             }
666             hir::ImplItemKind::Fn(_, body_id) => {
667                 if !self.symbol_is_live(impl_item.hir_id) {
668                     // FIXME(66095): Because impl_item.span is annotated with things
669                     // like expansion data, and ident.span isn't, we use the
670                     // def_span method if it's part of a macro invocation
671                     // (and thus has a source_callee set).
672                     // We should probably annotate ident.span with the macro
673                     // context, but that's a larger change.
674                     let span = if impl_item.span.source_callee().is_some() {
675                         self.tcx.sess.source_map().guess_head_span(impl_item.span)
676                     } else {
677                         impl_item.ident.span
678                     };
679                     self.warn_dead_code(impl_item.hir_id, span, impl_item.ident.name, "used");
680                 }
681                 self.visit_nested_body(body_id)
682             }
683             hir::ImplItemKind::TyAlias(..) => {}
684         }
685     }
686
687     // Overwrite so that we don't warn the trait item itself.
688     fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
689         match trait_item.kind {
690             hir::TraitItemKind::Const(_, Some(body_id))
691             | hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(body_id)) => {
692                 self.visit_nested_body(body_id)
693             }
694             hir::TraitItemKind::Const(_, None)
695             | hir::TraitItemKind::Fn(_, hir::TraitFn::Required(_))
696             | hir::TraitItemKind::Type(..) => {}
697         }
698     }
699 }
700
701 pub fn check_crate(tcx: TyCtxt<'_>) {
702     let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE);
703     let krate = tcx.hir().krate();
704     let live_symbols = find_live(tcx, access_levels, krate);
705     let mut visitor = DeadVisitor { tcx, live_symbols };
706     intravisit::walk_crate(&mut visitor, krate);
707 }