Skip to content

avoid_wildcard_cases_with_enums

v0.8.0 Warning Pattern Matching

This rule flags a _ or default case in a switch over a non-nullable enum.

A switch over an enum with no catch-all is checked for exhaustiveness: add a constant and the compiler points at every switch that must now handle it. A wildcard case turns that off permanently — new constants fall into the catch-all and inherit behaviour written for cases nobody had in mind, and the bug shows up at runtime.

See also: Dart: exhaustiveness checking

enum Status { active, inactive, pending }
String describe(Status status) => switch (status) {
Status.active => 'Active',
// Add a constant to Status and it silently becomes 'Other'
_ => 'Other', // LINT
};
String describe(Status status) => switch (status) {
Status.active => 'Active',
Status.inactive => 'Inactive',
Status.pending => 'Pending',
};

If several constants share behaviour, group them with || and keep the check:

String describe(Status status) => switch (status) {
Status.active => 'Active',
Status.inactive || Status.pending => 'Not active',
};

Both default: and case _: are reported:

// Don't
void handle(Status status) {
switch (status) {
case Status.active:
start();
default: // LINT
stop();
}
}
// Do
void handle(Status status) {
switch (status) {
case Status.active:
start();
case Status.inactive:
case Status.pending:
stop();
}
}

The rule stays silent where a catch-all is legitimate:

  • Nullable enums. Status? needs a case for null, and _ is a reasonable way to write it.
  • Guarded wildcards. _ when flag => ... is conditional, so the compiler still checks the remaining constants.
  • Non-enum switches, including sealed class hierarchies, which are outside this rule’s scope.

This rule appears only in the pedantic preset, because catch-all behaviour is sometimes the contract — mapping every unsupported HTTP method to one response, for instance.

Enable it by name:

many_lints.yaml
rules:
avoid_wildcard_cases_with_enums: true

To keep the rule on but skip certain paths, use per-rule exclude.