Unreachable arm
WarningUnsafe fixAn arm of a match that no value reaches, because the arms above it already match every value it matches. When one arm above takes all of them, a label points at it. The program still builds.
An alternative of a | pattern that no value reaches is reported the same way, as an "unreachable alternative": the arms above, or the alternatives before it in its own arm, take its values. A guarded arm takes no values, since its guard can be false.
Why
Arms are tried in order and the first that matches wins, so an arm below others that match the same values never runs. That is usually a mistake: an arm placed below a catch-all, or a pattern meant to be narrower. The check sees what the arms above match together, so Add(_, _) is unreachable after arms for Add(Num(_), _), Add(Add(_, _), _) and Add(Mul(_, _), _), though no one of them covers it.
The unsafe fix removes the arm, or the alternative with the | that joins it. It is unsafe because the arm may be the one you meant, and the arm above it the mistake.
Broken
enum Light {
Red
Green
}
fn go(l: Light) -> bool {
match l {
_ => false
Green => true
}
}
fn main() {
print(go(Light.Green))
print(go(Light.Red))
}
What sisu says
warning[W0006]: unreachable arm --> W0006.sisu:9:9 | 8 | _ => false | - this arm covers it 9 | Green => true | ^^^^^ | help: remove the arm | 9 - Green => true | sisu: 1 warning sisu: for more on a code, run `sisu explain <code>`: W0006
Fixed
enum Light {
Red
Green
}
fn go(l: Light) -> bool {
match l {
Green => true
_ => false
}
}
fn main() {
print(go(Light.Green))
print(go(Light.Red))
}
Related
- E0130: a
matchthat does not cover every value.
Sisu