]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/dead.rs
Rollup merge of #55758 - davidtwco:issue-55344, r=pnkfelix
[rust.git] / src / librustc / middle / dead.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // This implements the dead-code warning pass. It follows middle::reachable
12 // closely. The idea is that all reachable symbols are live, codes called
13 // from live codes are live, and everything else is dead.
14
15 use hir::Node;
16 use hir::{self, PatKind};
17 use hir::intravisit::{self, Visitor, NestedVisitorMap};
18 use hir::itemlikevisit::ItemLikeVisitor;
19
20 use hir::def::Def;
21 use hir::CodegenFnAttrFlags;
22 use hir::def_id::{DefId, LOCAL_CRATE};
23 use lint;
24 use middle::privacy;
25 use ty::{self, TyCtxt};
26 use util::nodemap::FxHashSet;
27
28 use syntax::{ast, source_map};
29 use syntax::attr;
30 use syntax_pos;
31
32 // Any local node that may call something in its body block should be
33 // explored. For example, if it's a live Node::Item that is a
34 // function, then we should explore its block to check for codes that
35 // may need to be marked as live.
36 fn should_explore<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
37                             node_id: ast::NodeId) -> bool {
38     match tcx.hir.find(node_id) {
39         Some(Node::Item(..)) |
40         Some(Node::ImplItem(..)) |
41         Some(Node::ForeignItem(..)) |
42         Some(Node::TraitItem(..)) =>
43             true,
44         _ =>
45             false
46     }
47 }
48
49 struct MarkSymbolVisitor<'a, 'tcx: 'a> {
50     worklist: Vec<ast::NodeId>,
51     tcx: TyCtxt<'a, 'tcx, 'tcx>,
52     tables: &'a ty::TypeckTables<'tcx>,
53     live_symbols: FxHashSet<ast::NodeId>,
54     repr_has_repr_c: bool,
55     in_pat: bool,
56     inherited_pub_visibility: bool,
57     ignore_variant_stack: Vec<DefId>,
58 }
59
60 impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
61     fn check_def_id(&mut self, def_id: DefId) {
62         if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
63             if should_explore(self.tcx, node_id) {
64                 self.worklist.push(node_id);
65             }
66             self.live_symbols.insert(node_id);
67         }
68     }
69
70     fn insert_def_id(&mut self, def_id: DefId) {
71         if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
72             debug_assert!(!should_explore(self.tcx, node_id));
73             self.live_symbols.insert(node_id);
74         }
75     }
76
77     fn handle_definition(&mut self, def: Def) {
78         match def {
79             Def::Const(_) | Def::AssociatedConst(..) | Def::TyAlias(_) => {
80                 self.check_def_id(def.def_id());
81             }
82             _ if self.in_pat => (),
83             Def::PrimTy(..) | Def::SelfTy(..) |
84             Def::Local(..) | Def::Upvar(..) => {}
85             Def::Variant(variant_id) | Def::VariantCtor(variant_id, ..) => {
86                 if let Some(enum_id) = self.tcx.parent_def_id(variant_id) {
87                     self.check_def_id(enum_id);
88                 }
89                 if !self.ignore_variant_stack.contains(&variant_id) {
90                     self.check_def_id(variant_id);
91                 }
92             }
93             _ => {
94                 self.check_def_id(def.def_id());
95             }
96         }
97     }
98
99     fn lookup_and_handle_method(&mut self, id: hir::HirId) {
100         if let Some(def) = self.tables.type_dependent_defs().get(id) {
101             self.check_def_id(def.def_id());
102         } else {
103             bug!("no type-dependent def for method");
104         }
105     }
106
107     fn handle_field_access(&mut self, lhs: &hir::Expr, node_id: ast::NodeId) {
108         match self.tables.expr_ty_adjusted(lhs).sty {
109             ty::Adt(def, _) => {
110                 let index = self.tcx.field_index(node_id, self.tables);
111                 self.insert_def_id(def.non_enum_variant().fields[index].did);
112             }
113             ty::Tuple(..) => {}
114             _ => span_bug!(lhs.span, "named field access on non-ADT"),
115         }
116     }
117
118     fn handle_field_pattern_match(&mut self, lhs: &hir::Pat, def: Def,
119                                   pats: &[source_map::Spanned<hir::FieldPat>]) {
120         let variant = match self.tables.node_id_to_type(lhs.hir_id).sty {
121             ty::Adt(adt, _) => adt.variant_of_def(def),
122             _ => span_bug!(lhs.span, "non-ADT in struct pattern")
123         };
124         for pat in pats {
125             if let PatKind::Wild = pat.node.pat.node {
126                 continue;
127             }
128             let index = self.tcx.field_index(pat.node.id, self.tables);
129             self.insert_def_id(variant.fields[index].did);
130         }
131     }
132
133     fn mark_live_symbols(&mut self) {
134         let mut scanned = FxHashSet::default();
135         while let Some(id) = self.worklist.pop() {
136             if !scanned.insert(id) {
137                 continue
138             }
139
140             if let Some(ref node) = self.tcx.hir.find(id) {
141                 self.live_symbols.insert(id);
142                 self.visit_node(node);
143             }
144         }
145     }
146
147     fn visit_node(&mut self, node: &Node<'tcx>) {
148         let had_repr_c = self.repr_has_repr_c;
149         self.repr_has_repr_c = false;
150         let had_inherited_pub_visibility = self.inherited_pub_visibility;
151         self.inherited_pub_visibility = false;
152         match *node {
153             Node::Item(item) => {
154                 match item.node {
155                     hir::ItemKind::Struct(..) | hir::ItemKind::Union(..) => {
156                         let def_id = self.tcx.hir.local_def_id(item.id);
157                         let def = self.tcx.adt_def(def_id);
158                         self.repr_has_repr_c = def.repr.c();
159
160                         intravisit::walk_item(self, &item);
161                     }
162                     hir::ItemKind::Enum(..) => {
163                         self.inherited_pub_visibility = item.vis.node.is_pub();
164                         intravisit::walk_item(self, &item);
165                     }
166                     hir::ItemKind::Fn(..)
167                     | hir::ItemKind::Ty(..)
168                     | hir::ItemKind::Static(..)
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.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, _: ast::NodeId, _: 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.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.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: ast::NodeId,
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(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<ast::NodeId>,
337     krate: &'k hir::Crate,
338     tcx: TyCtxt<'k, 'tcx, 'tcx>,
339 }
340
341 impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> {
342     fn visit_item(&mut self, item: &hir::Item) {
343         let allow_dead_code = has_allow_dead_code_or_lang_attr(self.tcx,
344                                                                item.id,
345                                                                &item.attrs);
346         if allow_dead_code {
347             self.worklist.push(item.id);
348         }
349         match item.node {
350             hir::ItemKind::Enum(ref enum_def, _) if allow_dead_code => {
351                 self.worklist.extend(enum_def.variants.iter()
352                                                       .map(|variant| variant.node.data.id()));
353             }
354             hir::ItemKind::Trait(.., ref trait_item_refs) => {
355                 for trait_item_ref in trait_item_refs {
356                     let trait_item = self.krate.trait_item(trait_item_ref.id);
357                     match trait_item.node {
358                         hir::TraitItemKind::Const(_, Some(_)) |
359                         hir::TraitItemKind::Method(_, hir::TraitMethod::Provided(_)) => {
360                             if has_allow_dead_code_or_lang_attr(self.tcx,
361                                                                 trait_item.id,
362                                                                 &trait_item.attrs) {
363                                 self.worklist.push(trait_item.id);
364                             }
365                         }
366                         _ => {}
367                     }
368                 }
369             }
370             hir::ItemKind::Impl(.., ref opt_trait, _, ref impl_item_refs) => {
371                 for impl_item_ref in impl_item_refs {
372                     let impl_item = self.krate.impl_item(impl_item_ref.id);
373                     if opt_trait.is_some() ||
374                             has_allow_dead_code_or_lang_attr(self.tcx,
375                                                              impl_item.id,
376                                                              &impl_item.attrs) {
377                         self.worklist.push(impl_item_ref.id.node_id);
378                     }
379                 }
380             }
381             _ => ()
382         }
383     }
384
385     fn visit_trait_item(&mut self, _item: &hir::TraitItem) {
386         // ignore: we are handling this in `visit_item` above
387     }
388
389     fn visit_impl_item(&mut self, _item: &hir::ImplItem) {
390         // ignore: we are handling this in `visit_item` above
391     }
392 }
393
394 fn create_and_seed_worklist<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
395                                       access_levels: &privacy::AccessLevels,
396                                       krate: &hir::Crate)
397                                       -> Vec<ast::NodeId>
398 {
399     let worklist = access_levels.map.iter().filter_map(|(&id, level)| {
400         if level >= &privacy::AccessLevel::Reachable {
401             Some(id)
402         } else {
403             None
404         }
405     }).chain(
406         // Seed entry point
407         tcx.sess.entry_fn.borrow().map(|(id, _, _)| id)
408     ).collect::<Vec<_>>();
409
410     // Seed implemented trait items
411     let mut life_seeder = LifeSeeder {
412         worklist,
413         krate,
414         tcx,
415     };
416     krate.visit_all_item_likes(&mut life_seeder);
417
418     return life_seeder.worklist;
419 }
420
421 fn find_live<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
422                        access_levels: &privacy::AccessLevels,
423                        krate: &hir::Crate)
424                        -> FxHashSet<ast::NodeId> {
425     let worklist = create_and_seed_worklist(tcx, access_levels, krate);
426     let mut symbol_visitor = MarkSymbolVisitor {
427         worklist,
428         tcx,
429         tables: &ty::TypeckTables::empty(None),
430         live_symbols: Default::default(),
431         repr_has_repr_c: false,
432         in_pat: false,
433         inherited_pub_visibility: false,
434         ignore_variant_stack: vec![],
435     };
436     symbol_visitor.mark_live_symbols();
437     symbol_visitor.live_symbols
438 }
439
440 fn get_struct_ctor_id(item: &hir::Item) -> Option<ast::NodeId> {
441     match item.node {
442         hir::ItemKind::Struct(ref struct_def, _) if !struct_def.is_struct() => {
443             Some(struct_def.id())
444         }
445         _ => None
446     }
447 }
448
449 struct DeadVisitor<'a, 'tcx: 'a> {
450     tcx: TyCtxt<'a, 'tcx, 'tcx>,
451     live_symbols: FxHashSet<ast::NodeId>,
452 }
453
454 impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
455     fn should_warn_about_item(&mut self, item: &hir::Item) -> bool {
456         let should_warn = match item.node {
457             hir::ItemKind::Static(..)
458             | hir::ItemKind::Const(..)
459             | hir::ItemKind::Fn(..)
460             | hir::ItemKind::Ty(..)
461             | hir::ItemKind::Enum(..)
462             | hir::ItemKind::Struct(..)
463             | hir::ItemKind::Union(..) => true,
464             _ => false
465         };
466         let ctor_id = get_struct_ctor_id(item);
467         should_warn && !self.symbol_is_live(item.id, ctor_id)
468     }
469
470     fn should_warn_about_field(&mut self, field: &hir::StructField) -> bool {
471         let field_type = self.tcx.type_of(self.tcx.hir.local_def_id(field.id));
472         !field.is_positional()
473             && !self.symbol_is_live(field.id, None)
474             && !field_type.is_phantom_data()
475             && !has_allow_dead_code_or_lang_attr(self.tcx, field.id, &field.attrs)
476     }
477
478     fn should_warn_about_variant(&mut self, variant: &hir::VariantKind) -> bool {
479         !self.symbol_is_live(variant.data.id(), None)
480             && !has_allow_dead_code_or_lang_attr(self.tcx,
481                                                  variant.data.id(),
482                                                  &variant.attrs)
483     }
484
485     fn should_warn_about_foreign_item(&mut self, fi: &hir::ForeignItem) -> bool {
486         !self.symbol_is_live(fi.id, None)
487             && !has_allow_dead_code_or_lang_attr(self.tcx, fi.id, &fi.attrs)
488     }
489
490     // id := node id of an item's definition.
491     // ctor_id := `Some` if the item is a struct_ctor (tuple struct),
492     //            `None` otherwise.
493     // If the item is a struct_ctor, then either its `id` or
494     // `ctor_id` (unwrapped) is in the live_symbols set. More specifically,
495     // DefMap maps the ExprKind::Path of a struct_ctor to the node referred by
496     // `ctor_id`. On the other hand, in a statement like
497     // `type <ident> <generics> = <ty>;` where <ty> refers to a struct_ctor,
498     // DefMap maps <ty> to `id` instead.
499     fn symbol_is_live(&mut self,
500                       id: ast::NodeId,
501                       ctor_id: Option<ast::NodeId>)
502                       -> bool {
503         if self.live_symbols.contains(&id)
504            || ctor_id.map_or(false, |ctor| self.live_symbols.contains(&ctor))
505         {
506             return true;
507         }
508         // If it's a type whose items are live, then it's live, too.
509         // This is done to handle the case where, for example, the static
510         // method of a private type is used, but the type itself is never
511         // called directly.
512         let def_id = self.tcx.hir.local_def_id(id);
513         let inherent_impls = self.tcx.inherent_impls(def_id);
514         for &impl_did in inherent_impls.iter() {
515             for &item_did in &self.tcx.associated_item_def_ids(impl_did)[..] {
516                 if let Some(item_node_id) = self.tcx.hir.as_local_node_id(item_did) {
517                     if self.live_symbols.contains(&item_node_id) {
518                         return true;
519                     }
520                 }
521             }
522         }
523         false
524     }
525
526     fn warn_dead_code(&mut self,
527                       id: ast::NodeId,
528                       span: syntax_pos::Span,
529                       name: ast::Name,
530                       node_type: &str,
531                       participle: &str) {
532         if !name.as_str().starts_with("_") {
533             self.tcx
534                 .lint_node(lint::builtin::DEAD_CODE,
535                            id,
536                            span,
537                            &format!("{} is never {}: `{}`",
538                                     node_type, participle, name));
539         }
540     }
541 }
542
543 impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
544     /// Walk nested items in place so that we don't report dead-code
545     /// on inner functions when the outer function is already getting
546     /// an error. We could do this also by checking the parents, but
547     /// this is how the code is setup and it seems harmless enough.
548     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
549         NestedVisitorMap::All(&self.tcx.hir)
550     }
551
552     fn visit_item(&mut self, item: &'tcx hir::Item) {
553         if self.should_warn_about_item(item) {
554             // For items that have a definition with a signature followed by a
555             // block, point only at the signature.
556             let span = match item.node {
557                 hir::ItemKind::Fn(..) |
558                 hir::ItemKind::Mod(..) |
559                 hir::ItemKind::Enum(..) |
560                 hir::ItemKind::Struct(..) |
561                 hir::ItemKind::Union(..) |
562                 hir::ItemKind::Trait(..) |
563                 hir::ItemKind::Impl(..) => self.tcx.sess.source_map().def_span(item.span),
564                 _ => item.span,
565             };
566             let participle = match item.node {
567                 hir::ItemKind::Struct(..) => "constructed", // Issue #52325
568                 _ => "used"
569             };
570             self.warn_dead_code(
571                 item.id,
572                 span,
573                 item.name,
574                 item.node.descriptive_variant(),
575                 participle,
576             );
577         } else {
578             // Only continue if we didn't warn
579             intravisit::walk_item(self, item);
580         }
581     }
582
583     fn visit_variant(&mut self,
584                      variant: &'tcx hir::Variant,
585                      g: &'tcx hir::Generics,
586                      id: ast::NodeId) {
587         if self.should_warn_about_variant(&variant.node) {
588             self.warn_dead_code(variant.node.data.id(), variant.span, variant.node.name,
589                                 "variant", "constructed");
590         } else {
591             intravisit::walk_variant(self, variant, g, id);
592         }
593     }
594
595     fn visit_foreign_item(&mut self, fi: &'tcx hir::ForeignItem) {
596         if self.should_warn_about_foreign_item(fi) {
597             self.warn_dead_code(fi.id, fi.span, fi.name,
598                                 fi.node.descriptive_variant(), "used");
599         }
600         intravisit::walk_foreign_item(self, fi);
601     }
602
603     fn visit_struct_field(&mut self, field: &'tcx hir::StructField) {
604         if self.should_warn_about_field(&field) {
605             self.warn_dead_code(field.id, field.span, field.ident.name, "field", "used");
606         }
607         intravisit::walk_struct_field(self, field);
608     }
609
610     fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
611         match impl_item.node {
612             hir::ImplItemKind::Const(_, body_id) => {
613                 if !self.symbol_is_live(impl_item.id, None) {
614                     self.warn_dead_code(impl_item.id,
615                                         impl_item.span,
616                                         impl_item.ident.name,
617                                         "associated const",
618                                         "used");
619                 }
620                 self.visit_nested_body(body_id)
621             }
622             hir::ImplItemKind::Method(_, body_id) => {
623                 if !self.symbol_is_live(impl_item.id, None) {
624                     let span = self.tcx.sess.source_map().def_span(impl_item.span);
625                     self.warn_dead_code(impl_item.id, span, impl_item.ident.name, "method", "used");
626                 }
627                 self.visit_nested_body(body_id)
628             }
629             hir::ImplItemKind::Existential(..) |
630             hir::ImplItemKind::Type(..) => {}
631         }
632     }
633
634     // Overwrite so that we don't warn the trait item itself.
635     fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
636         match trait_item.node {
637             hir::TraitItemKind::Const(_, Some(body_id)) |
638             hir::TraitItemKind::Method(_, hir::TraitMethod::Provided(body_id)) => {
639                 self.visit_nested_body(body_id)
640             }
641             hir::TraitItemKind::Const(_, None) |
642             hir::TraitItemKind::Method(_, hir::TraitMethod::Required(_)) |
643             hir::TraitItemKind::Type(..) => {}
644         }
645     }
646 }
647
648 pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
649     let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE);
650     let krate = tcx.hir.krate();
651     let live_symbols = find_live(tcx, access_levels, krate);
652     let mut visitor = DeadVisitor {
653         tcx,
654         live_symbols,
655     };
656     intravisit::walk_crate(&mut visitor, krate);
657 }