Skip to main content

avoid-unnecessary-patterns

added in: 1.25.0
Dart 3.0+
Pro+

Warns when a pattern variable declaration does not affect the type of an existing local variable.

Such declarations are often missing a type annotation or a null-check pattern.

Example

❌ Bad:

final value = 1;

void fn() {
// LINT: Avoid unnecessary patterns. Try changing it to a different pattern, for example, the null-check pattern (?).
if (value case final val) {}

// LINT: Avoid unnecessary patterns. Try changing it to a different pattern, for example, the null-check pattern (?).
if (value case var val) {}
}

✅ Good:

void fn() {
if (value case final val?) {}
if (value case final Object val) {}
if (value.sign case final sign) {}
}