Skip to content

Commit 356fa2c

Browse files
committed
Warn on unused #[macro_use] imports.
1 parent 2efec3c commit 356fa2c

File tree

17 files changed

+67
-24
lines changed

17 files changed

+67
-24
lines changed

src/libproc_macro_tokens/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
extern crate syntax;
6161
extern crate syntax_pos;
62-
#[macro_use] extern crate log;
62+
extern crate log;
6363

6464
pub mod build;
6565
pub mod parse;

src/librustc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ extern crate rustc_const_math;
5757
extern crate rustc_errors as errors;
5858
#[macro_use] extern crate log;
5959
#[macro_use] extern crate syntax;
60-
#[macro_use] extern crate syntax_pos;
60+
extern crate syntax_pos;
6161
#[macro_use] #[no_link] extern crate rustc_bitflags;
6262

6363
extern crate serialize as rustc_serialize; // used by deriving

src/librustc_const_math/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
#![feature(const_fn)]
2929
#![cfg_attr(not(stage0), feature(i128))]
3030

31-
#[macro_use] extern crate log;
32-
#[macro_use] extern crate syntax;
31+
extern crate log;
32+
extern crate syntax;
3333

3434
// SNAP: remove use of this crate
3535
extern crate rustc_i128;

src/librustc_driver/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ extern crate serialize;
5757
extern crate rustc_llvm as llvm;
5858
#[macro_use]
5959
extern crate log;
60-
#[macro_use]
6160
extern crate syntax;
6261
extern crate syntax_ext;
6362
extern crate syntax_pos;

src/librustc_errors/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727

2828
extern crate serialize;
2929
extern crate term;
30-
#[macro_use]
3130
extern crate log;
32-
#[macro_use]
3331
extern crate libc;
3432
extern crate std_unicode;
3533
extern crate serialize as rustc_serialize; // used by deriving

src/librustc_incremental/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extern crate rustc_data_structures;
3030
extern crate serialize as rustc_serialize;
3131

3232
#[macro_use] extern crate log;
33-
#[macro_use] extern crate syntax;
33+
extern crate syntax;
3434
extern crate syntax_pos;
3535

3636
extern crate rustc_i128;

src/librustc_lint/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#![feature(slice_patterns)]
3838
#![feature(staged_api)]
3939

40-
#[macro_use]
4140
extern crate syntax;
4241
#[macro_use]
4342
extern crate rustc;

src/librustc_plugin/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ crate-type = ["dylib"]
1313
log = { path = "../liblog" }
1414
rustc = { path = "../librustc" }
1515
rustc_back = { path = "../librustc_back" }
16-
rustc_bitflags = { path = "../librustc_bitflags" }
1716
rustc_metadata = { path = "../librustc_metadata" }
1817
syntax = { path = "../libsyntax" }
1918
syntax_pos = { path = "../libsyntax_pos" }

src/librustc_plugin/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@
6363
#![feature(rustc_diagnostic_macros)]
6464
#![feature(rustc_private)]
6565

66-
#[macro_use] extern crate log;
66+
extern crate log;
6767
#[macro_use] extern crate syntax;
68-
#[macro_use] #[no_link] extern crate rustc_bitflags;
6968

7069
extern crate rustc;
7170
extern crate rustc_back;

src/librustc_resolve/build_reduced_graph.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -552,16 +552,35 @@ impl<'a> Resolver<'a> {
552552
used = true; // Avoid the normal unused extern crate warning
553553
}
554554

555+
let (graph_root, arenas) = (self.graph_root, self.arenas);
556+
let macro_use_directive = |span| arenas.alloc_import_directive(ImportDirective {
557+
id: item.id,
558+
parent: graph_root,
559+
imported_module: Cell::new(Some(module)),
560+
subclass: ImportDirectiveSubclass::MacroUse,
561+
span: span,
562+
module_path: Vec::new(),
563+
vis: Cell::new(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))),
564+
expansion: expansion,
565+
used: Cell::new(false),
566+
});
567+
555568
if let Some(span) = legacy_imports.import_all {
569+
let directive = macro_use_directive(span);
570+
self.potentially_unused_imports.push(directive);
556571
module.for_each_child(|ident, ns, binding| if ns == MacroNS {
557-
self.legacy_import_macro(ident.name, binding, span, allow_shadowing);
572+
let imported_binding = self.import(binding, directive);
573+
self.legacy_import_macro(ident.name, imported_binding, span, allow_shadowing);
558574
});
559575
} else {
560576
for (name, span) in legacy_imports.imports {
561577
let ident = Ident::with_empty_ctxt(name);
562578
let result = self.resolve_ident_in_module(module, ident, MacroNS, false, None);
563579
if let Ok(binding) = result {
564-
self.legacy_import_macro(name, binding, span, allow_shadowing);
580+
let directive = macro_use_directive(span);
581+
self.potentially_unused_imports.push(directive);
582+
let imported_binding = self.import(binding, directive);
583+
self.legacy_import_macro(name, imported_binding, span, allow_shadowing);
565584
} else {
566585
span_err!(self.session, span, E0469, "imported macro not found");
567586
}

src/librustc_resolve/check_unused.rs

+5
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ pub fn check_crate(resolver: &mut Resolver, krate: &ast::Crate) {
125125
let msg = "unused extern crate".to_string();
126126
resolver.session.add_lint(lint, directive.id, directive.span, msg);
127127
}
128+
ImportDirectiveSubclass::MacroUse => {
129+
let lint = lint::builtin::UNUSED_IMPORTS;
130+
let msg = "unused `#[macro_use]` import".to_string();
131+
resolver.session.add_lint(lint, directive.id, directive.span, msg);
132+
}
128133
_ => {}
129134
}
130135
}

src/librustc_resolve/macros.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,15 @@ impl<'a> Resolver<'a> {
341341
};
342342
}
343343

344-
let binding = match binding {
345-
Some(binding) => MacroBinding::Legacy(binding),
346-
None => match self.builtin_macros.get(&name).cloned() {
347-
Some(binding) => MacroBinding::Modern(binding),
348-
None => return None,
349-
},
344+
let binding = if let Some(binding) = binding {
345+
MacroBinding::Legacy(binding)
346+
} else if let Some(binding) = self.builtin_macros.get(&name).cloned() {
347+
if !self.use_extern_macros {
348+
self.record_use(Ident::with_empty_ctxt(name), MacroNS, binding, DUMMY_SP);
349+
}
350+
MacroBinding::Modern(binding)
351+
} else {
352+
return None;
350353
};
351354

352355
if !self.use_extern_macros {
@@ -378,6 +381,7 @@ impl<'a> Resolver<'a> {
378381
let (legacy_resolution, resolution) = match (legacy_resolution, resolution) {
379382
(Some(legacy_resolution), Ok(resolution)) => (legacy_resolution, resolution),
380383
(Some(MacroBinding::Modern(binding)), Err(_)) => {
384+
self.record_use(ident, MacroNS, binding, span);
381385
self.err_if_macro_use_proc_macro(ident.name, span, binding);
382386
continue
383387
},

src/librustc_resolve/resolve_imports.rs

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub enum ImportDirectiveSubclass<'a> {
4949
// n.b. `max_vis` is only used in `finalize_import` to check for reexport errors.
5050
},
5151
ExternCrate,
52+
MacroUse,
5253
}
5354

5455
/// One import directive.
@@ -835,5 +836,6 @@ fn import_directive_subclass_to_string(subclass: &ImportDirectiveSubclass) -> St
835836
SingleImport { source, .. } => source.to_string(),
836837
GlobImport { .. } => "*".to_string(),
837838
ExternCrate => "<extern crate>".to_string(),
839+
MacroUse => "#[macro_use]".to_string(),
838840
}
839841
}

src/libserialize/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Core encoding and decoding interfaces.
3939

4040
// test harness access
4141
#[cfg(test)] extern crate test;
42-
#[macro_use] extern crate log;
42+
extern crate log;
4343

4444
extern crate std_unicode;
4545
extern crate collections;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
#![deny(unused)]
12+
13+
#[macro_use] //~ ERROR unused `#[macro_use]` import
14+
extern crate core;
15+
16+
#[macro_use(
17+
panic //~ ERROR unused `#[macro_use]` import
18+
)]
19+
extern crate core as core_2;
20+
21+
fn main() {}

src/test/compile-fail/lint-unused-extern-crate.rs

-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ extern crate rand; // no error, the use marks it as used
2626

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

29-
#[macro_use] extern crate core; // no error, the `#[macro_use]` marks it as used
30-
3129
#[allow(unused_imports)]
3230
use rand::isaac::IsaacRng;
3331

src/tools/cargotest/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const TEST_REPOS: &'static [Test] = &[
2525
Test {
2626
name: "cargo",
2727
repo: "https://github.com/rust-lang/cargo",
28-
sha: "b7be4f2ef2cf743492edc6dfb55d087ed88f2d76",
28+
sha: "2324c2bbaf7fc6ea9cbdd77c034ef1af769cb617",
2929
lock: None,
3030
},
3131
Test {

0 commit comments

Comments
 (0)