Control structure body is not wrapped in curly braces

ID

dart.maintainability.control_statement_braces

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Readability

Language

Dart

Description

Reports an if, else, for or while whose body is a bare statement on the LINE BELOW the header, rather than a block. A body on the same line as its header is not reported: Dart’s own style guide allows a one-line if with no else, and the same shape is how a collection-if is written inside a widget list (if (cond) Widget(),), which cannot take braces at all - measured over 8 real Flutter applications, reporting it was the bulk of this rule’s output and none of it was a defect. The body must also be a statement, ending in ;. A do/while, an else if that opens a block, an Allman-style brace on its own line and a condition split across lines are not reported.

Rationale

Without braces only the first statement belongs to the control structure, and nothing in the layout says so. Adding a second line under an unbraced if silently moves it outside the condition - it stays indented, it looks guarded, and it runs unconditionally. The same shape makes a dangling else bind to the wrong if and makes every diff that touches the body larger than it needs to be, because the braces have to be added before the line can be.

The following code illustrates the pattern detected by this rule:

class Guard {
  int limit = 0;

  void check(int value, bool flag, List<int> items) {
    if (value > limit) report(value);

    // FLAGGED: Control structure body is not wrapped in curly braces
    if (flag)
      report(value);

    while (value > 0) value--;

    for (final item in items) report(item);

    // FLAGGED: Control structure body is not wrapped in curly braces
    for (var i = 0; i < items.length; i++)
      report(items[i]);

    if (flag) {
      report(value);
    } else report(0);

    if (flag) {
      report(value);
    } else if (value > 0) report(1);
  }

  void report(int v) {}
}

Remediation

Wrap the body in { }, keeping the opening brace on the header line as Dart formatting does. Where the intent was a one-line guard, an early return, continue or break still reads well braced; a conditional value is usually better expressed with the ?: operator or a ?? fallback than with an unbraced if.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved