Skip to content

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

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, switch and try, with braces or without.
  • An if, else if, else chain is one level however long it is.
  • An expression adds no level: a conditional, a switch expression, an if or a for inside 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.

Violatingwith max: 2
Future<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}.');
}
}
}
}
}
CorrectedIts depth is 5.
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.

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: 5

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_depth
double 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.

All cases

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) in items.forEach((item) { … }).

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.

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.

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, switch and try. Each adds one level for what is inside it, with braces or without: if (ready) return; is a level like if (ready) { return; }.
  • What stands directly in the body is at depth 0. A function with no control-flow statement has depth 0.
  • An else is at the depth of its if. An else if goes on with the chain at the same depth, so if, else if, else if, else is one level however long it is. An if that is the single statement inside the braces of an else, else { if (…) { … } }, is one level deeper.
  • The cases of a switch are all one level inside it; a case is no level of its own.
  • The catch clauses and the finally of a try are at the depth of its body, not inside each other.
  • A bare block, a labelled statement and an assert add no level.
  • An expression adds no level, and neither does an element of a collection literal: a conditional expression, a switch expression, an if or a for inside 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.