Skip to content

Inappropriate unused_parens for inline const inside vec!/macros #126457

Open

Description

https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/Weird.20interaction.20between.20vec!.5B.5D.20macro.20and.20inline.20const

In this example, the inline const must be wrapped in parentheses, in order to work inside vec! under editions <2024:

fn main() {
    // Works fine, no warning.
    let _: [Vec<String>; 10] = [const { vec![] }; 10];
    // Works fine, no warning.
    let _: Vec<Vec<String>> = vec![(const { vec![] })];

    // Works, but warns about unnecessary parentheses.
    let _: Vec<Vec<String>> = vec![(const { vec![] }); 10]; // <=== THIS

    // These fail on stable79/beta80/nightly81 with edition 2021.
    // They work on nightly81 with edition 2024.
    let _: Vec<Vec<String>> = vec![const { vec![] }];
    let _: Vec<Vec<String>> = vec![const { vec![] }; 10];
}

(playground)

However, notice that in the case of vec![(const { vec![] }); 10], the compiler accepts the code but warns that the parentheses are unnecessary:

warning: unnecessary parentheses around function argument
 --> src/main.rs:8:36
  |
8 |     let _: Vec<Vec<String>> = vec![(const { vec![] }); 10];
  |                                    ^                ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
8 -     let _: Vec<Vec<String>> = vec![(const { vec![] }); 10];
8 +     let _: Vec<Vec<String>> = vec![const { vec![] }; 10];
  |

The warning is incorrect, because removing the parentheses would result in an error under edition <2024.

(The vec![(const { vec![] })] case correctly reports no warning.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.Category: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.Diagnostics: A diagnostic that is giving misleading or incorrect information.F-inline_constInline constants (aka: const blocks, const expressions, anonymous constants)Inline constants (aka: const blocks, const expressions, anonymous constants)L-unused_parensLint: unused_parensLint: unused_parensS-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant 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