LYR-SEM0098
a refutable pattern without a path for a miss
A let pattern that can fail has no else, the else of a let … else does not leave on every path, a for head or lambda parameter has a refutable pattern, or a let condition stands outside an if/while head.
Explanation
A pattern that can fail needs somewhere to go when it does. let .Num(n) = s else { return 0; }; names it, and the else must leave (return, throw, break, continue, a panic), because the names are not bound when it runs. A loop head or a lambda parameter has no such path, and a let condition belongs directly in if (…) or while (…).
Add an else that leaves, use match, or bind a name and take it apart in the body.
Wrong
enum Shape {
Num(int),
Empty,
}
fn number(s: Shape): int {
let .Num(n) = s;
return n;
}
fn main(): void {
let _ = number(Shape.Empty);
}
Right
enum Shape {
Num(int),
Empty,
}
fn number(s: Shape): int {
let .Num(n) = s else { return 0; };
return n;
}
fn main(): void {
let _ = number(Shape.Empty);
}