Skip to main content

no-equal-switch-expression-cases

added in: 1.5.0
Dart 3.0+
preset: recommended

Warns when a switch expression has cases with equal bodies.

Example

❌ Bad:

class WithField {
final String field;

const WithField(this.field);
}

final object = WithField('hello');

final value = switch (object) {
Object() => 'string literal',
WithField() => 'string literal', // LINT

WithField() => object.field,
(WithField f) => f.field,

WithField(:final field) => field,
(WithField(:final field)) => field, // LINT

Object o => o.toString(),
dynamic o => o.toString(), // LINT
};

✅ Good:

class WithField {
final String field;

const WithField(this.field);
}

final object = WithField('hello');

final value = switch (object) {
Object() || WithField() => 'string literal',

WithField() => object.field,
(WithField f) => f.field,

WithField(:final field) => field,

(Object() || WithField()) && final o => o.toString(),
};