Skip to content

#[deny] in expansion of proc macro is ignored #53975

Closed
@japaric

Description

@japaric

Affects proc_macro_bang! and #[proc_macro_attr]

STR

// crate: bar
// proc-macro = true
extern crate proc_macro;
#[macro_use]
extern crate quote;

use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn bar(_: TokenStream, _: TokenStream) -> TokenStream {
    quote!(
        #[deny(dead_code)]
        fn bar() {}
    ).into()
}

#[proc_macro]
pub fn baz(_: TokenStream) -> TokenStream {
    quote!(
        #[deny(dead_code)]
        fn baz() {}
    ).into()
}
// crate: baz
// crate-type: rlib

extern crate bar;

use bar::{bar, baz};

#[bar]
const FOO: () = ();

baz!();

Compiling this code succeeds

$ cargo build && echo OK
OK
$ cargo expand | tail -n9
extern crate bar;

use bar::{bar, baz};

#[deny(dead_code)]
fn bar() {}

#[deny(dead_code)]
fn baz() {}

Compiling the expanded code results in two errors, as expected:

$ cargo build
cargo build
   Compiling foo v0.1.0 (file:///home/japaric/tmp/foo)
warning: unused imports: `bar`, `baz`
 --> src/lib.rs:3:11
  |
3 | use bar::{bar, baz};
  |           ^^^  ^^^
  |
  = note: #[warn(unused_imports)] on by default

error: function is never used: `bar`
 --> src/lib.rs:6:1
  |
6 | fn bar() {}
  | ^^^^^^^^
  |
note: lint level defined here
 --> src/lib.rs:5:8
  |
5 | #[deny(dead_code)]
  |        ^^^^^^^^^

error: function is never used: `baz`
 --> src/lib.rs:9:1
  |
9 | fn baz() {}
  | ^^^^^^^^
  |
note: lint level defined here
 --> src/lib.rs:8:8
  |
8 | #[deny(dead_code)]
  |        ^^^^^^^^^

error: aborting due to 2 previous errors

Meta

$ rustc -V
rustc 1.30.0-nightly (1c2e17f4e 2018-09-04)

The name of the expanded function is not a problem. Renaming them to e.g. foo and quux doesn't change the outcome.

cc @alexcrichton @petrochenkov apologies if this has already been reported. I didn't search too hard.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-decl-macros-1-2Area: Declarative macros 1.2A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions