]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check_unused.rs
Changed issue number to 36105
[rust.git] / src / librustc_typeck / check_unused.rs
1 // Copyright 2016 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 use lint;
12 use rustc::dep_graph::DepNode;
13 use rustc::ty::TyCtxt;
14
15 use syntax::ast;
16 use syntax_pos::{Span, DUMMY_SP};
17
18 use rustc::hir;
19 use rustc::hir::intravisit::Visitor;
20
21 struct UnusedTraitImportVisitor<'a, 'tcx: 'a> {
22     tcx: TyCtxt<'a, 'tcx, 'tcx>,
23 }
24
25 impl<'a, 'tcx> UnusedTraitImportVisitor<'a, 'tcx> {
26     fn check_import(&self, id: ast::NodeId, span: Span) {
27         if !self.tcx.maybe_unused_trait_imports.contains(&id) {
28             return;
29         }
30         if self.tcx.used_trait_imports.borrow().contains(&id) {
31             return;
32         }
33         self.tcx.sess.add_lint(lint::builtin::UNUSED_IMPORTS,
34                                id,
35                                span,
36                                "unused import".to_string());
37     }
38 }
39
40 impl<'a, 'tcx, 'v> Visitor<'v> for UnusedTraitImportVisitor<'a, 'tcx> {
41     fn visit_item(&mut self, item: &hir::Item) {
42         if item.vis == hir::Public || item.span == DUMMY_SP {
43             return;
44         }
45         if let hir::ItemUse(ref path) = item.node {
46             match path.node {
47                 hir::ViewPathSimple(..) | hir::ViewPathGlob(..) => {
48                     self.check_import(item.id, path.span);
49                 }
50                 hir::ViewPathList(_, ref path_list) => {
51                     for path_item in path_list {
52                         self.check_import(path_item.node.id(), path_item.span);
53                     }
54                 }
55             }
56         }
57     }
58 }
59
60 pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
61     let _task = tcx.dep_graph.in_task(DepNode::UnusedTraitCheck);
62     let mut visitor = UnusedTraitImportVisitor { tcx: tcx };
63     tcx.map.krate().visit_all_items(&mut visitor);
64 }