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