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_parens false positive on braced macro calls #78747

Closed
KamilaBorowska opened this issue Nov 4, 2020 · 3 comments
Closed

unused_parens false positive on braced macro calls #78747

KamilaBorowska opened this issue Nov 4, 2020 · 3 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another.

Comments

@KamilaBorowska
Copy link
Contributor

KamilaBorowska commented Nov 4, 2020

I tried this code:

macro_rules! x {
    {} => { "q" };
}

fn f() -> String {
    (x! {}.to_string())
}

fn main() {}

I expected to see this happen: No warning about unnecessary parens

Instead, this happened: A warning about unnecessary parens suggesting to remove parens. Attempting to actually follow the advice causes an error.

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

This can be worked around by using x!().to_string or (x! {}).to_string() instead, however this shouldn't be necessary as the original code is fine.

This happens since Rust 1.42.

Meta

rustc --version --verbose:

rustc 1.47.0 (18bf6b4f0 2020-10-07)
binary: rustc
commit-hash: 18bf6b4f01a6feaf7259ba7cdae58031af1b7b39
commit-date: 2020-10-07
host: x86_64-unknown-linux-gnu
release: 1.47.0
LLVM version: 11.0
@KamilaBorowska KamilaBorowska added the C-bug Category: This is a bug. label Nov 4, 2020
@jyn514 jyn514 added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) regression-from-stable-to-stable Performance or correctness regression from one stable version to another. labels Nov 4, 2020
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Nov 4, 2020
@apiraino
Copy link
Contributor

apiraino commented Nov 5, 2020

Assigning P-medium as discussed as part of the Prioritization Working Group procedure and removing I-prioritize.

@apiraino apiraino added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Nov 5, 2020
@Aaron1011 Aaron1011 added A-proc-macros Area: Procedural macros and removed A-proc-macros Area: Procedural macros labels Nov 5, 2020
@Aaron1011
Copy link
Member

Note that the parenthesis are required due to the fact that we have special handling for macro invocations with curly braces:

let kind = if delim == token::Brace || self.token == token::Semi || self.token == token::Eof
{
StmtKind::MacCall(P(MacCallStmt { mac, style, attrs }))
} else {

Without the parenthesis, we parse x!{} as a statement, not an expression. However, x!().to_string() (without any surrounding parenthesis) compiles.

@kawadakk
Copy link
Contributor

kawadakk commented Dec 20, 2021

The suggestion is valid as of #88690 (included in 1.57.0).

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. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another.
Projects
None yet
Development

No branches or pull requests

6 participants