Rules / Style / curly-braces-in-flow-control-structures

curly-braces-in-flow-control-structures

lint/style/curly-braces-in-flow-control-structures
recommended
Same as curly_braces_in_flow_control_structures · Dart lints

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);
}
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);

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);
  }
}

How to configure

Set the severity of curly-braces-in-flow-control-structures in your falcon.json:

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