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
What it reports
Section titled “What it reports”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_implementationand the SDK’s lint of empty constructor bodies own; - a catch clause, which
avoid_swallowed_errorsand the SDK’s lint of empty catch clauses own; - the first branch of an
ifthat has anelse. 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
ifwhose branches are both empty, whichno_equal_branchesreports.
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
whileordoloop whose condition may call code: it polls; - an
await for, which drains a stream; - a
for-inover a value that may run code to give its elements; - a
forloop 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.
Example
Section titled “Example”if (response.statusCode == 401) {}saveSession(response);if (response.statusCode == 401) { return signOut();}saveSession(response);Rule parameters
Section titled “Rule parameters”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. |
Suppressing it
Section titled “Suppressing it”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_blockif (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.