Skip to main content

no-equal-switch-expression-cases

added in: 1.5.0
Dart 3.0+
Pro+
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: This switch expression already has a 'case' with the same body. Try combining these cases.

WithField(:final field) => field,
(WithField(:final field)) => field, // LINT: This switch expression already has a 'case' with the same body. Try combining these cases.

Object o => o.toString(),
dynamic o => o.toString(), // LINT: This switch expression already has a 'case' with the same body. Try combining these cases.
};

✅ Good:

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

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

WithField(:final field) => field,

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

Additional Resources