exhaustive-cases

Same as exhaustive_cases in Dart lints

Path
lint/correctness/exhaustive-cases
Default
On (recommended)
Origin
Dart lints
Rule
234 of 512
recommended

Require switch statements over enum-like classes to cover every constant value.

The official visitor removes canonical constant values, not field names: top-level/static aliases, parenthesized constant patterns, and dot shorthands cover the same value. Canonicalization retains collection runtime types and applies constructor argument contexts (for example, 1 becomes 1.0 for a double parameter); it must not globally equate integer and double values.

Invalid

example.dartdart
const seed = 1;

class Traffic {
  const Traffic._(this.value);
  final int value;

  static const Traffic red = Traffic._(0);
  static const Traffic yellow = Traffic._(1);
  @deprecated
  static const Traffic amber = yellow;
  static const Traffic green = Traffic._(2);
}

void describe(Traffic light) {
  switch (light) {
    case Traffic.red:
      break;
    case Traffic.amber:
      break;
  }
}

class UntypedToken {
  const UntypedToken._(this.value);
  final Object value;

  static const first = const UntypedToken._(seed + 1);
  static const firstAlias = UntypedToken._(2);
  static const second = UntypedToken._((true ? 3 : 4, [1, 2], {'x': 1}));
}

void describeUntyped(UntypedToken token) {
  switch (token) {
    case UntypedToken.firstAlias:
      break;
  }
}
Add a case for `Traffic.green`.
14void describe(Traffic light) {
15 switch (light) {
Add a case for `UntypedToken.second`.
32void describeUntyped(UntypedToken token) {
33 switch (token) {

Valid

example.dartdart
class GoodTraffic {
  const GoodTraffic._(this.value);
  final int value;

  static const GoodTraffic red = GoodTraffic._(0);
  static const GoodTraffic yellow = GoodTraffic._(1);
  static const GoodTraffic green = GoodTraffic._(2);
}

void exhaustive(GoodTraffic light) {
  switch (light) {
    case GoodTraffic.red:
      break;
    case GoodTraffic.yellow:
      break;
    case GoodTraffic.green:
      break;
  }
}

void withDefault(GoodTraffic light) {
  switch (light) {
    default:
      break;
  }
}

class PubliclyConstructible {
  const PubliclyConstructible();
  static const PubliclyConstructible one = PubliclyConstructible();
  static const PubliclyConstructible two = PubliclyConstructible();
}

void ignored(PubliclyConstructible value) {
  switch (value) {}
}

enum Level {
  low._(1),
  high._(2);

  const Level._(this.value);
  final int value;
  static const Level min = low;
  static const Level max = high;
}

void exhaustiveEnum(Level level) {
  switch (level) {
    case Level.low:
      break;
    case Level.high:
      break;
  }
}

Configure exhaustive-cases

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "exhaustive-cases": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/correctness/exhaustive-cases: <reason>