Skip to content

empty_block

Reports a statement block that holds nothing, not even a comment. Write the missing code, delete the statement, or say in a comment why it is empty.

Tier
advisory
Scope
Dart
Lowest preset
strict
Fix
none

A block of a statement that holds nothing, not even a comment: if (ready) {}, else {}, for (final item in items) {}, try {}, finally {}, or a block that stands alone. The violation is on the block.

It does not report:

  • a block with a comment in it, which says why it is empty;
  • a function body, which no_placeholder_implementation and the SDK’s lint of empty constructor bodies own;
  • a catch clause, which avoid_swallowed_errors and the SDK’s lint of empty catch clauses own;
  • the first branch of an if that has an else. In a chain that tells cases apart, if (kind is Library) {} else if (kind is Part) {…} else {…}, it says that one case needs nothing.
  • an if whose branches are both empty, which no_equal_branches reports.

An empty loop body is reported only where the loop can do nothing at all. These are not reported, because the loop itself does work:

  • a while or do loop whose condition may call code: it polls;
  • an await for, which drains a stream;
  • a for-in over a value that may run code to give its elements;
  • a for loop that calls code in its condition, or updates a variable it does not declare itself, as a search loop does.

An empty block is a statement that was started and never finished, or one that was emptied and left behind. Either the code meant for it is missing, or the statement does nothing and can go. A comment in it is enough to say that the emptiness is meant.

Violating
if (response.statusCode == 401) {}
saveSession(response);
Corrected
if (response.statusCode == 401) {
return signOut();
}
saveSession(response);

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

A comment inside the block is usually simpler. To suppress it, write why on the line directly above the suppression, which goes above the line where the block starts:

// The branch is kept for the case the protocol adds next.
// ignore: empty_block
if (kind == Kind.reserved) {}

// ignore_for_file: assay/empty_block silences it in a whole file, and // ignore: assay/empty_block with the prefix works too. See Suppressions.