curly-braces-in-flow-control-structures

Same as curly_braces_in_flow_control_structures in Dart lints

Group
Style
Path
lint/style/curly-braces-in-flow-control-structures
Default
On (recommended)
Origin
Dart lints
Rule
212 of 512
recommended

Requires curly braces around the bodies of flow-control statements.

A brace-less body invites the "goto fail" class of bug, where a later edit adds a second statement that silently falls outside the branch. Requiring blocks around for, while, do, and if/else bodies keeps the scope explicit and edits safe. Two carve-outs match the official lint: an if with no else whose body sits on the same line as its condition may omit braces, and an else if chain need not wrap the intermediate if in a block.

Invalid

example.dartdart
void f1(bool x) {
  while (x) doThing();
}

void f2() {
  for (var i = 0; i < 3; i++) doThing();
}

void f3(bool x) {
  do doThing();
  while (x);
}

void f4(bool a) {
  if (a)
    doThing();
}

void f5(bool a) {
  if (a)
    doThing();
  else
    doOther();
}

void f6(bool a, List<int> xs) {
  for (final x in xs) print(x);
}

// Closure argument of a constructor invocation (`Expr::New`).
Widget f7(bool x) {
  return new ElevatedButton(onPressed: () {
    while (x) doThing();
  });
}

// Closure nested inside a collection literal.
List<VoidCallback> f8(bool x) {
  return [
    () {
      if (x)
        doThing();
    },
  ];
}

void commentDelimiter(bool x) {
  if (x)
    /* ) */ doThing();
}
Use curly braces around the body of this flow-control statement.
1void f1(bool x) {
2 while (x) doThing();
Use curly braces around the body of this flow-control statement.
5void f2() {
6 for (var i = 0; i < 3; i++) doThing();
Use curly braces around the body of this flow-control statement.
9void f3(bool x) {
10 do doThing();
Use curly braces around the body of this flow-control statement.
15 if (a)
16 doThing();
Use curly braces around the body of this flow-control statement.
20 if (a)
21 doThing();
Use curly braces around the body of this flow-control statement.
22 else
23 doOther();
Use curly braces around the body of this flow-control statement.
26void f6(bool a, List<int> xs) {
27 for (final x in xs) print(x);
Use curly braces around the body of this flow-control statement.
32 return new ElevatedButton(onPressed: () {
33 while (x) doThing();
Use curly braces around the body of this flow-control statement.
41 if (x)
42 doThing();
Use curly braces around the body of this flow-control statement.
48 if (x)
49 /* ) */ doThing();

Valid

example.dartdart
void g1(bool x) {
  while (x) {
    doThing();
  }
}

void g2() {
  for (var i = 0; i < 3; i++) {
    doThing();
  }
}

void g3(bool x) {
  do {
    doThing();
  } while (x);
}

void g4(bool a) {
  if (a) doThing();
}

void g5(bool a, bool b) {
  if (a) {
    doThing();
  } else if (b) {
    doOther();
  } else {
    doThird();
  }
}

void g6(List<int> xs) {
  for (final x in xs) {
    print(x);
  }
}

Configure curly-braces-in-flow-control-structures

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "curly-braces-in-flow-control-structures": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/curly-braces-in-flow-control-structures: <reason>
  • dangling-library-doc-commentsdangling_library_doc_commentsrecommended

    Attach library documentation to an explicit library directive.

  • deprecated-consistencydeprecated_consistencyrecommended

    Flags deprecations that are not carried through to related declarations.

  • directives-orderingdirectives_ordering

    Flags import and export directives that break the Effective Dart ordering.

  • do-not-use-environmentdo_not_use_environment

    Disallow reading compilation environment declarations, including type aliases.

  • double-literal-formatdouble-literal-format

    Flags double literals written with redundant or missing digits.

  • empty-constructor-bodiesempty_constructor_bodiesrecommended

    Flags constructors with an empty block body {}.

  • enum-constants-ordering

    Flags enum constants whose declared names violate the configured order.

  • eol-at-end-of-fileeol_at_end_of_filerecommended

    Flags a non-empty file that does not end with exactly one line break.