Skip to content

Commit 6f53eaa

Browse files
committed
Warn on unused #[macro_use] imports.
1 parent bcf3418 commit 6f53eaa

File tree

16 files changed

+69
-23
lines changed

16 files changed

+69
-23
lines changed

src/libproc_macro_tokens/lib.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,16 +549,35 @@ impl<'a> Resolver<'a> {
549549
used = true; // Avoid the normal unused extern crate warning
550550
}
551551

552+
let (graph_root, arenas) = (self.graph_root, self.arenas);
553+
let macro_use_directive = |span| arenas.alloc_import_directive(ImportDirective {
554+
id: item.id,
555+
parent: graph_root,
556+
imported_module: Cell::new(Some(module)),
557+
subclass: ImportDirectiveSubclass::MacroUse,
558+
span: span,
559+
module_path: Vec::new(),
560+
vis: Cell::new(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))),
561+
expansion: expansion,
562+
used: Cell::new(false),
563+
});
564+
552565
if let Some(span) = legacy_imports.import_all {
566+
let directive = macro_use_directive(span);
567+
self.potentially_unused_imports.push(directive);
553568
module.for_each_child(|ident, ns, binding| if ns == MacroNS {
554-
self.legacy_import_macro(ident.name, binding, span, allow_shadowing);
569+
let imported_binding = self.import(binding, directive);
570+
self.legacy_import_macro(ident.name, imported_binding, span, allow_shadowing);
555571
});
556572
} else {
557573
for (name, span) in legacy_imports.imports {
558574
let ident = Ident::with_empty_ctxt(name);
559575
let result = self.resolve_ident_in_module(module, ident, MacroNS, false, None);
560576
if let Ok(binding) = result {
561-
self.legacy_import_macro(name, binding, span, allow_shadowing);
577+
let directive = macro_use_directive(span);
578+
self.potentially_unused_imports.push(directive);
579+
let imported_binding = self.import(binding, directive);
580+
self.legacy_import_macro(name, imported_binding, span, allow_shadowing);
562581
} else {
563582
span_err!(self.session, span, E0469, "imported macro not found");
564583
}

src/librustc_resolve/check_unused.rs

Lines changed: 5 additions & 0 deletions
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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,15 @@ impl<'a> Resolver<'a> {
337337
};
338338
}
339339

340-
let binding = match binding {
341-
Some(binding) => MacroBinding::Legacy(binding),
342-
None => match self.builtin_macros.get(&name).cloned() {
343-
Some(binding) => MacroBinding::Modern(binding),
344-
None => return None,
345-
},
340+
let binding = if let Some(binding) = binding {
341+
MacroBinding::Legacy(binding)
342+
} else if let Some(binding) = self.builtin_macros.get(&name).cloned() {
343+
if !self.use_extern_macros {
344+
self.record_use(Ident::with_empty_ctxt(name), MacroNS, binding, DUMMY_SP);
345+
}
346+
MacroBinding::Modern(binding)
347+
} else {
348+
return None;
346349
};
347350

348351
if !self.use_extern_macros {
@@ -373,6 +376,10 @@ impl<'a> Resolver<'a> {
373376
let resolution = self.resolve_lexical_macro_path_segment(ident, MacroNS, Some(span));
374377
let (legacy_resolution, resolution) = match (legacy_resolution, resolution) {
375378
(Some(legacy_resolution), Ok(resolution)) => (legacy_resolution, resolution),
379+
(Some(MacroBinding::Modern(binding)), Err(..)) => {
380+
self.record_use(ident, MacroNS, binding, span);
381+
continue
382+
}
376383
_ => continue,
377384
};
378385
let (legacy_span, participle) = match legacy_resolution {

src/librustc_resolve/resolve_imports.rs

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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;
Lines changed: 21 additions & 0 deletions
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

Lines changed: 0 additions & 2 deletions
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

0 commit comments

Comments
 (0)