Lyricdev
GitHub

LYR-SEM0112

a match arm no value can reach

warning · spec 09-patterns · lint unreachable-pattern, group unused, warn unless said

An arm of a 'match' stands after an arm that already matches every value, or tests the same variant or literal as an unguarded arm above it.

Explanation

Arms are tried top to bottom, and the first that matches wins. An arm after a catch-all (_, a plain binding) or after an unguarded arm with the same exact pattern never runs. The program is still correct, so this is a warning; but the dead arm is usually a mistake in the order or in the pattern.

Remove the arm, reorder the arms, or correct the pattern it was meant to have.

Wrong

enum Signal { Red, Yellow, Green }

fn code(s: Signal): int {
    return match (s) {
        .Red => 0,
        .Red => 1,
        _ => 2,
    };
}

fn main(): void {
    let _ = code(Signal.Green);
}
enum Signal { Red, Yellow, Green }

fn code(s: Signal): int {
    return match (s) {
        .Red => 0,
        .Yellow => 1,
        _ => 2,
    };
}

fn main(): void {
    let _ = code(Signal.Green);
}