Skip to content

function-like procedural macros: no dead_code warning on created code (e.g. "function is never used" warning) #73556

Open
@sfmunoz

Description

@sfmunoz

Problem: I got no "function is never used" warning (dead_code) on code generated by function-like procedural macros

I dare say this is a bug but I'm fairly new to procedural macros and maybe I'm doing something wrong. After quite a lot of searching I was unable to figure out if that was really the case so I decided to open an issue.

Ref: https://doc.rust-lang.org/reference/procedural-macros.html

  • Cargo.toml:
$ cat Cargo.toml
[package]
name = "aux"
version = "0.1.0"
edition = "2018"

[lib]
proc-macro = true
  • src/lib.rs:
$ cat src/lib.rs
use proc_macro::TokenStream;
#[proc_macro]
pub fn make_dummy(_item: TokenStream) -> TokenStream {
    "fn dummy() {}".parse().unwrap()
}
  • src/main.rs:
$ cat src/main.rs
aux::make_dummy!();
fn dummy2() {}
fn main() {
    println!("hello");
}

Expected: with the previous code I expected two function is never used warnings:

  • The first one for the generated fn dummy() {} code
  • The second one for fn dummy2() {} code

Instead: I just got a warning on dummy2() but nothing on dummy():

$ cargo build
   Compiling aux v0.1.0 (/var/tmp/aux)
warning: function is never used: `dummy2`
 --> src/main.rs:2:4
  |
2 | fn dummy2() {}
  |    ^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.23s

Expanded code: both dummy() and dummy2() are there, but there's only a build warning on dummy2():

$ cargo expand --bin aux
    Checking aux v0.1.0 (/var/tmp/aux)
    Finished check [unoptimized + debuginfo] target(s) in 0.06s

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
fn dummy() {}
fn dummy2() {}
fn main() {
    {
        ::std::io::_print(::core::fmt::Arguments::new_v1(
            &["hello\n"],
            &match () {
                () => [],
            },
        ));
    };
}

rustc version: I checked with the following versions of the compiler with same result:

  • rustc 1.44.1:
$ rustc --version --verbose
rustc 1.44.1 (c7087fe00 2020-06-17)
binary: rustc
commit-hash: c7087fe00d2ba919df1d813c040a5d47e43b0fe7
commit-date: 2020-06-17
host: x86_64-unknown-linux-gnu
release: 1.44.1
LLVM version: 9.0
  • rustc 1.46.0-nightly:
$ rustc --version --verbose
rustc 1.46.0-nightly (feb3536eb 2020-06-09)
binary: rustc
commit-hash: feb3536eba10c2e4585d066629598f03d5ddc7c6
commit-date: 2020-06-09
host: x86_64-unknown-linux-gnu
release: 1.46.0-nightly
LLVM version: 10.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-proc-macrosArea: Procedural macrosC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions