max_nesting_depth
Reports a function that nests its control flow deeper than the limit. Leave early on the failing conditions, or move the inner work into a function of its own.
- Tier
- advisory
- Scope
- Dart
- Fix
- none
What it reports
Section titled “What it reports”A function whose control-flow statements stand inside each other deeper than the limit max. A function exactly at max is not reported. The violation is on the function’s name: “This function nests its control flow 5 levels deep, which is over the limit of 4.”
- The statements counted are
if,for,for-in,while,do,switchandtry, with braces or without. - An
if,else if,elsechain is one level however long it is. - An expression adds no level: a conditional, a
switchexpression, anifor aforinside a widget list. - Each function is measured on its own, closures and local functions included. A closure starts again at depth 0.
Where max_cognitive_complexity reports the same function, only that rule reports it, and its message gives this rule’s value.
A statement five levels deep runs only when five conditions hold at once, and you have to carry all five to know when. Code written by an AI coding agent nests readily: the next case is one more if inside the block that is there, and a limit makes it leave early instead. The rule is advisory because depth is a signal, not a proof: two loops over a grid with one test inside are three levels and plain.
Example
Section titled “Example”max: 2Future<void> sync(List<Account> accounts) async { for (final account in accounts) { if (account.isActive) { if (account.token != null) { try { final changes = await fetch(account); if (changes.isNotEmpty) { await store(account, changes); } } on TimeoutException { log('No answer for ${account.id}.'); } } } }}Future<void> sync(List<Account> accounts) async { for (final account in accounts) { if (!account.isActive || account.token == null) continue; await syncOne(account); }}
Future<void> syncOne(Account account) async { try { final changes = await fetch(account); if (changes.isEmpty) return; await store(account, changes); } on TimeoutException { log('No answer for ${account.id}.'); }}Both functions now have depth 2: the failing conditions leave early, and the work on one account has a name.
Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
max |
none in the rule; the presets give it | The largest number of control-flow statements that may stand one inside the other in a function. A whole number, zero or more. |
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
The rule has no limit of its own. The presets give one, and where no limit is in effect, the rule reports nothing:
| Preset | max |
|---|---|
core |
the rule is not in this preset |
recommended |
4 |
strict |
3 |
assay: rules: max_nesting_depth: max: 5Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression. It goes above the function’s name, where the violation is, or at the end of that line:
// The four loops walk the four axes of the tensor, and each needs its index.// ignore: assay/max_nesting_depthdouble contract(Tensor left, Tensor right) {Where max_cognitive_complexity reports the function, a suppression of this rule hides nothing, since that rule carries the violation. Suppress that rule instead: it covers this one too.
// ignore_for_file: assay/max_nesting_depth silences it in a whole file, and // ignore: max_nesting_depth without the prefix works too. See Suppressions.
Details
Section titled “Details”All cases
What is measured
Section titled “What is measured”Each of these is measured on its own:
- a top-level function, a method, a getter, a setter, an operator;
- a constructor with a body, factory constructors included;
- a local function, declared inside another one;
- a closure. It has no name, so the violation is on its parameter list:
(item)initems.forEach((item) { … }).
Beside max_cognitive_complexity
Section titled “Beside max_cognitive_complexity”Where max_cognitive_complexity reports the same function, only that rule reports it, and its message gives this rule’s value. This rule reports as usual where that rule does not judge the file (the preset does not switch it on, enabled: false, its exclude covers the file, or no limit is in effect) and at a function under that rule’s limit. Both hosts decide this the same way, before any suppression.
Why it matters
Section titled “Why it matters”Each level also pushes the code to the right, where line breaks get worse and the conditions that govern a line are a screen above it. Nothing makes an agent turn the conditions around and leave early; a limit does.
How the depth is counted
Section titled “How the depth is counted”The depth is the largest number of control-flow statements that stand one inside the other in the function’s body:
- The control-flow statements are
if,for,for-in,while,do,switchandtry. Each adds one level for what is inside it, with braces or without:if (ready) return;is a level likeif (ready) { return; }. - What stands directly in the body is at depth 0. A function with no control-flow statement has depth 0.
- An
elseis at the depth of itsif. Anelse ifgoes on with the chain at the same depth, soif,else if,else if,elseis one level however long it is. Anifthat is the single statement inside the braces of anelse,else { if (…) { … } }, is one level deeper. - The cases of a
switchare all one level inside it; acaseis no level of its own. - The catch clauses and the
finallyof atryare at the depth of its body, not inside each other. - A bare block, a labelled statement and an
assertadd no level. - An expression adds no level, and neither does an element of a collection literal: a conditional expression, a
switchexpression, anifor aforinside a list.
So this function has depth 3: the for, the if in it, the while in that. The else is at depth 1 again.
void drain(List<Queue> queues) { for (final queue in queues) { if (queue.isOpen) { while (queue.isNotEmpty) { queue.removeFirst(); } } else { queue.reset(); } }}A function declared inside another one adds no level to the function around it. Its statements are counted for it alone, from depth 0: an if inside a closure inside an if is depth 1 for the closure and depth 1 for the outer function, not 2.