Description
When writing macros that expand to "match" expressions, it can be hard to guarantee that all cases of the match will be necessary. Consider the following macro:
macro_rules! case(
($e:expr, $p:pat where $cond:expr => $thn:expr, $els:expr) => (
match $e {
$p => if $cond { $thn } else { $els },
_ => $els
}
)
)
An example use of this macro would be case!(some_expr, x where x > 3 => x, 3)
, which is equivalent to max(3, some_expr)
(a silly example, but still).
Unfortunately, this example fails to compile because it expands to
match some_expr {
x => if x > 3 { x } else { 3 },
_ => 3
}
The last case of the match is redundant. However, there are also uses of the macro where the last case will not be redundant, eg. case!(some_expr, [email protected] where x % 2 == 0 => true, false)
.
It would be nice if there were some way to mark that a particular match
expression is allowed to have unreachable cases, or (better yet) that a particular case was allowed to be unreachable.