Skip to content

Wrong "unnecessary parentheses around function argument"-warning with macro in assignment and return positions #113563

Open
@Phlosioneer

Description

@Phlosioneer

I tried this code:

macro_rules! range_array {
    ( $( $num:tt ),* ) => {
        [ $( { let value = $num; value..=value } ),* ]
    };
}

fn foo(x: u8) -> u8 {
    x
}

fn main() {
    // This isn't allowed, need to use a token tree.
    //assert_eq!(range_array![1, foo(4), 5], [1..=1, 4..=4, 5..=5])

    // But this triggers an unused_paren warning.
    assert_eq!(range_array![1, (foo(4)), 5], [1..=1, 4..=4, 5..=5]);
}

I expected to see this happen: No warnings

Instead, this happened:

warning: unnecessary parentheses around assigned value
  --> src/main.rs:12:32
   |
12 |     assert_eq!(range_array![1, (foo(4)), 5], [1..=1, 4..=4, 5..=5]);
   |                                ^      ^
   |
   = note: `#[warn(unused_parens)]` on by default

This is an unexpected corner case of #47775 (assignment), and was not fixed by #47896. Assignment-expressions and block-return-expressions are affected:

$num; // OK
let value = $num; // Warn
let value = 0 + $num; // OK
let value = 0 + ($num); // OK
let value = { $num }; // Warn
let value = 0 + { $num }; // Warn
let value = { 0 + $num }; // OK
{ $num }; // Warn
0 + { $num }; // Warn

In my actual use case, I can't do 0 + $num. I don't think I have any idenity function available.

Meta

rustc --version --verbose:

rustc 1.72.0-nightly (83964c156 2023-07-08)
binary: rustc
commit-hash: 83964c156db1f444050a38b2498dbd0da6d5d503
commit-date: 2023-07-08
host: x86_64-pc-windows-msvc
release: 1.72.0-nightly
LLVM version: 16.0.5

I still have a 1.36 install of rust, and that showed the same warning, so I think this has been around for a while.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.L-unused_parensLint: unused_parensT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions