rust-lang / rust-analyzer

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

Feature request: simplify match expression #10443

Open Nadrieril opened 3 years ago

Nadrieril commented 3 years ago

When I'm refactoring and inlining stuff all over the place, I often end up with cases like this:

match Some(foo()) {
  None => { do_none() }
  Some(0) => { do_0() }
  Some(1) => { do_1() }
  _ => { do_wild() }
}

I would love an assist that keeps only the Some branches and simplifies this to

match foo() {
  0 => { do_0() }
  1 => { do_1() }
  _ => { do_wild() }
}

It would also make sense for if lets and even plain ifs.

I realize this is unlikely to be actionable until match checking is librarified, but that's not too far in the future hopefully.

flodiebold commented 3 years ago

It is actionable, we have working match checking already, it's just not shared with rustc yet. (This is a bit more complicated than match checking though, I think, since match checking only takes into account the discriminant's type, not its actual value.)

This should probably even be a (hint) diagnostic.

Nadrieril commented 3 years ago

You do have match checking, but afaik it's copy-pasted from the rustc code so you're not going to modify it, right? Or do you also have your own implementation?

Veykril commented 3 years ago

Our implementation is mainly copied from rustc yes, see https://github.com/rust-analyzer/rust-analyzer/pull/8717 that updated the implementation.

Nadrieril commented 3 years ago

Oh I see, you are essentially maintaining some patches over it, so the feature would be doable. Cool!

FYI I'm moving stuff around quite a bit on the rustc side of this. I hope to move the core of the logic to a separate crate in the not-too-far future. So you may want to wait.

Nadrieril commented 8 months ago

Now that we've librarified match checking, would anyone be keen to work on this? I can help with the pattern logic, I'd just rather not have to learn the rust-analyzer side of things :angel: