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.
Why use this rule
Section titled “Why use this rule”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',};In a switch statement
Section titled “In a switch statement”Both default: and case _: are reported:
// Don'tvoid handle(Status status) { switch (status) { case Status.active: start(); default: // LINT stop(); }}
// Dovoid handle(Status status) { switch (status) { case Status.active: start(); case Status.inactive: case Status.pending: stop(); }}Known limitations
Section titled “Known limitations”The rule stays silent where a catch-all is legitimate:
- Nullable enums.
Status?needs a case fornull, 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.
Configuration
Section titled “Configuration”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:
rules: avoid_wildcard_cases_with_enums: trueTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_wildcard_pattern— Opposing convention. Use the wildcard pattern ‘_’ instead of ‘Object()’ for catch-all cases.prefer_switch_with_enums— Use a switch instead of an if-else chain over enum constants.avoid_single_field_destructuring— Avoid destructuring a single field when direct property access is simpler.use_existing_destructuring— Add properties to an existing destructuring instead of accessing them directly.