From 5bbc240ffbb0dcf510fd73d71dae529bd345e6b2 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 15 May 2021 16:10:17 -0700 Subject: [PATCH] Fix unused attributes on macro_rules. --- compiler/rustc_lint/src/late.rs | 4 +++ compiler/rustc_target/src/asm/mod.rs | 3 -- src/test/ui/unused/unused-attr-macro-rules.rs | 34 +++++++++++++++++++ .../ui/unused/unused-attr-macro-rules.stderr | 32 +++++++++++++++++ 4 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/unused/unused-attr-macro-rules.rs create mode 100644 src/test/ui/unused/unused-attr-macro-rules.stderr diff --git a/compiler/rustc_lint/src/late.rs b/compiler/rustc_lint/src/late.rs index d325b5fe7f8c6..0a7ab4a2bafa3 100644 --- a/compiler/rustc_lint/src/late.rs +++ b/compiler/rustc_lint/src/late.rs @@ -448,6 +448,10 @@ fn late_lint_pass_crate<'tcx, T: LateLintPass<'tcx>>(tcx: TyCtxt<'tcx>, pass: T) lint_callback!(cx, check_crate, krate); hir_visit::walk_crate(cx, krate); + for attr in krate.non_exported_macro_attrs { + // This HIR ID is a lie, since the macro ID isn't available. + cx.visit_attribute(hir::CRATE_HIR_ID, attr); + } lint_callback!(cx, check_crate_post, krate); }) diff --git a/compiler/rustc_target/src/asm/mod.rs b/compiler/rustc_target/src/asm/mod.rs index f12debb5a3412..f4e4579bec27e 100644 --- a/compiler/rustc_target/src/asm/mod.rs +++ b/compiler/rustc_target/src/asm/mod.rs @@ -6,7 +6,6 @@ use rustc_span::Symbol; use std::fmt; use std::str::FromStr; -#[macro_use] macro_rules! def_reg_class { ($arch:ident $arch_regclass:ident { $( @@ -51,7 +50,6 @@ macro_rules! def_reg_class { } } -#[macro_use] macro_rules! def_regs { ($arch:ident $arch_reg:ident $arch_regclass:ident { $( @@ -129,7 +127,6 @@ macro_rules! def_regs { } } -#[macro_use] macro_rules! types { ( $(_ : $($ty:expr),+;)? diff --git a/src/test/ui/unused/unused-attr-macro-rules.rs b/src/test/ui/unused/unused-attr-macro-rules.rs new file mode 100644 index 0000000000000..396137a11d069 --- /dev/null +++ b/src/test/ui/unused/unused-attr-macro-rules.rs @@ -0,0 +1,34 @@ +#![deny(unused_attributes)] +// Unused attributes on macro_rules requires special handling since the +// macro_rules definition does not survive towards HIR. + +// A sample of various built-in attributes. +#[macro_export] +#[macro_use] //~ ERROR unused attribute +#[path="foo"] //~ ERROR unused attribute +#[recursion_limit="1"] //~ ERROR unused attribute + //~| ERROR crate-level attribute should be an inner attribute +macro_rules! foo { + () => {}; +} + +// The following should not warn about unused attributes. +#[allow(unused)] +macro_rules! foo2 { + () => {}; +} + +#[cfg(FALSE)] +macro_rules! foo { + () => {}; +} + +/// Some docs +#[deprecated] +#[doc = "more docs"] +#[macro_export] +macro_rules! bar { + () => {}; +} + +fn main() {} diff --git a/src/test/ui/unused/unused-attr-macro-rules.stderr b/src/test/ui/unused/unused-attr-macro-rules.stderr new file mode 100644 index 0000000000000..4606be01ac014 --- /dev/null +++ b/src/test/ui/unused/unused-attr-macro-rules.stderr @@ -0,0 +1,32 @@ +error: unused attribute + --> $DIR/unused-attr-macro-rules.rs:7:1 + | +LL | #[macro_use] + | ^^^^^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/unused-attr-macro-rules.rs:1:9 + | +LL | #![deny(unused_attributes)] + | ^^^^^^^^^^^^^^^^^ + +error: unused attribute + --> $DIR/unused-attr-macro-rules.rs:8:1 + | +LL | #[path="foo"] + | ^^^^^^^^^^^^^ + +error: unused attribute + --> $DIR/unused-attr-macro-rules.rs:9:1 + | +LL | #[recursion_limit="1"] + | ^^^^^^^^^^^^^^^^^^^^^^ + +error: crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]` + --> $DIR/unused-attr-macro-rules.rs:9:1 + | +LL | #[recursion_limit="1"] + | ^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 4 previous errors +