Skip to content

False positive of unused_braces when using methods inside of const-generic inside enum variant #145843

@datdenkikniet

Description

@datdenkikniet

Code

#![allow(dead_code)]
use core::num::NonZeroUsize;

const TEST: NonZeroUsize = NonZeroUsize::new(1).unwrap();

struct Test<const N: usize> {
    value: [u8; { TEST.get() }],
}

Current output

Compiling playground v0.0.1 (/playground)
warning: unnecessary braces around const expression
 --> src/lib.rs:7:17
  |
7 |     value: [u8; { TEST.get() }],
  |                 ^^          ^^
  |
  = note: `#[warn(unused_braces)]` on by default
help: remove these braces
  |
7 -     value: [u8; { TEST.get() }],
7 +     value: [u8; TEST.get()],
  |

warning: `playground` (lib) generated 1 warning (run `cargo fix --lib -p playground` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.76s

Desired output

Compiling playground v0.0.1 (/playground)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.59s

Rationale and extra context

Applying the suggestion results in a compilation error as it is no longer valid.

I've tried my best to find duplicates, but have not managed to find any.

Other cases

This is also broken in the latest nightly:

rustc 1.91.0-nightly (809200ec9 2025-08-24)
binary: rustc
commit-hash: 809200ec956983fce4ae178b87dada69f01d0820
commit-date: 2025-08-24
host: x86_64-unknown-linux-gnu
release: 1.91.0-nightly
LLVM version: 21.1.0

Rust Version

rustc 1.89.0 (29483883e 2025-08-04)
binary: rustc
commit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2
commit-date: 2025-08-04
host: x86_64-unknown-linux-gnu
release: 1.89.0
LLVM version: 20.1.7

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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