Skip to main content

E0028: Unreachable Pattern

A pattern in your match expression can never be reached because previous patterns already cover all its cases.

Example

enum Status {
Active,
Inactive,
}

fn check(s: Status) -> i64 {
match s {
Status::Active => { 1 },
Status::Inactive => { 0 },
Status::Active => { 2 }, // Error: unreachable pattern
}
}
enum Color {
Red,
Green,
Blue,
}

fn check(c: Color) -> i64 {
match c {
_ => { 0 },
Color::Red => { 1 }, // Error: unreachable pattern (wildcard already matches everything)
}
}

How to fix

Remove the unreachable pattern:

enum Status {
Active,
Inactive,
}

fn check(s: Status) -> i64 {
match s {
Status::Active => { 1 },
Status::Inactive => { 0 },
}
}

Or reorder your patterns so more specific ones come first:

enum Color {
Red,
Green,
Blue,
}

fn check(c: Color) -> i64 {
match c {
Color::Red => { 1 },
_ => { 0 }, // OK: wildcard comes after specific patterns
}
}