rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

Assist to turn `match` into `matches!` invocation

Open Veykril opened this issue 3 years ago • 6 comments

Given a 2-arm match that evaluates to a boolean we should offer an assist/lint diagnostic to turn it into the equivalent matches! invocation.

match scrutinee {
    Some(val) if val.cond() => true,
    _ => false,
}
// should become
matches!(scrutinee, Some(val) if val.cond());

We should also easily tell if it needs inversion, that is


match scrutinee {
    Some(val) if val.cond() => false,
    _ => true,
}
// should become (note the leading !)
!matches!(scrutinee, Some(val) if val.cond());

Making this an assist is probably simpler, and we can always upgrade it to a lint afterwards.

Veykril avatar Jun 12 '22 12:06 Veykril

Can i pick this up? @Veykril

neel-desh avatar Jun 12 '22 16:06 neel-desh

Sure. The assist should be placed in a new file at https://github.com/rust-lang/rust-analyzer/tree/master/crates/ide-assists/src/handlers I think and then registered at https://github.com/rust-lang/rust-analyzer/blob/4f2a67b26feba72dd9ae0ad0d36a2b36652cc68c/crates/ide-assists/src/lib.rs#L193

bjorn3 avatar Jun 12 '22 16:06 bjorn3

@bjorn3 Thank you, I would like to mention one thing, I have recently started learning Rust (few weeks back), So I might take a little while to complete this.

neel-desh avatar Jun 12 '22 17:06 neel-desh

No problem. If you have any questions feel free to ask here or on the rust-lang zulip (https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Frust-analyzer).

bjorn3 avatar Jun 12 '22 17:06 bjorn3

@neel-desh @bjorn3 May I work on it?

pocket7878 avatar Aug 12 '22 05:08 pocket7878

@pocket7878 hey go ahead.

neel-desh avatar Aug 12 '22 06:08 neel-desh