Skip to content

unused_parens false positives and false negatives in code containing macro calls #119426

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

Open
dtolnay opened this issue Dec 30, 2023 · 0 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-unused_parens Lint: unused_parens T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Dec 30, 2023

Code

macro_rules! m1 {
    () => {
        1
    };
}

pub fn f1() -> u8 {
    // Lint says parens are not needed, but they are.
    (m1! {} + 1)
}

macro_rules! m2 {
    () => {
        loop { break 1; }
    };
}

pub fn f2() -> u8 {
    // Lint says parens are needed, but they are not.
    (m2!() + 1)
}

Current output

warning: unnecessary parentheses around block return value
 --> src/lib.rs:9:5
  |
9 |     (m1! {} + 1)
  |     ^          ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
9 -     (m1! {} + 1)
9 +     m1! {} + 1
  |

Desired output

warning: unnecessary parentheses around block return value
 --> src/lib.rs:20:5
   |
20 |     (m2!() + 1)
   |     ^         ^
   |
   = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
   |
20 -     (m2!() + 1)
20 +     m2!() + 1
   |

Rationale and extra context

The code suggested by rustc does not compile, because that pair of parentheses really is required.

error: leading `+` is not supported
 --> src/lib.rs:9:12
  |
9 |     m1! {} + 1
  |            ^ unexpected `+`
  |
help: try removing the `+`
  |
9 -     m1! {} + 1
9 +     m1! {}  1
  |

error[E0308]: mismatched types
 --> src/lib.rs:3:9
  |
3 |         1
  |         ^ expected `()`, found integer
...
9 |     m1! {} + 1
  |     ------ in this macro invocation
  |
  = note: this error originates in the macro `m1` (in Nightly builds, run with -Z macro-backtrace for more info)

Anything else?

I found a partly related issue #113563, in which @Nilstrieb writes:

I don't think it's easy to fix this. The lint works on the finalized AST and can't really take into account the details of how the macro was expanded. The easiest solution here is to just put an #[allow()] inside the macro for now (which you've probably done already).

However, I have filed this as a distinct issue because I am hopeful that the case in this issue might be easier to fix than the other one, because this one involves parentheses that come from outside the macro call, as opposed to parentheses that come from inside the macro definition.

@dtolnay dtolnay added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 30, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 30, 2023
@dtolnay dtolnay added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. and removed A-diagnostics Area: Messages for errors, warnings, and lints labels Dec 30, 2023
@Noratrieb Noratrieb added C-bug Category: This is a bug. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 30, 2023
@jieyouxu jieyouxu added the L-unused_parens Lint: unused_parens label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-unused_parens Lint: unused_parens 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