Skip to content

Commit

Permalink
Warn on unused #[macro_use] imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Jan 22, 2017
1 parent 2efec3c commit 356fa2c
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/libproc_macro_tokens/lib.rs
Expand Up @@ -59,7 +59,7 @@

extern crate syntax;
extern crate syntax_pos;
#[macro_use] extern crate log;
extern crate log;

pub mod build;
pub mod parse;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lib.rs
Expand Up @@ -57,7 +57,7 @@ extern crate rustc_const_math;
extern crate rustc_errors as errors;
#[macro_use] extern crate log;
#[macro_use] extern crate syntax;
#[macro_use] extern crate syntax_pos;
extern crate syntax_pos;
#[macro_use] #[no_link] extern crate rustc_bitflags;

extern crate serialize as rustc_serialize; // used by deriving
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_const_math/lib.rs
Expand Up @@ -28,8 +28,8 @@
#![feature(const_fn)]
#![cfg_attr(not(stage0), feature(i128))]

#[macro_use] extern crate log;
#[macro_use] extern crate syntax;
extern crate log;
extern crate syntax;

// SNAP: remove use of this crate
extern crate rustc_i128;
Expand Down
1 change: 0 additions & 1 deletion src/librustc_driver/lib.rs
Expand Up @@ -57,7 +57,6 @@ extern crate serialize;
extern crate rustc_llvm as llvm;
#[macro_use]
extern crate log;
#[macro_use]
extern crate syntax;
extern crate syntax_ext;
extern crate syntax_pos;
Expand Down
2 changes: 0 additions & 2 deletions src/librustc_errors/lib.rs
Expand Up @@ -27,9 +27,7 @@

extern crate serialize;
extern crate term;
#[macro_use]
extern crate log;
#[macro_use]
extern crate libc;
extern crate std_unicode;
extern crate serialize as rustc_serialize; // used by deriving
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_incremental/lib.rs
Expand Up @@ -30,7 +30,7 @@ extern crate rustc_data_structures;
extern crate serialize as rustc_serialize;

#[macro_use] extern crate log;
#[macro_use] extern crate syntax;
extern crate syntax;
extern crate syntax_pos;

extern crate rustc_i128;
Expand Down
1 change: 0 additions & 1 deletion src/librustc_lint/lib.rs
Expand Up @@ -37,7 +37,6 @@
#![feature(slice_patterns)]
#![feature(staged_api)]

#[macro_use]
extern crate syntax;
#[macro_use]
extern crate rustc;
Expand Down
1 change: 0 additions & 1 deletion src/librustc_plugin/Cargo.toml
Expand Up @@ -13,7 +13,6 @@ crate-type = ["dylib"]
log = { path = "../liblog" }
rustc = { path = "../librustc" }
rustc_back = { path = "../librustc_back" }
rustc_bitflags = { path = "../librustc_bitflags" }
rustc_metadata = { path = "../librustc_metadata" }
syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_plugin/lib.rs
Expand Up @@ -63,9 +63,8 @@
#![feature(rustc_diagnostic_macros)]
#![feature(rustc_private)]

#[macro_use] extern crate log;
extern crate log;
#[macro_use] extern crate syntax;
#[macro_use] #[no_link] extern crate rustc_bitflags;

extern crate rustc;
extern crate rustc_back;
Expand Down
23 changes: 21 additions & 2 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -552,16 +552,35 @@ impl<'a> Resolver<'a> {
used = true; // Avoid the normal unused extern crate warning
}

let (graph_root, arenas) = (self.graph_root, self.arenas);
let macro_use_directive = |span| arenas.alloc_import_directive(ImportDirective {
id: item.id,
parent: graph_root,
imported_module: Cell::new(Some(module)),
subclass: ImportDirectiveSubclass::MacroUse,
span: span,
module_path: Vec::new(),
vis: Cell::new(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))),
expansion: expansion,
used: Cell::new(false),
});

if let Some(span) = legacy_imports.import_all {
let directive = macro_use_directive(span);
self.potentially_unused_imports.push(directive);
module.for_each_child(|ident, ns, binding| if ns == MacroNS {
self.legacy_import_macro(ident.name, binding, span, allow_shadowing);
let imported_binding = self.import(binding, directive);
self.legacy_import_macro(ident.name, imported_binding, span, allow_shadowing);
});
} else {
for (name, span) in legacy_imports.imports {
let ident = Ident::with_empty_ctxt(name);
let result = self.resolve_ident_in_module(module, ident, MacroNS, false, None);
if let Ok(binding) = result {
self.legacy_import_macro(name, binding, span, allow_shadowing);
let directive = macro_use_directive(span);
self.potentially_unused_imports.push(directive);
let imported_binding = self.import(binding, directive);
self.legacy_import_macro(name, imported_binding, span, allow_shadowing);
} else {
span_err!(self.session, span, E0469, "imported macro not found");
}
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_resolve/check_unused.rs
Expand Up @@ -125,6 +125,11 @@ pub fn check_crate(resolver: &mut Resolver, krate: &ast::Crate) {
let msg = "unused extern crate".to_string();
resolver.session.add_lint(lint, directive.id, directive.span, msg);
}
ImportDirectiveSubclass::MacroUse => {
let lint = lint::builtin::UNUSED_IMPORTS;
let msg = "unused `#[macro_use]` import".to_string();
resolver.session.add_lint(lint, directive.id, directive.span, msg);
}
_ => {}
}
}
Expand Down
16 changes: 10 additions & 6 deletions src/librustc_resolve/macros.rs
Expand Up @@ -341,12 +341,15 @@ impl<'a> Resolver<'a> {
};
}

let binding = match binding {
Some(binding) => MacroBinding::Legacy(binding),
None => match self.builtin_macros.get(&name).cloned() {
Some(binding) => MacroBinding::Modern(binding),
None => return None,
},
let binding = if let Some(binding) = binding {
MacroBinding::Legacy(binding)
} else if let Some(binding) = self.builtin_macros.get(&name).cloned() {
if !self.use_extern_macros {
self.record_use(Ident::with_empty_ctxt(name), MacroNS, binding, DUMMY_SP);
}
MacroBinding::Modern(binding)
} else {
return None;
};

if !self.use_extern_macros {
Expand Down Expand Up @@ -378,6 +381,7 @@ impl<'a> Resolver<'a> {
let (legacy_resolution, resolution) = match (legacy_resolution, resolution) {
(Some(legacy_resolution), Ok(resolution)) => (legacy_resolution, resolution),
(Some(MacroBinding::Modern(binding)), Err(_)) => {
self.record_use(ident, MacroNS, binding, span);
self.err_if_macro_use_proc_macro(ident.name, span, binding);
continue
},
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_resolve/resolve_imports.rs
Expand Up @@ -49,6 +49,7 @@ pub enum ImportDirectiveSubclass<'a> {
// n.b. `max_vis` is only used in `finalize_import` to check for reexport errors.
},
ExternCrate,
MacroUse,
}

/// One import directive.
Expand Down Expand Up @@ -835,5 +836,6 @@ fn import_directive_subclass_to_string(subclass: &ImportDirectiveSubclass) -> St
SingleImport { source, .. } => source.to_string(),
GlobImport { .. } => "*".to_string(),
ExternCrate => "<extern crate>".to_string(),
MacroUse => "#[macro_use]".to_string(),
}
}
2 changes: 1 addition & 1 deletion src/libserialize/lib.rs
Expand Up @@ -39,7 +39,7 @@ Core encoding and decoding interfaces.

// test harness access
#[cfg(test)] extern crate test;
#[macro_use] extern crate log;
extern crate log;

extern crate std_unicode;
extern crate collections;
Expand Down
21 changes: 21 additions & 0 deletions src/test/compile-fail/imports/unused-macro-use.rs
@@ -0,0 +1,21 @@
// Copyright 2016 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.

#![deny(unused)]

#[macro_use] //~ ERROR unused `#[macro_use]` import
extern crate core;

#[macro_use(
panic //~ ERROR unused `#[macro_use]` import
)]
extern crate core as core_2;

fn main() {}
2 changes: 0 additions & 2 deletions src/test/compile-fail/lint-unused-extern-crate.rs
Expand Up @@ -26,8 +26,6 @@ extern crate rand; // no error, the use marks it as used

extern crate lint_unused_extern_crate as other; // no error, the use * marks it as used

#[macro_use] extern crate core; // no error, the `#[macro_use]` marks it as used

#[allow(unused_imports)]
use rand::isaac::IsaacRng;

Expand Down
2 changes: 1 addition & 1 deletion src/tools/cargotest/main.rs
Expand Up @@ -25,7 +25,7 @@ const TEST_REPOS: &'static [Test] = &[
Test {
name: "cargo",
repo: "https://github.com/rust-lang/cargo",
sha: "b7be4f2ef2cf743492edc6dfb55d087ed88f2d76",
sha: "2324c2bbaf7fc6ea9cbdd77c034ef1af769cb617",
lock: None,
},
Test {
Expand Down

0 comments on commit 356fa2c

Please sign in to comment.