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

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

Open
Phlosioneer opened this issue Jul 11, 2023 · 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_parens Lint: unused_parens T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Phlosioneer
Copy link
Contributor

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.

@Phlosioneer Phlosioneer added the C-bug Category: This is a bug. label Jul 11, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 11, 2023
@Noratrieb Noratrieb 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 Jul 11, 2023
@Noratrieb
Copy link
Member

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).

@Noratrieb Noratrieb removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 11, 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-lint 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