always-put-control-body-on-new-line

Same as always_put_control_body_on_new_line in Dart lints

Group
Style
Path
lint/style/always-put-control-body-on-new-line
Default
Off, opt in
Origin
Dart lints
Rule
5 of 512

Flags a control-flow body that starts on the same line as its control clause.

Putting the body of an if, else, for, while, or do on the line of its condition hides it from a reader skimming the left edge of the code. For a block body, the first statement inside the block is what must move:

if (done) return;            // flagged
while (x) { x = next(); }    // flagged
if (done) {
  return;
} else return;               // flagged
if (done) {}                 // fine: empty block

An else if chain is not reported at its else.

Invalid

example.dartdart
void f(bool ok, List<int> xs) {
  if (ok) return;

  if (ok) { print('same line'); }

  if (ok) {
    print('then');
  } else print('else');

  for (var i = 0; i < 3; i++) print(i);

  for (final x in xs) print(x);

  while (ok) ok = false;

  do print('once');
  while (false);

  do { print('block'); }
  while (false);

  if (xs case [var first]) print(first);
}
Statement should be on a separate line.
1void f(bool ok, List<int> xs) {
2 if (ok) return;
Statement should be on a separate line.
3
4 if (ok) { print('same line'); }
Statement should be on a separate line.
7 print('then');
8 } else print('else');
Statement should be on a separate line.
9
10 for (var i = 0; i < 3; i++) print(i);
Statement should be on a separate line.
11
12 for (final x in xs) print(x);
Statement should be on a separate line.
13
14 while (ok) ok = false;
Statement should be on a separate line.
15
16 do print('once');
Statement should be on a separate line.
18
19 do { print('block'); }
Statement should be on a separate line.
21
22 if (xs case [var first]) print(first);

Valid

example.dartdart
void f(bool ok, List<int> xs) {
  if (ok)
    return;

  if (ok) {
    print('then');
  } else if (xs.isEmpty) {
    print('else if is not reported at its else');
  } else {
    print('else');
  }

  if (ok) {}

  for (var i = 0; i < 3; i++)
    print(i);

  for (final x in xs) {
    print(x);
  }

  while (ok) {
    ok = false;
  }

  do {
    print('once');
  } while (false);

  if (ok) // comment
    print('body on the next line');
}

Configure always-put-control-body-on-new-line

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "always-put-control-body-on-new-line": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/always-put-control-body-on-new-line: <reason>