Skip to content

Incorrect diagnostic for if-let-guard + attempting to use a non-constant value in a constant #152831

@SpriteOvO

Description

@SpriteOvO

Code

if let Some(v) = Some(1) {
    const { v }
}

Current output

error[E0435]: attempt to use a non-constant value in a constant
 --> src/main.rs:3:13
  |
3 |     const { v }
  |             ^ non-constant value
  |
help: consider using `const` instead of `let`
  |
2 - if let Some(v) = Some(1) {
2 + if const v: /* Type */) = Some(1) {
  |

For more information about this error, try `rustc --explain E0435`.

Desired output

error[E0435]: attempt to use a non-constant value in a constant
 --> src/main.rs:3:13
  |
3 |     const { v }
  |             ^ non-constant value
  |

For more information about this error, try `rustc --explain E0435`.

Rationale and extra context

No response

Other cases

Rust Version

rustc 1.95.0-nightly (873b4beb0 2026-02-15)
binary: rustc
commit-hash: 873b4beb0cc726493b94c8ef21f68795c04fbbc1
commit-date: 2026-02-15
host: x86_64-unknown-linux-gnu
release: 1.95.0-nightly
LLVM version: 22.1.0

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions