exhaustive_cases

Group: style

Maturity: stable

Dart SDK: >= 2.9.0-12.0.dev • (Linter v0.1.116)

Since info is static, may be stale

View all Lint Rules

Using the Linter

Switching on instances of enum-like classes should be exhaustive.

Enum-like classes are defined as concrete (non-abstract) classes that have:

DO define case clauses for all constants in enum-like classes.

BAD:

class EnumLike {
  final int i;
  const E._(this.i);

  static const e = E._(1);
  static const f = E._(2);
  static const g = E._(3);
}

void bad(EnumLike e) {
  // Missing case.
  switch(e) { // LINT
    case E.e :
      print('e');
      break;
    case E.f :
      print('e');
      break;
  }
}

GOOD:

class EnumLike {
  final int i;
  const E._(this.i);

  static const e = E._(1);
  static const f = E._(2);
  static const g = E._(3);
}

void ok(E e) {
  // All cases covered.
  switch(e) { // OK
    case E.e :
      print('e');
      break;
    case E.f :
      print('e');
      break;
    case E.g :
      print('e');
      break;
  }
}