Skip to main content

avoid-duplicate-patterns

added in: 1.5.0
Dart 3.0+
preset: recommended

Warns when a LogicalOrPattern or LogicalAndPattern contains duplicate patterns.

Example

❌ Bad:

final object = WithField('hello');

if (object case Object() || Object()) {} // LINT
if (object case dynamic() && dynamic()) {} // LINT

if (object case != null && Object() && != null) {} // LINT

if (object case WithField() || AnotherClass() || WithField()) {} // LINT

if (object case > 10 || > 10) {} // LINT

final value = switch (object) {
!= null && Object() && != null => 'bad', // LINT
WithField() || AnotherClass() || WithField() => 'bad', // LINT
};

✅ Good:

final object = WithField('hello');

if (object case Object()) {}
if (object case dynamic()) {}

if (object case != null && Object()) {}

if (object case WithField() || AnotherClass()) {}

if (object case > 10) {}

final value = switch (object) {
!= null && Object() => 'good',
WithField() || AnotherClass() => 'good',
};