avoid-bottom-type-in-patterns
Warns when a pattern contains a void
, Never
or Null
type.
Presence of these types inside a pattern is most likely a bug.
Example
❌ Bad:
final object = WithField('hello');
if (object case Null()) {} // LINT: Avoid the 'Null' type inside patterns. Try using a different type.
if (object case Never()) {} // LINT: Avoid the 'Never' type inside patterns. Try using a different type.
if (object case final Null value) {} // LINT: Avoid the 'Null' type inside patterns. Try using a different type.
if (object case final Never value) {} // LINT: Avoid the 'Never' type inside patterns. Try using a different type.
final value = switch (object) {
Null() => 'bad', // LINT: Avoid the 'Null' type inside patterns. Try using a different type.
Never() => 'bad', // LINT: Avoid the 'Never' type inside patterns. Try using a different type.
};
✅ Good:
final object = WithField('hello');
if (object == null) {}
if (object case == null) {} // Correct, 'null' instead of 'Null'
if (object case WithField()) {}
final value = switch (object) {
== null => 'good',
_ => 'good',
};