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 warns macros unused if declaration is after usage #104221

Closed
eli-front opened this issue Nov 10, 2022 · 2 comments
Closed

rustc warns macros unused if declaration is after usage #104221

eli-front opened this issue Nov 10, 2022 · 2 comments
Labels
C-bug Category: This is a bug.

Comments

@eli-front
Copy link

I tried this code:

fn main() {
    print!("hi");
}

macro_rules! print {
    ($t: expr) => {
        println!("{}", $t);
    };
}

I expected to see this happen: no errors/warnings

Instead, this happened: unused macro definition: print
#[warn(unused_macros)] on by default. NOTE: this only occurs when the macro is defined after usage.

Meta

rustc --version --verbose:

binary: rustc
commit-hash: 4b91a6ea7258a947e59c6522cd5898e7c0a6a88f
commit-date: 2022-08-08
host: aarch64-apple-darwin
release: 1.63.0
LLVM version: 14.0.5
Backtrace

 --> src/main.rs:5:14
  |
5 | macro_rules! print {
  |              ^^^^^
  |
  = note: `#[warn(unused_macros)]` on by default

warning: `learn_test` (bin "learn_test") generated 1 warning

@eli-front eli-front added the C-bug Category: This is a bug. label Nov 10, 2022
@eli-front
Copy link
Author

Related to #96150

@sfackler
Copy link
Member

That definition is unused. Your print! call in main is invoking https://doc.rust-lang.org/stable/std/macro.print.html. Unlike types, functions, etc, macros are only available lexically after they have been defined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants