]> git.lizzy.rs Git - rust.git/blob - src/librustc_resolve/check_unused.rs
870990f78607c2e305735d7d1420c7c4f4121d0b
[rust.git] / src / librustc_resolve / check_unused.rs
1 // Copyright 2012-2014 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
12 //
13 // Unused import checking
14 //
15 // Although this is mostly a lint pass, it lives in here because it depends on
16 // resolve data structures and because it finalises the privacy information for
17 // `use` directives.
18 //
19
20 use std::ops::{Deref, DerefMut};
21
22 use Resolver;
23 use Namespace::{TypeNS, ValueNS};
24
25 use rustc::lint;
26 use rustc::middle::privacy::{DependsOn, LastImport, Used, Unused};
27 use syntax::ast;
28 use syntax::codemap::{Span, DUMMY_SP};
29
30 use rustc_front::hir;
31 use rustc_front::hir::{ViewPathGlob, ViewPathList, ViewPathSimple};
32 use rustc_front::visit::{self, Visitor};
33
34 struct UnusedImportCheckVisitor<'a, 'b: 'a, 'tcx: 'b> {
35     resolver: &'a mut Resolver<'b, 'tcx>,
36 }
37
38 // Deref and DerefMut impls allow treating UnusedImportCheckVisitor as Resolver.
39 impl<'a, 'b, 'tcx:'b> Deref for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
40     type Target = Resolver<'b, 'tcx>;
41
42     fn deref<'c>(&'c self) -> &'c Resolver<'b, 'tcx> {
43         &*self.resolver
44     }
45 }
46
47 impl<'a, 'b, 'tcx:'b> DerefMut for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
48     fn deref_mut<'c>(&'c mut self) -> &'c mut Resolver<'b, 'tcx> {
49         &mut *self.resolver
50     }
51 }
52
53 impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
54     // We have information about whether `use` (import) directives are actually
55     // used now. If an import is not used at all, we signal a lint error. If an
56     // import is only used for a single namespace, we remove the other namespace
57     // from the recorded privacy information. That means in privacy.rs, we will
58     // only check imports and namespaces which are used. In particular, this
59     // means that if an import could name either a public or private item, we
60     // will check the correct thing, dependent on how the import is used.
61     fn finalize_import(&mut self, id: ast::NodeId, span: Span) {
62         debug!("finalizing import uses for {:?}",
63                self.session.codemap().span_to_snippet(span));
64
65         if !self.used_imports.contains(&(id, TypeNS)) &&
66            !self.used_imports.contains(&(id, ValueNS)) {
67             self.session.add_lint(lint::builtin::UNUSED_IMPORTS,
68                                   id,
69                                   span,
70                                   "unused import".to_string());
71         }
72
73         let mut def_map = self.def_map.borrow_mut();
74         let path_res = if let Some(r) = def_map.get_mut(&id) {
75             r
76         } else {
77             return;
78         };
79         let (v_priv, t_priv) = match path_res.last_private {
80             LastImport { value_priv, type_priv, .. } => (value_priv, type_priv),
81             _ => {
82                 panic!("we should only have LastImport for `use` directives")
83             }
84         };
85
86         let mut v_used = if self.used_imports.contains(&(id, ValueNS)) {
87             Used
88         } else {
89             Unused
90         };
91         let t_used = if self.used_imports.contains(&(id, TypeNS)) {
92             Used
93         } else {
94             Unused
95         };
96
97         match (v_priv, t_priv) {
98             // Since some items may be both in the value _and_ type namespaces (e.g., structs)
99             // we might have two LastPrivates pointing at the same thing. There is no point
100             // checking both, so lets not check the value one.
101             (Some(DependsOn(def_v)), Some(DependsOn(def_t))) if def_v == def_t => v_used = Unused,
102             _ => {}
103         }
104
105         path_res.last_private = LastImport {
106             value_priv: v_priv,
107             value_used: v_used,
108             type_priv: t_priv,
109             type_used: t_used,
110         };
111     }
112 }
113
114 impl<'a, 'b, 'v, 'tcx> Visitor<'v> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
115     fn visit_item(&mut self, item: &hir::Item) {
116         // Ignore is_public import statements because there's no way to be sure
117         // whether they're used or not. Also ignore imports with a dummy span
118         // because this means that they were generated in some fashion by the
119         // compiler and we don't need to consider them.
120         if item.vis == hir::Public || item.span == DUMMY_SP {
121             visit::walk_item(self, item);
122             return;
123         }
124
125         match item.node {
126             hir::ItemExternCrate(_) => {
127                 if let Some(crate_num) = self.session.cstore.find_extern_mod_stmt_cnum(item.id) {
128                     if !self.used_crates.contains(&crate_num) {
129                         self.session.add_lint(lint::builtin::UNUSED_EXTERN_CRATES,
130                                               item.id,
131                                               item.span,
132                                               "unused extern crate".to_string());
133                     }
134                 }
135             }
136             hir::ItemUse(ref p) => {
137                 match p.node {
138                     ViewPathSimple(_, _) => {
139                         self.finalize_import(item.id, p.span)
140                     }
141
142                     ViewPathList(_, ref list) => {
143                         for i in list {
144                             self.finalize_import(i.node.id(), i.span);
145                         }
146                     }
147                     ViewPathGlob(_) => {
148                         if !self.used_imports.contains(&(item.id, TypeNS)) &&
149                            !self.used_imports.contains(&(item.id, ValueNS)) {
150                             self.session
151                                 .add_lint(lint::builtin::UNUSED_IMPORTS,
152                                           item.id,
153                                           p.span,
154                                           "unused import".to_string());
155                         }
156                     }
157                 }
158             }
159             _ => {}
160         }
161
162         visit::walk_item(self, item);
163     }
164 }
165
166 pub fn check_crate(resolver: &mut Resolver, krate: &hir::Crate) {
167     let mut visitor = UnusedImportCheckVisitor { resolver: resolver };
168     visit::walk_crate(&mut visitor, krate);
169 }