avoid_deep_nesting
This rule flags control flow nested more deeply than the configured budget.
Each level of if, for, while, try or switch is another condition the reader has to hold to know why a line runs at all. The innermost statement of a five-level nest is reachable only through a path nobody can state out loud.
Depth is usually more actionable than complexity: an early return, a guard clause or an extracted method removes a whole level, where a high complexity count says only that something is wrong.
An else if is a sibling branch, not another level — it is written flat and reads flat, so a long dispatch chain is never reported. A nested function starts its own count, since its body is not reached through the enclosing nest.
One diagnostic per function, anchored at the statement that first crosses the limit.
This rule is in the pedantic preset.
void process(List<List<int>> rows, bool enabled) { if (enabled) { for (final row in rows) { for (final cell in row) { if (cell > 0) { while (cell > 1) { // 5 levels in handle(cell); } } } } }}void process(List<List<int>> rows, bool enabled) { if (!enabled) return;
for (final row in rows) { _processRow(row); }}
void _processRow(List<int> row) { for (final cell in row) { if (cell <= 0) continue; _handleRepeatedly(cell); }}Enabling this rule
Section titled “Enabling this rule”This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:
rules: avoid_deep_nesting: enabled: trueOptions
Section titled “Options”many_lints: rules: avoid_deep_nesting: max_depth: 4rules: avoid_deep_nesting: max_depth: 4| Option | Type | Default | Description |
|---|---|---|---|
max_depth |
int | 4 |
How many levels of control flow may nest |
Turning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: avoid_deep_nesting: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_complex_conditions— Keep boolean conditions within an operand budget.avoid_high_cyclomatic_complexity— Keep a function within a complexity budget.avoid_long_functions— Keep function bodies within a line budget.max_statements— Keep a function within a statement budget.