I hit an interesting spurious "match in match" non exhaustive error with rust. To make a small simple example (of course the production case is more interesting):
pub enum SomeEnum {
Possibility1,
Possibility2,
Possibility3
}
pub fn do_something(some: &SomeEnum){
match some {
SomeEnum::Possibility1 => println!("this is possibility 1"),
something_else => {
println!("this is not possibility 1");
match something_else {
SomeEnum::Possibility2 => println!("but this is 2"),
SomeEnum::Possibility3 => println!("but this is 3")
}
}
}
}
does not compile, with an error:
error[E0004]: non-exhaustive patterns: `&Possibility1` not covered
--> src/main.rs:13:19
|
1 | / pub enum SomeEnum {
2 | | Possibility1,
| | ------------ not covered
3 | | Possibility2,
4 | | Possibility3
5 | | }
| |_- `SomeEnum` defined here
...
13 | match something_else {
| ^^^^^^^^^^^^^^ pattern `&Possibility1` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
= note: the matched value is of type `&SomeEnum`
I understand this error, and I know how to "brutally" fix it (similarly to Non-exhaustive patterns - Rust match expressions by adding a:
_ => unreachable!();
branch for example).
Still, I feel this is very sad: the compiler is smart enough at the first match to know that the something_else
is either of kind Possibility2
or Possibility3
, while toss away this knowledge down the code?
My question is therefore: is there a simple, idiomatic way to force rust to "propagate" this kind knowledge from the outer match to the inner match so that I can suppress the error without brutally matching all inputs? Again, in this simple example the _
match may seem like the no-brainer, but in some more complex code, I would like to have help from the compiler checking that my logics are right, and this disqualifies using a _
match-it-all filter.