avoid-nested-if
Same as avoid_nested_if in Pyramid Lint
- Group
- Complexity
- Path
lint/complexity/avoid-nested-if- Default
- Off, opt in
- Origin
- Pyramid Lint
- Rule
- 100 of 512
Flags an if statement whose then-branch contains nested if statements.
Pyramid-shaped nesting is hard to follow; combining conditions with &&,
using early returns, or extracting a method flattens it. The rule counts the
if statements within a then-branch subtree and fires when at least two are
present (pyramid_lint's default max_nesting_level of two), so an if whose
body nests two further ifs is reported. Local functions and closures count
as part of the then-branch subtree, and closures anywhere in the file are checked.
Invalid
class NestedIfExamples {
// A vertical chain: the outer then-branch contains two further `if`
// statements (b and c), so the outermost `if` is flagged.
void chain(bool a, bool b, bool c) {
if (a) {
if (b) {
if (c) {
print('all true');
}
}
}
}
// Two sibling `if` statements inside the then-branch also reach the
// nesting threshold of two.
void siblings(bool a, bool b, bool c) {
if (a) {
if (b) {
print('b');
}
if (c) {
print('c');
}
}
}
// Nested `if` statements reached through an intervening loop still count.
void throughLoop(bool a, bool b, List<int> xs) {
if (a) {
for (final x in xs) {
if (b) {
if (x > 0) {
print(x);
}
}
}
}
}
// A label on the nested chain must not hide the `if`s it wraps.
void labeledChain(bool a, bool b, bool c) {
if (a) {
outer:
if (b) {
if (c) {
print('all true');
}
}
}
}
void doSomething() {}
} void chain(bool a, bool b, bool c) { if (a) { void siblings(bool a, bool b, bool c) { if (a) { void throughLoop(bool a, bool b, List<int> xs) { if (a) { void labeledChain(bool a, bool b, bool c) { if (a) {Valid
class NestedIfExamples {
// A single level of nesting is allowed (default max_nesting_level is 2).
void singleNesting(bool a, bool b) {
if (a) {
if (b) {
print('both true');
}
}
}
// Combined conditions avoid nesting entirely.
void combined(bool a, bool b, bool c) {
if (a && b && c) {
print('all true');
}
}
// Guard clauses with early returns keep the body flat.
void guards(bool a, bool b) {
if (!a) {
return;
}
doSomething();
if (!b) {
return;
}
print('both conditions met');
}
// An if/else-if chain is a sibling chain, not nesting: each then-branch
// holds no further `if`.
void elseIfChain(int code) {
if (code == 1) {
print('one');
} else if (code == 2) {
print('two');
} else {
print('other');
}
}
// A labeled `if` with only one nested `if` stays under the threshold.
void labeledSingle(bool a, bool b) {
outer:
if (a) {
if (b) {
print('two');
}
}
}
void doSomething() {}
}
void unbracedThenRoot(bool a, bool b, bool c) {
if (a)
if (b) {
if (c) {}
}
}Configure avoid-nested-if
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"complexity": {
"avoid-nested-if": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/avoid-nested-if: <reason>More complexity rules
- 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. - avoid-unnecessary-type-assertions
avoid-unnecessary-type-assertionsrecommendedFlags an
ischeck whose result is already guaranteed by the resolved static type. - avoid-unnecessary-type-casts
avoid-unnecessary-type-castsrecommendedFlags an
ascast to the operand's resolved static type. - cascade-invocations
cascade_invocationsFlags consecutive statements that invoke members on the same variable.