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::itemlikevisit::ItemLikeVisitor;
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) {
34 let msg = if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(span) {
35 format!("unused import: `{}`", snippet)
37 "unused import".to_string()
39 self.tcx.sess.add_lint(lint::builtin::UNUSED_IMPORTS, id, span, msg);
43 impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for UnusedTraitImportVisitor<'a, 'tcx> {
44 fn visit_item(&mut self, item: &hir::Item) {
45 if item.vis == hir::Public || item.span == DUMMY_SP {
48 if let hir::ItemUse(ref path) = item.node {
50 hir::ViewPathSimple(..) | hir::ViewPathGlob(..) => {
51 self.check_import(item.id, path.span);
53 hir::ViewPathList(_, ref path_list) => {
54 for path_item in path_list {
55 self.check_import(path_item.node.id, path_item.span);
62 fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
66 pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
67 let _task = tcx.dep_graph.in_task(DepNode::UnusedTraitCheck);
68 let mut visitor = UnusedTraitImportVisitor { tcx: tcx };
69 tcx.map.krate().visit_all_item_likes(&mut visitor);