rust-lang / rust-lang/rust

Inappropriate `unused_parens` for inline `const` inside `vec!`/macros

Open
#126,457 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lints A-macros C-bug D-incorrect F-inline_const L-unused_parens S-has-mcve T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided playground reproducer for editions 2021 and 2024, comparing the inline const cases inside vec!. Trace how the unused_parens lint interacts with vec! macro expansion and inline const parsing. Done means the required parentheses are not reported as unnecessary while genuinely unnecessary parentheses still receive the lint.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.