no-default-cases

Same as no_default_cases in Dart lints

Group
Style
Path
lint/style/no-default-cases
Default
Off, opt in
Origin
Dart lints
Rule
282 of 512

Flags default: in switch statements over enums and enum-like classes.

A default case hides newly added enum values: the switch keeps compiling and silently routes them to the fallback. Listing every value explicitly lets the compiler (or exhaustive-cases) point at the switch when the type grows. Only switch statements whose subject is provably an enum or an enum-like class (private non-factory constructors, at least two static constants of its own type, no subclasses in its library) are checked. The diagnostic covers the default member including its statement body.

switch (color) {
  case Color.red: paint();
  default: skip(); // flagged
}

Invalid

example.dartdart
enum Color { red, green, blue }

Color pick() => Color.red;

class Status {
  final int code;
  const Status._(this.code);
  static const ok = Status._(0);
  static const failed = Status._(1);
}

void f(Color color, Status status, Color? maybe) {
  switch (color) {
    case Color.red:
      print('red');
    default:
      print('other');
  }
  switch (status) {
    case Status.ok:
      print('ok');
    default:
      print('failed');
  }
  switch (color) {
    case Color.green:
    default:
      print('green or other');
  }
  switch (maybe) {
    case Color.blue:
      print('blue');
    default:
      print('other');
  }
  switch (pick()) {
    case Color.red:
      break;
    default:
      break;
  }
}
Avoid using a `default` member in this switch.
15 print('red');
16 default:
Avoid using a `default` member in this switch.
21 print('ok');
22 default:
Avoid using a `default` member in this switch.
26 case Color.green:
27 default:
Avoid using a `default` member in this switch.
32 print('blue');
33 default:
Avoid using a `default` member in this switch.
38 break;
39 default:

Valid

example.dartdart
enum Color { red, green, blue }

class Point {
  final int x;
  Point(this.x);
}

abstract class Shape {
  const Shape();
  static const unit = Circle();
  static const none = Circle();
}

class Circle extends Shape {
  const Circle();
}

extension type const Level._(int value) {
  static const low = Level._(0);
  static const high = Level._(1);
}

void levels(Level level) {
  switch (level) {
    case Level.low:
      print('low');
    default:
      print('other');
  }
}

void f(Color color, int n, String s, Point p, Shape shape, dynamic d) {
  switch (color) {
    case Color.red:
      print('red');
    case Color.green:
    case Color.blue:
      print('other');
  }
  switch (n) {
    case 1:
      print(1);
    default:
      print('n');
  }
  switch (s) {
    case 'a':
      print(s);
    default:
      print('default');
  }
  switch (p) {
    case Point(x: 0):
      print('origin');
    default:
      print('elsewhere');
  }
  switch (shape) {
    case Circle():
      print('circle');
    default:
      print('shape');
  }
  switch (d) {
    case 1:
      print(d);
    default:
      print('d');
  }
  final label = switch (color) {
    Color.red => 'red',
    _ => 'other',
  };
  print(label);
}

Configure no-default-cases

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "no-default-cases": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/no-default-cases: <reason>