about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-07-13 03:40:33 +0000
committerbors <bors@rust-lang.org>2018-07-13 03:40:33 +0000
commit68c39b9fec17846005da9a8e42991422c08c377c (patch)
treeff3bab41b7bd920d7b603deb1d7ba147ac58e958
parente92e9ce0d8383ac0126467294575401c00e3b60a (diff)
parent0b969a9d68b29fa53fb0595beb319036210d9e73 (diff)
downloadrust-68c39b9fec17846005da9a8e42991422c08c377c.tar.gz
rust-68c39b9fec17846005da9a8e42991422c08c377c.zip
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
-rw-r--r--src/librustc/lint/builtin.rs8
-rw-r--r--src/librustc_lint/lib.rs2
-rw-r--r--src/librustc_resolve/check_unused.rs17
-rw-r--r--src/test/ui/rust-2018/auxiliary/macro-use-warned-against.rs12
-rw-r--r--src/test/ui/rust-2018/auxiliary/macro-use-warned-against2.rs10
-rw-r--r--src/test/ui/rust-2018/macro-use-warned-against.rs25
-rw-r--r--src/test/ui/rust-2018/macro-use-warned-against.stderr26
7 files changed, 99 insertions, 1 deletions
diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs
index efc2d9311c1..a46b3120622 100644
--- a/src/librustc/lint/builtin.rs
+++ b/src/librustc/lint/builtin.rs
@@ -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)]
@@ -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,
         )
     }
 }
diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs
index 359b056b5a2..5348a47eed6 100644
--- a/src/librustc_lint/lib.rs
+++ b/src/librustc_lint/lib.rs
@@ -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;
@@ -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:
diff --git a/src/librustc_resolve/check_unused.rs b/src/librustc_resolve/check_unused.rs
index 0c4b9a546cb..ef24a201e0f 100644
--- a/src/librustc_resolve/check_unused.rs
+++ b/src/librustc_resolve/check_unused.rs
@@ -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));
             }
diff --git a/src/test/ui/rust-2018/auxiliary/macro-use-warned-against.rs b/src/test/ui/rust-2018/auxiliary/macro-use-warned-against.rs
new file mode 100644
index 00000000000..9487fd808cd
--- /dev/null
+++ b/src/test/ui/rust-2018/auxiliary/macro-use-warned-against.rs
@@ -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 { () => () }
diff --git a/src/test/ui/rust-2018/auxiliary/macro-use-warned-against2.rs b/src/test/ui/rust-2018/auxiliary/macro-use-warned-against2.rs
new file mode 100644
index 00000000000..6391db85dc5
--- /dev/null
+++ b/src/test/ui/rust-2018/auxiliary/macro-use-warned-against2.rs
@@ -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.
+
diff --git a/src/test/ui/rust-2018/macro-use-warned-against.rs b/src/test/ui/rust-2018/macro-use-warned-against.rs
new file mode 100644
index 00000000000..f7a6b560280
--- /dev/null
+++ b/src/test/ui/rust-2018/macro-use-warned-against.rs
@@ -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!();
+}
diff --git a/src/test/ui/rust-2018/macro-use-warned-against.stderr b/src/test/ui/rust-2018/macro-use-warned-against.stderr
new file mode 100644
index 00000000000..bebad31510f
--- /dev/null
+++ b/src/test/ui/rust-2018/macro-use-warned-against.stderr
@@ -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)]
+