avoid-inverted-boolean-expressions

Same as avoid_inverted_boolean_expressions in Pyramid Lint

Path
lint/complexity/avoid-inverted-boolean-expressions
Default
On (recommended)
Origin
Pyramid Lint
Rule
79 of 512
recommended

Flags a doubly-negated boolean expression such as !!x.

Two ! operators cancel out, so !!x is just x, and a longer run of leading bangs reduces to x or !x by parity. The redundant negation adds no meaning and obscures the value. The rule flags a ! whose operand is itself a !, reporting once at the outer negation and skipping the inner bangs. Despite the rule's name, it targets only stacked negations — it does not rewrite !(a == b) into a != b.

Invalid

example.dartdart
class BooleanExpressions {
  void examples() {
    bool isValid = true;
    bool condition = false;

    /// Double negation with double bang
    if (!!isValid) {
      print('valid');
    }

    /// Nested negation
    if (!(!condition)) {
      print('condition met');
    }

    /// Double negation in assignment
    final result = !!isValid;
    print(result);

    /// Double negation in variable declaration
    var flag = !!condition;

    /// Negation of negation in return
    if (!(!isValid)) {
      return;
    }

    /// Triple negation (still bad)
    bool x = !!!isValid;
  }
}

// Regression: the violation must still be found inside Dart 3 containers
// (pattern declaration, labeled statement, switch expression and subject,
// collection if/spread, record field).
void containersRegression(int rcount, bool flag) {
  final (ra, _) = (!!flag, 0);
  lbl: {
    final rb = !!flag;
    print(rb);
  }
  final rc = switch (rcount) {
    0 => !!flag,
    _ => null,
  };
  final rd = switch (!!flag) {
    _ => 0,
  };
  final re = [if (rcount > 0) !!flag];
  final rf = [...[!!flag]];
  final rg = (p: !!flag, q: 0);
  print([ra, rc, rd, re, rf, rg]);
}

bool nestedChains(bool a, bool b) =>
    !!(a &&
        !!b);
Avoid inverted boolean expressions. Simplify the double negation.
6 /// Double negation with double bang
7 if (!!isValid) {
Avoid inverted boolean expressions. Simplify the double negation.
11 /// Nested negation
12 if (!(!condition)) {
Avoid inverted boolean expressions. Simplify the double negation.
16 /// Double negation in assignment
17 final result = !!isValid;
Avoid inverted boolean expressions. Simplify the double negation.
20 /// Double negation in variable declaration
21 var flag = !!condition;
Avoid inverted boolean expressions. Simplify the double negation.
23 /// Negation of negation in return
24 if (!(!isValid)) {
Avoid inverted boolean expressions. Simplify the double negation.
28 /// Triple negation (still bad)
29 bool x = !!!isValid;
Avoid inverted boolean expressions. Simplify the double negation.
36void containersRegression(int rcount, bool flag) {
37 final (ra, _) = (!!flag, 0);
Avoid inverted boolean expressions. Simplify the double negation.
38 lbl: {
39 final rb = !!flag;
Avoid inverted boolean expressions. Simplify the double negation.
42 final rc = switch (rcount) {
43 0 => !!flag,
Avoid inverted boolean expressions. Simplify the double negation.
45 };
46 final rd = switch (!!flag) {
Avoid inverted boolean expressions. Simplify the double negation.
48 };
49 final re = [if (rcount > 0) !!flag];
Avoid inverted boolean expressions. Simplify the double negation.
49 final re = [if (rcount > 0) !!flag];
50 final rf = [...[!!flag]];
Avoid inverted boolean expressions. Simplify the double negation.
50 final rf = [...[!!flag]];
51 final rg = (p: !!flag, q: 0);
Avoid inverted boolean expressions. Simplify the double negation.
55bool nestedChains(bool a, bool b) =>
56 !!(a &&
Avoid inverted boolean expressions. Simplify the double negation.
56 !!(a &&
57 !!b);

Valid

example.dartdart
class BooleanExpressions {
  void examples() {
    bool isValid = true;
    bool condition = false;

    /// Direct boolean check
    if (isValid) {
      print('valid');
    }

    /// Single negation
    if (!condition) {
      print('condition met');
    }

    /// Direct assignment
    final result = isValid;
    print(result);

    /// Single negation in assignment
    var flag = !condition;

    /// Single negation in return
    if (isValid) {
      return;
    }

    /// Simple negation
    bool x = !isValid;
  }
}

Configure avoid-inverted-boolean-expressions

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "complexity": {
        "avoid-inverted-boolean-expressions": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/complexity/avoid-inverted-boolean-expressions: <reason>