Question

How to enforce error on impossible equality check?

There is a linter to it https://dart.dev/tools/linter-rules/unrelated_type_equality_checks but it just shows a warning, rendering it almost useless for large projects.

How to convert the warning into errors ?

Example:

void giveAccessToEverything() {
  /// nuclear codes
}
void main () {
  final isAdmin = "isAdmin"; // Database guy Changed to enum during migration 2.0

  if (isAdmin != true) { //  nobody saw this, I don't blame the team, I blame Dart
    return;
  }
  giveAccessToEverything();
}
 3  62  3
1 Jan 1970

Solution

 4

In analysis_options.yaml we actually can enforce any linter rule. IMHO, I think this one in particular should be a Dart built-in compiler error.

analyzer:
  errors:
    unrelated_type_equality_checks: error

linter:
  rules:
    unrelated_type_equality_checks: true

I don't know why we have to enable it manually, except to allow some bug like your nuclear code. Haven't ever seen a use-case where comparing two unrelated types is anywhere useful.

2024-07-11
TSR