avoid-duplicate-patterns
preset: recommended
Warns when a LogicalOrPattern
or LogicalAndPattern
contains duplicate patterns.
Duplicate patterns are either the result of a typo or are redundant and can be simply removed.
Example
❌ Bad:
final object = WithField('hello');
// LINT: Avoid duplicate patterns. Try removing the duplicate pattern or replacing it with a different pattern.
if (object case Object() || Object()) {}
// LINT: Avoid duplicate patterns. Try removing the duplicate pattern or replacing it with a different pattern.
if (object case dynamic() && dynamic()) {}
// LINT: Avoid duplicate patterns. Try removing the duplicate pattern or replacing it with a different pattern.
if (object case != null && Object() && != null) {}
// LINT: Avoid duplicate patterns. Try removing the duplicate pattern or replacing it with a different pattern.
if (object case WithField() || AnotherClass() || WithField()) {}
// LINT: Avoid duplicate patterns. Try removing the duplicate pattern or replacing it with a different pattern.
if (object case > 10 || > 10) {}
final value = switch (object) {
// LINT: Avoid duplicate patterns. Try removing the duplicate pattern or replacing it with a different pattern.
!= null && Object() && != null => 'bad',
// LINT: Avoid duplicate patterns. Try removing the duplicate pattern or replacing it with a different pattern.
WithField() || AnotherClass() || WithField() => 'bad',
};
✅ Good:
final object = WithField('hello');
if (object case Object()) {} // Correct, no duplicate patterns
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',
};