rust/src/librustc_resolve/check_unused.rs

164 lines
6.0 KiB
Rust
Raw Normal View History

// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// Unused import checking
//
// Although this is mostly a lint pass, it lives in here because it depends on
// resolve data structures and because it finalises the privacy information for
// `use` directives.
//
2016-04-19 22:43:10 +09:00
// Unused trait imports can't be checked until the method resolution. We save
// candidates here, and do the acutal check in librustc_typeck/check_unused.rs.
use std::ops::{Deref, DerefMut};
use Resolver;
use resolve_imports::ImportDirectiveSubclass;
use rustc::{lint, ty};
use rustc::util::nodemap::NodeMap;
2016-04-24 03:26:10 +00:00
use syntax::ast::{self, ViewPathGlob, ViewPathList, ViewPathSimple};
use syntax::visit::{self, Visitor};
use syntax_pos::{Span, MultiSpan, DUMMY_SP};
2015-07-31 00:04:06 -07:00
2016-04-24 03:26:10 +00:00
struct UnusedImportCheckVisitor<'a, 'b: 'a> {
resolver: &'a mut Resolver<'b>,
/// All the (so far) unused imports, grouped path list
unused_imports: NodeMap<NodeMap<Span>>,
}
// Deref and DerefMut impls allow treating UnusedImportCheckVisitor as Resolver.
2016-04-24 03:26:10 +00:00
impl<'a, 'b> Deref for UnusedImportCheckVisitor<'a, 'b> {
type Target = Resolver<'b>;
2015-01-01 14:53:20 -05:00
2016-04-24 03:26:10 +00:00
fn deref<'c>(&'c self) -> &'c Resolver<'b> {
&*self.resolver
}
}
2016-04-24 03:26:10 +00:00
impl<'a, 'b> DerefMut for UnusedImportCheckVisitor<'a, 'b> {
fn deref_mut<'c>(&'c mut self) -> &'c mut Resolver<'b> {
&mut *self.resolver
}
}
2016-04-24 03:26:10 +00:00
impl<'a, 'b> UnusedImportCheckVisitor<'a, 'b> {
2015-10-26 20:31:11 +01:00
// We have information about whether `use` (import) directives are actually
2016-04-04 20:32:42 +09:00
// used now. If an import is not used at all, we signal a lint error.
fn check_import(&mut self, item_id: ast::NodeId, id: ast::NodeId, span: Span) {
2016-11-10 06:19:54 +00:00
let mut used = false;
self.per_ns(|this, ns| used |= this.used_imports.contains(&(id, ns)));
if !used {
2016-04-19 22:43:10 +09:00
if self.maybe_unused_trait_imports.contains(&id) {
// Check later.
return;
}
self.unused_imports.entry(item_id).or_insert_with(NodeMap).insert(id, span);
2016-04-19 22:43:10 +09:00
} else {
// This trait import is definitely used, in a way other than
// method resolution.
self.maybe_unused_trait_imports.remove(&id);
if let Some(i) = self.unused_imports.get_mut(&item_id) {
i.remove(&id);
}
}
}
}
impl<'a, 'b> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b> {
fn visit_item(&mut self, item: &'a ast::Item) {
2016-04-24 03:26:10 +00:00
visit::walk_item(self, item);
// Ignore is_public import statements because there's no way to be sure
// whether they're used or not. Also ignore imports with a dummy span
// because this means that they were generated in some fashion by the
// compiler and we don't need to consider them.
2016-04-24 03:26:10 +00:00
if item.vis == ast::Visibility::Public || item.span.source_equal(&DUMMY_SP) {
return;
}
match item.node {
2016-04-24 03:26:10 +00:00
ast::ItemKind::Use(ref p) => {
match p.node {
2016-08-26 19:23:42 +03:00
ViewPathSimple(..) => {
self.check_import(item.id, item.id, p.span)
}
ViewPathList(_, ref list) => {
if list.len() == 0 {
self.unused_imports
.entry(item.id)
.or_insert_with(NodeMap)
.insert(item.id, item.span);
}
2015-01-31 12:20:46 -05:00
for i in list {
self.check_import(item.id, i.node.id, i.span);
}
}
ViewPathGlob(_) => {
self.check_import(item.id, item.id, p.span);
}
}
}
_ => {}
}
}
}
2016-04-24 03:26:10 +00:00
pub fn check_crate(resolver: &mut Resolver, krate: &ast::Crate) {
for directive in resolver.potentially_unused_imports.iter() {
match directive.subclass {
_ if directive.used.get() ||
directive.vis.get() == ty::Visibility::Public ||
directive.span.source_equal(&DUMMY_SP) => {}
ImportDirectiveSubclass::ExternCrate(_) => {
2017-08-13 11:58:17 +09:00
resolver.maybe_unused_extern_crates.push((directive.id, directive.span));
}
2017-01-14 08:58:50 +00:00
ImportDirectiveSubclass::MacroUse => {
let lint = lint::builtin::UNUSED_IMPORTS;
rustc: Rearchitect lints to be emitted more eagerly In preparation for incremental compilation this commit refactors the lint handling infrastructure in the compiler to be more "eager" and overall more incremental-friendly. Many passes of the compiler can emit lints at various points but before this commit all lints were buffered in a table to be emitted at the very end of compilation. This commit changes these lints to be emitted immediately during compilation using pre-calculated lint level-related data structures. Linting today is split into two phases, one set of "early" lints run on the `syntax::ast` and a "late" set of lints run on the HIR. This commit moves the "early" lints to running as late as possible in compilation, just before HIR lowering. This notably means that we're catching resolve-related lints just before HIR lowering. The early linting remains a pass very similar to how it was before, maintaining context of the current lint level as it walks the tree. Post-HIR, however, linting is structured as a method on the `TyCtxt` which transitively executes a query to calculate lint levels. Each request to lint on a `TyCtxt` will query the entire crate's 'lint level data structure' and then go from there about whether the lint should be emitted or not. The query depends on the entire HIR crate but should be very quick to calculate (just a quick walk of the HIR) and the red-green system should notice that the lint level data structure rarely changes, and should hopefully preserve incrementality. Overall this resulted in a pretty big change to the test suite now that lints are emitted much earlier in compilation (on-demand vs only at the end). This in turn necessitated the addition of many `#![allow(warnings)]` directives throughout the compile-fail test suite and a number of updates to the UI test suite.
2017-07-26 21:51:09 -07:00
let msg = "unused `#[macro_use]` import";
resolver.session.buffer_lint(lint, directive.id, directive.span, msg);
2017-01-14 08:58:50 +00:00
}
_ => {}
}
}
let mut visitor = UnusedImportCheckVisitor {
resolver,
unused_imports: NodeMap(),
};
2016-04-24 03:26:10 +00:00
visit::walk_crate(&mut visitor, krate);
for (id, spans) in &visitor.unused_imports {
let len = spans.len();
let mut spans = spans.values().map(|s| *s).collect::<Vec<Span>>();
spans.sort();
let ms = MultiSpan::from_spans(spans.clone());
let mut span_snippets = spans.iter()
.filter_map(|s| {
match visitor.session.codemap().span_to_snippet(*s) {
Ok(s) => Some(format!("`{}`", s)),
_ => None,
}
}).collect::<Vec<String>>();
span_snippets.sort();
let msg = format!("unused import{}{}",
if len > 1 { "s" } else { "" },
if span_snippets.len() > 0 {
format!(": {}", span_snippets.join(", "))
} else {
String::new()
});
rustc: Rearchitect lints to be emitted more eagerly In preparation for incremental compilation this commit refactors the lint handling infrastructure in the compiler to be more "eager" and overall more incremental-friendly. Many passes of the compiler can emit lints at various points but before this commit all lints were buffered in a table to be emitted at the very end of compilation. This commit changes these lints to be emitted immediately during compilation using pre-calculated lint level-related data structures. Linting today is split into two phases, one set of "early" lints run on the `syntax::ast` and a "late" set of lints run on the HIR. This commit moves the "early" lints to running as late as possible in compilation, just before HIR lowering. This notably means that we're catching resolve-related lints just before HIR lowering. The early linting remains a pass very similar to how it was before, maintaining context of the current lint level as it walks the tree. Post-HIR, however, linting is structured as a method on the `TyCtxt` which transitively executes a query to calculate lint levels. Each request to lint on a `TyCtxt` will query the entire crate's 'lint level data structure' and then go from there about whether the lint should be emitted or not. The query depends on the entire HIR crate but should be very quick to calculate (just a quick walk of the HIR) and the red-green system should notice that the lint level data structure rarely changes, and should hopefully preserve incrementality. Overall this resulted in a pretty big change to the test suite now that lints are emitted much earlier in compilation (on-demand vs only at the end). This in turn necessitated the addition of many `#![allow(warnings)]` directives throughout the compile-fail test suite and a number of updates to the UI test suite.
2017-07-26 21:51:09 -07:00
visitor.session.buffer_lint(lint::builtin::UNUSED_IMPORTS, *id, ms, &msg);
}
}