Skip to content

unused_parens false positive on braced macro calls #78747

Closed

Description

@sugar700

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

Activity

  1. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
    on Nov 4, 2020
  2. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Nov 4, 2020
  3. apiraino commented on Nov 5, 2020

    @apiraino
    Contributor

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

  4. added and removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Nov 5, 2020
  5. added and removed on Nov 5, 2020
  6. Aaron1011 commented on Nov 7, 2020

    @Aaron1011
    Contributor

    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.

  7. kawadakk commented on Dec 20, 2021

    @kawadakk
    Contributor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.P-mediumMedium priorityregression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions