Skip to main content

unrelated_type_equality_checks

The type of the operand ('{0}') isn't a subtype or a supertype of the value being matched ('{1}').

The type of the right operand ('{0}') isn't a subtype or a supertype of the left operand ('{1}').

Description

#

The analyzer produces this diagnostic when two objects are being compared and neither of the static types of the two objects is a subtype of the other.

Such a comparison will usually return false and might not reflect the programmer's intent.

There can be false positives. For example, a class named Point might have subclasses named CartesianPoint and PolarPoint, neither of which is a subtype of the other, but it might still be appropriate to test the equality of instances.

As a concrete case, the classes Int64 and Int32 from package:fixnum allow comparing instances to an int provided the int is on the right-hand side. This case is specifically allowed by the diagnostic, but other such cases are not.

Example

#

The following code produces this diagnostic because the string s is being compared to the integer 1:

dart
bool f(String s) {
  return s == 1;
}

Common fixes

#

Replace one of the operands with something compatible with the other operand:

dart
bool f(String s) {
  return s.length == 1;
}