avoid-inverted-boolean-checks

Path
lint/suspicious/avoid-inverted-boolean-checks
Default
Off, opt in
Origin
Falcon
Rule
78 of 512

Prefer direct equality comparisons and De Morgan forms in control-flow conditions. Unlike avoid-inverted-boolean-expressions, this does not diagnose !!value. Relational operators are excluded because NaN and custom operators lack reliable inverses.

Invalid

example.dartdart
void f(int x, int y, bool a, bool b) {
  if (!(x == y)) {}
  if (!(x != y)) {}
  if (!(a && b)) {}
  while (!(a || b)) {}
}
Express this check directly instead of negating a comparison or logical expression.
1void f(int x, int y, bool a, bool b) {
2 if (!(x == y)) {}
Express this check directly instead of negating a comparison or logical expression.
2 if (!(x == y)) {}
3 if (!(x != y)) {}
Express this check directly instead of negating a comparison or logical expression.
3 if (!(x != y)) {}
4 if (!(a && b)) {}
Express this check directly instead of negating a comparison or logical expression.
4 if (!(a && b)) {}
5 while (!(a || b)) {}

Valid

example.dartdart
void f(int x, int y, bool a, bool b) {
  if (x != y) {}
  if (x == y) {}
  if (!a) {}
  final c = !!a;
  final d = !a || !b;
  final e = !(x + y).isEven;
  final inverted = !(a || b);
  final comparison = !(x == y);
  final callback = () => !(x > y);
}

void relational(double x, int y, GreaterOnly custom) {
  if (!(x > 0)) {}
  if (!(x >= 0)) {}
  if (!(x < 0)) {}
  if (!(x <= 0)) {}
  final d = !((x <= y)) ? 1 : 2;
  if (!(custom > 0)) {}
}

class GreaterOnly {
  bool operator >(int other) => true;
}

Configure avoid-inverted-boolean-checks

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "avoid-inverted-boolean-checks": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/suspicious/avoid-inverted-boolean-checks: <reason>