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.
Known limitations
Section titled “Known limitations”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 valueString 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.
Configuration
Section titled “Configuration”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:
rules: prefer_wildcard_pattern: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_wildcard_cases_with_enums— Opposing convention. Keep exhaustiveness checking by listing enum cases explicitly.prefer_switch_with_enums— Use a switch instead of an if-else chain over enum constants.avoid_single_field_destructuring— Avoid destructuring a single field when direct property access is simpler.use_existing_destructuring— Add properties to an existing destructuring instead of accessing them directly.