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

False unused_braces lint warning when using non-proc macro #73068

Open
jeekobu opened this issue Jun 6, 2020 · 1 comment
Open

False unused_braces lint warning when using non-proc macro #73068

jeekobu opened this issue Jun 6, 2020 · 1 comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-unused_braces Lint: unused_braces T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jeekobu
Copy link

jeekobu commented Jun 6, 2020

I tried this code:

#![allow(dead_code)]
#[derive(Debug)]
struct Foo;
#[derive(Debug)]
struct Bar;
enum SomeWrapperEnum {
    Foo(Foo),
    Bar(Bar),
}

macro_rules! match_all {
    ($on:expr, $with:ident, $body:tt, $($var:ident),*) => {
        match $on {
            $(
                SomeWrapperEnum::$var($with) => { $body },
            )*
        }
    }
}

macro_rules! dispatch {
    ($on:expr, $with:ident, $body:tt) => {
        match_all!($on, $with, $body, Foo, Bar)
    }
}

fn main() {
    let quz = SomeWrapperEnum::Foo(Foo);
    dispatch!(quz, inner, { println!("{:?}", inner) } );
}

I expected to see this happen: No warnings, or applying hints from warnings improves the code.

Instead, this happened: The unused_braces lint fired, and applying the hint breaks the code.

The hint:

29 |     dispatch!(quz, inner, { println!("{:?}", inner) } );
   |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove these braces

If the braces are removed:

29 |     dispatch!(quz, inner, println!("{:?}", inner));
   |                                  ^ no rules expected this token in macro call

Note: Adding a semicolon after the println!() (and leaving the braces) results in no warning and the code still compiles.

I'm using stable, but reproduced on stable, debug, and nightly on play.rust-lang.org. The lint was added in #70081. I see #70717, but there are no explicit refs here. And I also see #70814, but these are not proc macros.

rustc --version --verbose:

rustc 1.44.0 (49cae5576 2020-06-01)
binary: rustc
commit-hash: 49cae55760da0a43428eba73abcb659bb70cf2e4
commit-date: 2020-06-01
host: x86_64-unknown-linux-gnu
release: 1.44.0
LLVM version: 9.0
@jeekobu jeekobu added the C-bug Category: This is a bug. label Jun 6, 2020
@lcnr
Copy link
Contributor

lcnr commented Jun 6, 2020

Also happens with unused_parens. Not sure how to fix this while still linting against

let _: Vec<i32> = vec![0; { 1 << 8 }];
let _: Vec<i32> = vec![0; (1 << 8)];

@LeSeulArtichaut LeSeulArtichaut added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 8, 2020
@jieyouxu jieyouxu added the L-unused_braces Lint: unused_braces label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-unused_braces Lint: unused_braces T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants