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.
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.
12 use rustc::dep_graph::DepNode;
13 use rustc::ty::TyCtxt;
16 use syntax_pos::{Span, DUMMY_SP};
19 use rustc::hir::intravisit::Visitor;
21 struct UnusedTraitImportVisitor<'a, 'tcx: 'a> {
22 tcx: TyCtxt<'a, 'tcx, 'tcx>,
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) {
30 if self.tcx.used_trait_imports.borrow().contains(&id) {
33 self.tcx.sess.add_lint(lint::builtin::UNUSED_IMPORTS,
36 "unused import".to_string());
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 {
45 if let hir::ItemUse(ref path) = item.node {
47 hir::ViewPathSimple(..) | hir::ViewPathGlob(..) => {
48 self.check_import(item.id, path.span);
50 hir::ViewPathList(_, ref path_list) => {
51 for path_item in path_list {
52 self.check_import(path_item.node.id(), path_item.span);
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);