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
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;
}
} print('red'); default: print('ok'); default: case Color.green: default: print('blue'); default: break; default:Valid
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.
{
"linter": {
"rules": {
"style": {
"no-default-cases": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/no-default-cases: <reason>More style rules
- no-leading-underscores-for-library-prefixes
no_leading_underscores_for_library_prefixesrecommendedFlags import prefixes that begin with an underscore.
- no-leading-underscores-for-local-identifiers
no_leading_underscores_for_local_identifiersrecommendedFlags local variables and parameters whose names begin with an underscore.
- no-literal-bool-comparisons
no_literal_bool_comparisonsFlags boolean literals compared or combined with a non-nullable
bool. - no-magic-number
no-magic-numberFlags unnamed numeric literals (magic numbers) used inline in expressions.
- no-object-declaration
no-object-declarationFlags class members declared with the type
Object(orObject?). - no-runtime-type-to-string
no_runtimeType_toStringAvoid calling
toString()onruntimeType. - non-constant-identifier-names
non_constant_identifier_namesrecommendedFlags non-constant identifiers that are not written in lowerCamelCase.
- omit-local-variable-types
omit_local_variable_typesFlags local variable type annotations that repeat the initializer's type.