rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.28k stars 1.61k forks source link

Feature: Assist to convert `match` to `let` #17773

Open kpreid opened 3 months ago

kpreid commented 3 months ago
struct Example {
    v: i32,
}

fn foo(e: Example) {
    match e {
        Example { v } => undefined(v),
    }
}

This match can be replaced with a let. It would be useful if rust-analyzer would offer to convert it. In this case, rustc does give an applicable suggestion, but that suggestion is only available when check has run and when the program is free of other errors, so it would be useful to have this refactor available even when the program does not compile, for use as an editing tool on the way to getting it to compile.

@rustbot label +A-assists

Veykril commented 3 months ago

As in, turning it into

fn foo() {
    let Example { v } = e;
    undefined(v)
}

?

kpreid commented 3 months ago

Yes, that's correct. That's what the compiler-generated suggestion would do, if it existed under these conditions. Related: It'd also be useful to be able to convert two-arm matches and if lets to let else (not just match to if let, as is currently offered).