Skip to content

match_single_binding invents new experimental const = let = expr syntax in const ctx #15840

@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::match-single-binding

this code:

//@ check-pass

enum Foo {
    Prob,
}

const FOO: u32 = match Foo::Prob {
    Foo::Prob => 42,
};

const BAR: u32 = match Foo::Prob {
    x => 42,
};

impl Foo {
    pub const fn as_val(&self) -> u8 {
        use self::Foo::*;

        match *self {
            Prob => 0x1,
        }
    }
}

fn main() {}

caused the following diagnostics:

    Checking _single_variant_match_ice v0.1.0 (/tmp/icemaker_global_tempdir.paNtiJF6jVLU/icemaker_clippyfix_tempdir.a1tVzUcKTtrl/_single_variant_match_ice)
warning: this match could be written as a `let` statement
  --> src/main.rs:11:18
   |
11 |   const BAR: u32 = match Foo::Prob {
   |  __________________^
12 | |     x => 42,
13 | | };
   | |_^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_single_binding
   = note: requested on the command line with `--force-warn clippy::match-single-binding`
help: consider using a `let` statement
   |
11 ~ const BAR: u32 = let x = Foo::Prob;
12 ~ 42;
   |

warning: `_single_variant_match_ice` (bin "_single_variant_match_ice") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.25s

However after applying these diagnostics, the resulting code:

//@ check-pass

enum Foo {
    Prob,
}

const FOO: u32 = match Foo::Prob {
    Foo::Prob => 42,
};

const BAR: u32 = let x = Foo::Prob;
42;

impl Foo {
    pub const fn as_val(&self) -> u8 {
        use self::Foo::*;

        match *self {
            Prob => 0x1,
        }
    }
}

fn main() {}

no longer compiled:

    Checking _single_variant_match_ice v0.1.0 (/tmp/icemaker_global_tempdir.paNtiJF6jVLU/icemaker_clippyfix_tempdir.a1tVzUcKTtrl/_single_variant_match_ice)
error: expected expression, found `let` statement
  --> src/main.rs:11:18
   |
11 | const BAR: u32 = let x = Foo::Prob;
   |                  ^^^
   |
   = note: only supported directly in conditions of `if` and `while` expressions

error: expected item, found `42`
  --> src/main.rs:12:1
   |
12 | 42;
   | ^^ expected item
   |
   = note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>

error: could not compile `_single_variant_match_ice` (bin "_single_variant_match_ice" test) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `_single_variant_match_ice` (bin "_single_variant_match_ice") due to 2 previous errors

Version:

rustc 1.92.0-nightly (4a54b26d3 2025-10-07)
binary: rustc
commit-hash: 4a54b26d30dac43778afb0e503524b763fce0eee
commit-date: 2025-10-07
host: x86_64-unknown-linux-gnu
release: 1.92.0-nightly
LLVM version: 21.1.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions