avoid_collapsible_if
v0.8.0 Warning Fix Control Flow
This rule flags an if statement whose body is nothing but another if, where neither has an else.
Why use this rule
Section titled “Why use this rule”Two nested conditions with no else on either level are a conjunction written across two blocks. Merging them with && states the real condition in one place and removes a level of indentation from everything inside.
The nested form also hides the relationship: a reader has to scan to the end of the outer block to confirm nothing else happens there.
if (user != null) { if (user.isActive) { sendNotification(user); }}if (user != null && user.isActive) { sendNotification(user);}Known limitations
Section titled “Known limitations”The rule stays silent whenever the nesting could carry meaning:
- An
elseon either level. - Any other statement in the outer block, before or after the inner
if. - A pattern
if (x case P)on either level, which cannot be joined with&&.
The quick fix parenthesises an operand when needed, so merging a condition containing || does not change precedence.
Configuration
Section titled “Configuration”This rule is in the recommended preset, so it is on with
preset: recommended or preset: opinionated. Add it to preset: core with
avoid_collapsible_if: true.
To turn it off:
rules: avoid_collapsible_if: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_redundant_else— Drop the else when the if branch always exits.prefer_early_return— Replace a body-wrapping if with an early-return guard.prefer_immediate_return— Return an expression directly instead of via a throwaway variable.avoid_cascade_after_if_null— Detect cascades after if-null operators without parentheses.