Skip to content

catch_all_case_on_closed_type

Reports a catch-all case of a switch on an enum or a sealed class that stands in for only one or two values. Name them instead.

Tier
advisory
Scope
Dart
Lowest preset
strict
Fix
none

A default or a _ case, or a case of a variable without a type, in a switch on an enum or a sealed class, where the other cases already name all but one or two of its values or subtypes.

  • A value counts as named by a case that names it or a type it has, also under a when clause or with a test of a field.
  • Null counts as one more value of a nullable type.
  • A sealed class counts its subclasses in its own library, including those of a sealed class below it.

It does not report:

  • a catch-all that stands in for three values or more: a switch that handles a few of many on purpose;
  • a catch-all beside cases that name every value;
  • the default of a switch statement on an enum, which the SDK lint no_default_cases reports;
  • a sealed class with a subtype that is generic, an enum, a mixin or an extension type, nor a generic enum.

The compiler checks a switch that names every value: a value added later is an error until the switch handles it. A catch-all turns that check off, and where it stands in for one or two values it saves little.

The rule is in strict only because a catch-all is a valid choice where new values should fall into the same case.

Violating
String label(Status status) => switch (status) {
Status.open => 'open',
Status.paid => 'paid',
_ => 'sent',
};
Corrected
String label(Status status) => switch (status) {
Status.open => 'open',
Status.paid => 'paid',
Status.sent => 'sent',
};

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Write why on the line directly above the suppression:

// Every status but open looks the same on this screen.
// ignore: assay/catch_all_case_on_closed_type

// ignore_for_file: assay/catch_all_case_on_closed_type silences it in a whole file, and // ignore: catch_all_case_on_closed_type without the prefix works too. See Suppressions.