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