avoid-inverted-boolean-expressions
Same as avoid_inverted_boolean_expressions in Pyramid Lint
- Group
- Complexity
- Path
lint/complexity/avoid-inverted-boolean-expressions- Default
- On (recommended)
- Origin
- Pyramid Lint
- Rule
- 79 of 512
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
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); /// Double negation with double bang if (!!isValid) { /// Nested negation if (!(!condition)) { /// Double negation in assignment final result = !!isValid; /// Double negation in variable declaration var flag = !!condition; /// Negation of negation in return if (!(!isValid)) { /// Triple negation (still bad) bool x = !!!isValid;void containersRegression(int rcount, bool flag) { final (ra, _) = (!!flag, 0); lbl: { final rb = !!flag; final rc = switch (rcount) { 0 => !!flag, }; final rd = switch (!!flag) { }; final re = [if (rcount > 0) !!flag]; final re = [if (rcount > 0) !!flag]; final rf = [...[!!flag]]; final rf = [...[!!flag]]; final rg = (p: !!flag, q: 0);bool nestedChains(bool a, bool b) => !!(a && !!(a && !!b);Valid
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.
{
"linter": {
"rules": {
"complexity": {
"avoid-inverted-boolean-expressions": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/avoid-inverted-boolean-expressions: <reason>More complexity rules
- avoid-long-records
Reports record values and record types with too many fields.
- avoid-nested-conditional-expressions
avoid-nested-conditional-expressionsFlags a ternary conditional expression nested inside another ternary.
- avoid-nested-if
avoid_nested_ifFlags an
ifstatement whose then-branch contains nestedifstatements. - avoid-nested-switch-expressions
Reports deeply nested switch expressions.
- avoid-nested-switches
Reports deeply nested switch statements.
- avoid-nested-try-statements
Reports deeply nested try statements.
- avoid-redundant-async
avoid-redundant-asyncrecommendedFlags an
asyncfunction whose body is a singlereturn await ...;. - avoid-unnecessary-containers
avoid_unnecessary_containersrecommendedflutterFlags a
Containerwhose only argument is achild.