exhaustive-cases
Same as exhaustive_cases in Dart lints
- Group
- Correctness
- Path
lint/correctness/exhaustive-cases- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 234 of 512
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
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;
}
}void describe(Traffic light) { switch (light) {void describeUntyped(UntypedToken token) { switch (token) {Valid
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.
{
"linter": {
"rules": {
"correctness": {
"exhaustive-cases": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/exhaustive-cases: <reason>More correctness rules
- function-always-returns-null
Flags value functions with a proven explicit null return on every path.
- function-always-returns-same-value
Warns when distinct return paths in a function produce the same scalar constant.
- handle-bloc-event-subclassesbloc
Warns when a Bloc constructor leaves a concrete event subclass without a handler.
- hash-and-equals
hash_and_equalsrecommendedRequire
hashCodeand==to be overridden together. - implementation-imports
implementation_importsrecommendedDisallow importing another package's private
src/libraries. - implicit-reopen
implicit_reopenFlags classes that implicitly reopen a supertype declared in the same library.
- invalid-case-patterns
invalid_case_patternsrecommendedFlags pre-3.0 switch case expressions that change meaning or break under Dart 3 patterns.
- invalid-runtime-check-with-js-interop-types
invalid_runtime_check_with_js_interop_typesrecommendedReject platform-dependent runtime checks involving JavaScript interop types.