Skip to content

Commit 68c39b9

Browse files
committed
Auto merge of #52275 - alexcrichton:no-macro-use, r=nrc
rustc: Lint against `#[macro_use]` in 2018 idioms This commit adds a lint to the compiler to warn against the `#[macro_use]` directive as part of the `rust_2018_idioms` lint. This lint is turned off by default and is only enabled when the `use_extern_macros` feature is also enabled. The lint here isn't fully fleshed out as it's just a simple warning rather than suggestions of how to actually import the macro, but hopefully it's a good base to start from! cc #52043
2 parents e92e9ce + 0b969a9 commit 68c39b9

File tree

7 files changed

+99
-1
lines changed

7 files changed

+99
-1
lines changed

src/librustc/lint/builtin.rs

+8
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,13 @@ declare_lint! {
322322
"detects proc macro derives using inaccessible names from parent modules"
323323
}
324324

325+
declare_lint! {
326+
pub MACRO_USE_EXTERN_CRATE,
327+
Allow,
328+
"the `#[macro_use]` attribute is now deprecated in favor of using macros \
329+
via the module system"
330+
}
331+
325332
/// Does nothing as a lint pass, but registers some `Lint`s
326333
/// which are used by other parts of the compiler.
327334
#[derive(Copy, Clone)]
@@ -379,6 +386,7 @@ impl LintPass for HardwiredLints {
379386
INTRA_DOC_LINK_RESOLUTION_FAILURE,
380387
WHERE_CLAUSES_OBJECT_SAFETY,
381388
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
389+
MACRO_USE_EXTERN_CRATE,
382390
)
383391
}
384392
}

src/librustc_lint/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ extern crate syntax_pos;
4343
use rustc::lint;
4444
use rustc::lint::{LateContext, LateLintPass, LintPass, LintArray};
4545
use rustc::lint::builtin::{BARE_TRAIT_OBJECTS, ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE};
46+
use rustc::lint::builtin::MACRO_USE_EXTERN_CRATE;
4647
use rustc::session;
4748
use rustc::util;
4849
use rustc::hir;
@@ -179,6 +180,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
179180
BARE_TRAIT_OBJECTS,
180181
UNREACHABLE_PUB,
181182
UNUSED_EXTERN_CRATES,
183+
MACRO_USE_EXTERN_CRATE,
182184
ELLIPSIS_INCLUSIVE_RANGE_PATTERNS);
183185

184186
// Guidelines for creating a future incompatibility lint:

src/librustc_resolve/check_unused.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,22 @@ pub fn check_crate(resolver: &mut Resolver, krate: &ast::Crate) {
129129
match directive.subclass {
130130
_ if directive.used.get() ||
131131
directive.vis.get() == ty::Visibility::Public ||
132-
directive.span.is_dummy() => {}
132+
directive.span.is_dummy() => {
133+
if let ImportDirectiveSubclass::MacroUse = directive.subclass {
134+
if resolver.session.features_untracked().use_extern_macros &&
135+
!directive.span.is_dummy() {
136+
resolver.session.buffer_lint(
137+
lint::builtin::MACRO_USE_EXTERN_CRATE,
138+
directive.id,
139+
directive.span,
140+
"deprecated `#[macro_use]` directive used to \
141+
import macros should be replaced at use sites \
142+
with a `use` statement to import the macro \
143+
instead",
144+
);
145+
}
146+
}
147+
}
133148
ImportDirectiveSubclass::ExternCrate(_) => {
134149
resolver.maybe_unused_extern_crates.push((directive.id, directive.span));
135150
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2012-2014 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+
#[macro_export]
12+
macro_rules! foo { () => () }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2018 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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2018 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+
// aux-build:macro-use-warned-against.rs
12+
// aux-build:macro-use-warned-against2.rs
13+
// compile-pass
14+
15+
#![warn(rust_2018_idioms, unused)]
16+
#![feature(use_extern_macros)]
17+
18+
#[macro_use] //~ WARN should be replaced at use sites with a `use` statement
19+
extern crate macro_use_warned_against;
20+
#[macro_use] //~ WARN unused `#[macro_use]`
21+
extern crate macro_use_warned_against2;
22+
23+
fn main() {
24+
foo!();
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
warning: deprecated `#[macro_use]` directive used to import macros should be replaced at use sites with a `use` statement to import the macro instead
2+
--> $DIR/macro-use-warned-against.rs:18:1
3+
|
4+
LL | #[macro_use] //~ WARN should be replaced at use sites with a `use` statement
5+
| ^^^^^^^^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/macro-use-warned-against.rs:15:9
9+
|
10+
LL | #![warn(rust_2018_idioms, unused)]
11+
| ^^^^^^^^^^^^^^^^
12+
= note: #[warn(macro_use_extern_crate)] implied by #[warn(rust_2018_idioms)]
13+
14+
warning: unused `#[macro_use]` import
15+
--> $DIR/macro-use-warned-against.rs:20:1
16+
|
17+
LL | #[macro_use] //~ WARN unused `#[macro_use]`
18+
| ^^^^^^^^^^^^
19+
|
20+
note: lint level defined here
21+
--> $DIR/macro-use-warned-against.rs:15:27
22+
|
23+
LL | #![warn(rust_2018_idioms, unused)]
24+
| ^^^^^^
25+
= note: #[warn(unused_imports)] implied by #[warn(unused)]
26+

0 commit comments

Comments
 (0)