Skip to content
non-exhaustive-match · Lint · warning

Non-exhaustive `match`

A `match` over an enum does not cover every variant and has no wildcard `_` arm.

Why this fires

A match expression over an enum does not cover every variant and has no wildcard _ arm. That means some future value will fall through and the program's behaviour at that point is undefined by the match.

enum Color { Red, Green, Blue }

fn name_of(c: Color) -> str {
    match c {
        Color::Red => "red",
        Color::Green => "green",
        // warning: non-exhaustive — `Color::Blue` not handled
    }
}

Fix it

1. Add the missing arms

The robust fix — every variant explicitly handled. If you later add a new variant to Color, the compiler will tell you exactly where you forgot to update.

match c {
    Color::Red => "red",
    Color::Green => "green",
    Color::Blue => "blue",
}

2. Add a wildcard arm

Use this only when "everything else" is genuinely the same answer. It also turns off the helpful "you forgot to update the match" signal — adding a new variant will silently fall into the wildcard branch.

match c {
    Color::Red => "red",
    _ => "not red",
}

A defensive variant — pair the wildcard with a panic so you find out at runtime:

match c {
    Color::Red => "red",
    _ => panic("unhandled variant: ${c}"),
}

See also

See also

enespt-br