Skip to content

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

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

Open
Zalathar opened this issue Jun 14, 2024 · 1 comment
Open

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

Zalathar opened this issue Jun 14, 2024 · 1 comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. F-inline_const Inline constants (aka: const blocks, const expressions, anonymous constants) L-unused_parens Lint: unused_parens S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Zalathar
Copy link
Contributor

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

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 14, 2024
@Zalathar
Copy link
Contributor Author

@rustbot label +A-diagnostics

@rustbot rustbot added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 14, 2024
@jieyouxu jieyouxu added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. F-inline_const Inline constants (aka: const blocks, const expressions, anonymous constants) S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue L-unused_parens Lint: unused_parens T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. and removed A-diagnostics Area: Messages for errors, warnings, and lints needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. F-inline_const Inline constants (aka: const blocks, const expressions, anonymous constants) L-unused_parens Lint: unused_parens S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue 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

3 participants