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