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

unused_braces false positive for macro expression. #88104

Open
ehuss opened this issue Aug 17, 2021 · 1 comment
Open

unused_braces false positive for macro expression. #88104

ehuss opened this issue Aug 17, 2021 · 1 comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug.

Comments

@ehuss
Copy link
Contributor

ehuss commented Aug 17, 2021

I tried this code:

macro_rules! foo(
    ($imp: expr) => {
        pub fn f() -> i32 {
            $imp
            1
        }
    }
);

foo!(
    { println!("hi") }
);

fn main() {}

I expected to see this happen: No warning.

Instead, this happened: unused_braces warning with a suggestion that causes the code to fail to compile.

warning: unnecessary braces around block return value
  --> src/main.rs:11:5
   |
11 |     { println!("hi") }
   |     ^^^^^^^^^^^^^^^^^^ help: remove these braces
   |
   = note: `#[warn(unused_braces)]` on by default

After applying the suggestion:

error: expected `;`, found `1`
  --> src/main.rs:4:17
   |
4  |               $imp
   |                   ^ help: add `;` here
5  |               1
   |               - unexpected token
...
10 | / foo!(
11 | |     println!("hi")
12 | | );
   | |__- in this macro invocation
   |
   = note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)

Found when trying to run cargo fix on nphysics2d 0.24.0.

Also found on https://github.com/pjohansson/grafen.

Meta

rustc --version --verbose:

rustc 1.56.0-nightly (0035d9dce 2021-08-16)
binary: rustc
commit-hash: 0035d9dcecee49d1f7349932bfa52c05a6f83641
commit-date: 2021-08-16
host: x86_64-apple-darwin
release: 1.56.0-nightly
LLVM version: 12.0.1
@bugadani
Copy link
Contributor

This issue also triggers when the matcher is a tt, but only if the expression is in the same line as the braces. I.e. the following triggers the warning:

macro_rules! trigger_warning {
    ($tokens:tt) => {{ $tokens }}
}

pub fn main() {
    trigger_warning!({ println!("hello") });
}

but this does not:

macro_rules! trigger_warning {
    ($tokens:tt) => {{ $tokens }}
}

pub fn main() {
    trigger_warning!({
        println!("hello") });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants