提交 356fa2c5 编写于 作者: J Jeffrey Seyfried

Warn on unused `#[macro_use]` imports.

上级 2efec3c1
......@@ -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;
......
......@@ -57,7 +57,7 @@
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
......
......@@ -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;
......
......@@ -57,7 +57,6 @@
extern crate rustc_llvm as llvm;
#[macro_use]
extern crate log;
#[macro_use]
extern crate syntax;
extern crate syntax_ext;
extern crate syntax_pos;
......
......@@ -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
......
......@@ -30,7 +30,7 @@
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;
......
......@@ -37,7 +37,6 @@
#![feature(slice_patterns)]
#![feature(staged_api)]
#[macro_use]
extern crate syntax;
#[macro_use]
extern crate rustc;
......
......@@ -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" }
......
......@@ -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;
......
......@@ -552,16 +552,35 @@ fn process_legacy_macro_imports(&mut self, item: &Item, module: Module<'a>, expa
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");
}
......
......@@ -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);
}
_ => {}
}
}
......
......@@ -341,12 +341,15 @@ pub fn resolve_legacy_scope(&mut self,
};
}
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 {
......@@ -378,6 +381,7 @@ pub fn finalize_current_module_macro_resolutions(&mut self) {
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
},
......
......@@ -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.
......@@ -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(),
}
}
......@@ -39,7 +39,7 @@
// test harness access
#[cfg(test)] extern crate test;
#[macro_use] extern crate log;
extern crate log;
extern crate std_unicode;
extern crate collections;
......
// 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() {}
......@@ -26,8 +26,6 @@
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;
......
......@@ -25,7 +25,7 @@ struct Test {
Test {
name: "cargo",
repo: "https://github.com/rust-lang/cargo",
sha: "b7be4f2ef2cf743492edc6dfb55d087ed88f2d76",
sha: "2324c2bbaf7fc6ea9cbdd77c034ef1af769cb617",
lock: None,
},
Test {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册