avoid-unnecessary-negations

Group
Style
Path
lint/style/avoid-unnecessary-negations
Default
Off, opt in
Origin
Falcon
Rule
168 of 512

Simplifies negated bool literals and equality comparisons with a logical negation on both sides. Stacked negations belong to avoid-inverted-boolean-expressions; arithmetic, bitwise and relational operators are deliberately excluded.

Invalid

example.dartdart
bool trueLiteral() => !true;
bool falseLiteral() => !(false);
bool equal(bool flag, bool another) => !flag == !another;
bool unequal(bool flag, bool another) => (!flag) != (!another);
bool nested(bool a) => a && !true;
bool groupedEqual(bool a, bool b) => (!(a)) == (!(b));
Simplify these unnecessary logical negations.
1bool trueLiteral() => !true;
Simplify these unnecessary logical negations.
1bool trueLiteral() => !true;
2bool falseLiteral() => !(false);
Simplify these unnecessary logical negations.
2bool falseLiteral() => !(false);
3bool equal(bool flag, bool another) => !flag == !another;
Simplify these unnecessary logical negations.
3bool equal(bool flag, bool another) => !flag == !another;
4bool unequal(bool flag, bool another) => (!flag) != (!another);
Simplify these unnecessary logical negations.
4bool unequal(bool flag, bool another) => (!flag) != (!another);
5bool nested(bool a) => a && !true;
Simplify these unnecessary logical negations.
5bool nested(bool a) => a && !true;
6bool groupedEqual(bool a, bool b) => (!(a)) == (!(b));

Valid

example.dartdart
bool single(bool flag) => !flag;
bool equality(bool flag, bool another) => flag == another;
bool unequal(bool flag, bool another) => flag != another;
bool different(bool flag, bool another) => !flag == another;
bool conjunction(bool flag, bool another) => !flag && !another;
bool disjunction(bool flag, bool another) => !flag || !another;
int arithmetic(int value) => -(-value);
int bitwise(int value) => ~(~value);
bool relational(int a, int b) => !(a < b);
bool floating(double a, double b) => !(a >= b);
bool doubleNegation(bool flag) => !!flag;
bool parenthesized(bool flag) => !(!flag);
bool triple(bool flag) => !!!flag;
bool nested(bool a, bool b) => !!(a && !(!b));
bool stackedLiteral() => !!true;

Configure avoid-unnecessary-negations

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-unnecessary-negations": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/avoid-unnecessary-negations: <reason>