Rules / Complexity / avoid-inverted-boolean-expressions
avoid-inverted-boolean-expressions
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;
}
}
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;
∙
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;
}
}
How to configure
Set the severity of avoid-inverted-boolean-expressions in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"complexity": {
"avoid-inverted-boolean-expressions": "error"
}
}
}
}