Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc: Lint against #[macro_use] in 2018 idioms #52275

Merged
merged 1 commit into from
Jul 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,13 @@ declare_lint! {
"detects proc macro derives using inaccessible names from parent modules"
}

declare_lint! {
pub MACRO_USE_EXTERN_CRATE,
Allow,
"the `#[macro_use]` attribute is now deprecated in favor of using macros \
via the module system"
}

/// Does nothing as a lint pass, but registers some `Lint`s
/// which are used by other parts of the compiler.
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -379,6 +386,7 @@ impl LintPass for HardwiredLints {
INTRA_DOC_LINK_RESOLUTION_FAILURE,
WHERE_CLAUSES_OBJECT_SAFETY,
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
MACRO_USE_EXTERN_CRATE,
)
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_lint/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ extern crate syntax_pos;
use rustc::lint;
use rustc::lint::{LateContext, LateLintPass, LintPass, LintArray};
use rustc::lint::builtin::{BARE_TRAIT_OBJECTS, ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE};
use rustc::lint::builtin::MACRO_USE_EXTERN_CRATE;
use rustc::session;
use rustc::util;
use rustc::hir;
Expand Down Expand Up @@ -179,6 +180,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
BARE_TRAIT_OBJECTS,
UNREACHABLE_PUB,
UNUSED_EXTERN_CRATES,
MACRO_USE_EXTERN_CRATE,
ELLIPSIS_INCLUSIVE_RANGE_PATTERNS);

// Guidelines for creating a future incompatibility lint:
Expand Down
17 changes: 16 additions & 1 deletion src/librustc_resolve/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,22 @@ pub fn check_crate(resolver: &mut Resolver, krate: &ast::Crate) {
match directive.subclass {
_ if directive.used.get() ||
directive.vis.get() == ty::Visibility::Public ||
directive.span.is_dummy() => {}
directive.span.is_dummy() => {
if let ImportDirectiveSubclass::MacroUse = directive.subclass {
if resolver.session.features_untracked().use_extern_macros &&
!directive.span.is_dummy() {
resolver.session.buffer_lint(
lint::builtin::MACRO_USE_EXTERN_CRATE,
directive.id,
directive.span,
"deprecated `#[macro_use]` directive used to \
import macros should be replaced at use sites \
with a `use` statement to import the macro \
instead",
);
}
}
}
ImportDirectiveSubclass::ExternCrate(_) => {
resolver.maybe_unused_extern_crates.push((directive.id, directive.span));
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/rust-2018/auxiliary/macro-use-warned-against.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2012-2014 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.

#[macro_export]
macro_rules! foo { () => () }
10 changes: 10 additions & 0 deletions src/test/ui/rust-2018/auxiliary/macro-use-warned-against2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2018 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.

25 changes: 25 additions & 0 deletions src/test/ui/rust-2018/macro-use-warned-against.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2018 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.

// aux-build:macro-use-warned-against.rs
// aux-build:macro-use-warned-against2.rs
// compile-pass

#![warn(rust_2018_idioms, unused)]
#![feature(use_extern_macros)]

#[macro_use] //~ WARN should be replaced at use sites with a `use` statement
extern crate macro_use_warned_against;
#[macro_use] //~ WARN unused `#[macro_use]`
extern crate macro_use_warned_against2;

fn main() {
foo!();
}
26 changes: 26 additions & 0 deletions src/test/ui/rust-2018/macro-use-warned-against.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
warning: deprecated `#[macro_use]` directive used to import macros should be replaced at use sites with a `use` statement to import the macro instead
--> $DIR/macro-use-warned-against.rs:18:1
|
LL | #[macro_use] //~ WARN should be replaced at use sites with a `use` statement
| ^^^^^^^^^^^^
|
note: lint level defined here
--> $DIR/macro-use-warned-against.rs:15:9
|
LL | #![warn(rust_2018_idioms, unused)]
| ^^^^^^^^^^^^^^^^
= note: #[warn(macro_use_extern_crate)] implied by #[warn(rust_2018_idioms)]

warning: unused `#[macro_use]` import
--> $DIR/macro-use-warned-against.rs:20:1
|
LL | #[macro_use] //~ WARN unused `#[macro_use]`
| ^^^^^^^^^^^^
|
note: lint level defined here
--> $DIR/macro-use-warned-against.rs:15:27
|
LL | #![warn(rust_2018_idioms, unused)]
| ^^^^^^
= note: #[warn(unused_imports)] implied by #[warn(unused)]