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