]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check_unused.rs
rustc: desugar `use a::{b,c};` into `use a::b; use a::c;` in HIR.
[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::itemlikevisit::ItemLikeVisitor;
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
34         let msg = if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(span) {
35             format!("unused import: `{}`", snippet)
36         } else {
37             "unused import".to_string()
38         };
39         self.tcx.sess.add_lint(lint::builtin::UNUSED_IMPORTS, id, span, msg);
40     }
41 }
42
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 {
46             return;
47         }
48         if let hir::ItemUse(ref path, _) = item.node {
49             self.check_import(item.id, path.span);
50         }
51     }
52
53     fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
54     }
55 }
56
57 pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
58     let _task = tcx.dep_graph.in_task(DepNode::UnusedTraitCheck);
59     let mut visitor = UnusedTraitImportVisitor { tcx: tcx };
60     tcx.map.krate().visit_all_item_likes(&mut visitor);
61 }