Skip to main content

avoid-keywords-in-wildcard-pattern

added in: 1.7.0
🛠
Dart 3.0+
Pro+
preset: recommended

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',
};
}