Skip to main content

avoid-keywords-in-wildcard-pattern

dart 3.0+
has auto-fix
pro+

Warns when a wildcard pattern has declaration keywords.

Adding keywords to wildcard patterns in unnecessary, since the wildcard value cannot be referenced or mutated.

Example

❌ Bad:

void fn() {
final animal = 'hello';

final value = switch (animal) {
// LINT: Avoid keywords in wildcard patterns. Try removing the keyword.
final Object? _ => 'bad',

// LINT: Avoid keywords in wildcard patterns. Try removing the keyword.
var _ => 'bad',

// LINT: Avoid keywords in wildcard patterns. Try removing the keyword.
final _ => 'bad',
};
}

✅ Good:

void fn() {
final animal = 'hello';

final value = switch (animal) {
Object? _ => 'good', // Correct, no keywords
_ => 'good',
Object _ => 'good',
};
}