avoid-collapsible-if

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

example.dartdart
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); } }
}
Merge this nested `if` with its outer condition using `&&`.
1void f(bool a, bool b) {
2 if (a) { if (b) {} }
Merge this nested `if` with its outer condition using `&&`.
2 if (a) { if (b) {} }
3 if (a) if (b) {}
Merge this nested `if` with its outer condition using `&&`.
3 if (a) if (b) {}
4 if (a || b) { if (!b) return; }
Merge this nested `if` with its outer condition using `&&`.
4 if (a || b) { if (!b) return; }
5 if (a) { { if (b) {} } }
Merge this nested `if` with its outer condition using `&&`.
5 if (a) { { if (b) {} } }
6 if (!a) { if (a && b) { print(a); } }

Valid

example.dartdart
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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "complexity": {
        "avoid-collapsible-if": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/complexity/avoid-collapsible-if: <reason>