no-boolean-literal-compare

Same as no-boolean-literal-compare in Dart Code Metrics

Path
lint/complexity/no-boolean-literal-compare
Default
On (recommended)
Origin
Dart Code Metrics
Rule
281 of 512
recommended

Don't compare boolean values to boolean literals.

Invalid

example.dartdart
// Test cases for no-boolean-literal-compare.
// Only comparisons whose non-literal operand is PROVABLY boolean are flagged.

void knownBooleanComparisons(bool a, bool b, Object o) {
  if (true == false) {}
  if (!a == true) {}
  if ((a && b) == true) {}
  if ((a || b) != false) {}
  if ((a == b) == true) {}
  if ((o is String) == true) {}
}

// The type-resolution layer widens the check to locals/params whose inferred
// static type is a non-nullable `bool`, not just syntactically-boolean operands.
void resolvedNonNullableBooleans(bool flag) {
  if (flag == true) {}
  bool ready = flag;
  if (ready != false) {}
  final bool done = flag && ready;
  if (done == false) {}
}

class Widget {
  bool isEnabled = true;
}

bool checkStatus() => true;

void resolvedMembersAndReturns(Widget foo) {
  if (foo.isEnabled == true) {}
  if (checkStatus() == false) {}
}
Avoid comparing boolean values to boolean literals.
4void knownBooleanComparisons(bool a, bool b, Object o) {
5 if (true == false) {}
Avoid comparing boolean values to boolean literals.
5 if (true == false) {}
6 if (!a == true) {}
Avoid comparing boolean values to boolean literals.
6 if (!a == true) {}
7 if ((a && b) == true) {}
Avoid comparing boolean values to boolean literals.
7 if ((a && b) == true) {}
8 if ((a || b) != false) {}
Avoid comparing boolean values to boolean literals.
8 if ((a || b) != false) {}
9 if ((a == b) == true) {}
Avoid comparing boolean values to boolean literals.
9 if ((a == b) == true) {}
10 if ((o is String) == true) {}
Avoid comparing boolean values to boolean literals.
15void resolvedNonNullableBooleans(bool flag) {
16 if (flag == true) {}
Avoid comparing boolean values to boolean literals.
17 bool ready = flag;
18 if (ready != false) {}
Avoid comparing boolean values to boolean literals.
19 final bool done = flag && ready;
20 if (done == false) {}
Avoid comparing boolean values to boolean literals.
29void resolvedMembersAndReturns(Widget foo) {
30 if (foo.isEnabled == true) {}
Avoid comparing boolean values to boolean literals.
30 if (foo.isEnabled == true) {}
31 if (checkStatus() == false) {}

Valid

example.dartdart
// Good cases for no-boolean-literal-compare rule
// No violations expected

void testBooleanDirect() {
  if (isValid) {}
  if (!isActive) {}
  if (hasError) {}
  if (!isEmpty) {}
}

void testBooleanNegation() {
  if (!isReady) {}
  if (!isDone) {}
}

void testInWhile() {
  while (isRunning) {}
  while (!shouldContinue) {}
}

bool checkStatus() {
  return isInitialized;
}

void assertConditions() {
  assert(isValid);
  assert(!isDisabled);
}

class Widget {
  bool isEnabled = true;

  void setState() {
    if (isEnabled) {
      activate();
    }
  }

  bool canRender() => isEnabled;
}

void ternaryWithBoolean() {
  final status = isActive ? "on" : "off";
  final result = !isValid ? "error" : "ok";
}

void multipleComparisons() {
  if (flag1 && !flag2) {}
}

void methodCall(bool condition) {
  process(condition);
}

void complexLogic(bool a, bool b, bool c) {
  if (a && (b || !c)) {
    print('condition met');
  }
}

bool canAccess(bool isAuthenticated, bool isAuthorized) {
  return isAuthenticated && isAuthorized;
}

void toggleState(bool current) {
  final newState = !current;
  update(newState);
}

class FeatureFlag {
  bool isEnabled = false;

  void check() {
    if (!isEnabled) {
      enable();
    }
  }
}

// Resolved non-nullable members and returns can be compared directly.
void nullableIdiom(bool? maybe, Widget foo) {
  if (maybe == true) {}
  if (maybe != false) {}
}

// A `bool?` local/param resolves to a *nullable* bool, so the resolver leaves
// `== true` alone — it is the idiomatic null-safe form, not a redundant compare.
void resolvedNullableBoolean(bool? maybe) {
  bool? nb = maybe;
  if (nb == true) {}
  if (maybe != false) {}
}

Configure no-boolean-literal-compare

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "complexity": {
        "no-boolean-literal-compare": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/complexity/no-boolean-literal-compare: <reason>