Skip to content

prefer_wildcard_pattern

v0.4.0 Warning Fix Pattern Matching

This rule flags a bare Object() pattern used as a catch-all. It matches everything non-null, exactly like _, but reads as if it were testing for something. The quick fix replaces it with _.

It applies in switch expressions, switch statements, and if-case conditions.

See also: Dart patterns

String describe(Object value) {
return switch (value) {
int() => 'a number',
String() => 'text',
Object() => 'something else', // LINT
};
}
void log(Object value) {
switch (value) {
case int():
print('int');
case Object(): // LINT
print('other');
}
}
void guard(Object value) {
if (value case Object()) { // LINT
print('always runs');
}
}
String describe(Object value) {
return switch (value) {
int() => 'a number',
String() => 'text',
_ => 'something else',
};
}
void log(Object value) {
switch (value) {
case int():
print('int');
case _:
print('other');
}
}
void guard(Object value) {
print('always runs');
}

That last one is the case worth noticing: if (x case Object()) is a condition that is always true for a non-null value, so the if itself was doing nothing.

Object() with fields is left alone. Once the pattern destructures something it is doing real work, and _ cannot replace it:

// Not reported — the pattern extracts a value
String describe(Object value) => switch (value) {
Object(hashCode: final h) => 'hash: $h',
};

Named types other than Object are not the target. int(), String(), or your own Response() narrow the match and are never reported.

Nested inside && and || still counts. Object() && Object() reports both operands, since each one is independently a catch-all.

The type name is matched textually. A class of your own named Object in scope would be reported as well.

This rule is in the recommended preset, so it is on with preset: recommended or preset: opinionated. Add it to preset: core with prefer_wildcard_pattern: true.

To turn it off:

many_lints.yaml
rules:
prefer_wildcard_pattern: false

To keep the rule on but skip certain paths, use per-rule exclude.