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