avoid-collapsible-if
- Group
- Complexity
- Path
lint/complexity/avoid-collapsible-if- Default
- Off, opt in
- Origin
- Falcon
- Rule
- 31 of 512
Flags an else-free if whose only then-statement is another else-free if.
Combining these conditions with && removes unnecessary nesting without
changing evaluation order. Both conditions must be ordinary expressions;
pattern conditions, either else branch, and blocks containing other
statements are excluded. No automatic edit is made, preserving comments.
Invalid
void f(bool a, bool b) {
if (a) { if (b) {} }
if (a) if (b) {}
if (a || b) { if (!b) return; }
if (a) { { if (b) {} } }
if (!a) { if (a && b) { print(a); } }
}void f(bool a, bool b) { if (a) { if (b) {} } if (a) { if (b) {} } if (a) if (b) {} if (a) if (b) {} if (a || b) { if (!b) return; } if (a || b) { if (!b) return; } if (a) { { if (b) {} } } if (a) { { if (b) {} } } if (!a) { if (a && b) { print(a); } }Valid
void f(bool a, bool b, Object? value) {
if (a && b) {}
if (a) { print(a); if (b) {} }
if (a) { if (b) {} else {} }
if (a) { if (b) {} } else {}
if (value case int n) { if (n > 0) {} }
if (a) { if (value case int n) { print(n); } }
}Configure avoid-collapsible-if
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"complexity": {
"avoid-collapsible-if": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/avoid-collapsible-if: <reason>More complexity rules
- avoid-complex-arithmetic-expressions
Reports arithmetic expressions containing too many terms.
- avoid-complex-conditions
Flags boolean expressions with more logical operands than the configured threshold.
- avoid-complex-loop-conditions
Flags loop conditions with more logical operands than the configured threshold.
- avoid-function-literals-in-foreach-calls
avoid_function_literals_in_foreach_callsFlags a function literal passed to an Iterable's
forEach. - avoid-if-with-many-branches
Reports long if/else-if chains; replace them with a switch or extracted helpers.
- avoid-inverted-boolean-expressions
avoid_inverted_boolean_expressionsrecommendedFlags a doubly-negated boolean expression such as
!!x. - 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.