avoid-unnecessary-block

Group
Style
Path
lint/style/avoid-unnecessary-block
Default
Off, opt in
Origin
Falcon
Rule
155 of 512

Flags standalone nested blocks that introduce no lexical declarations.

Control-flow and function bodies retain their braces. A block containing a variable, pattern, or local function declaration retains its lexical scope.

Invalid

example.dartdart
void expressions() {
  { print('nested'); }
}

void empty() {
  {}
}

int returning() {
  { return 1; }
}

void multiple() {
  { print('first'); print('second'); }
}

void conditional(bool ready) {
  { if (ready) { print('ready'); } }
}

void scopedChild() {
  {
    { final value = 1; print(value); }
  }
}
Remove this unnecessary block without changing the enclosed statements.
1void expressions() {
2 { print('nested'); }
Remove this unnecessary block without changing the enclosed statements.
5void empty() {
6 {}
Remove this unnecessary block without changing the enclosed statements.
9int returning() {
10 { return 1; }
Remove this unnecessary block without changing the enclosed statements.
13void multiple() {
14 { print('first'); print('second'); }
Remove this unnecessary block without changing the enclosed statements.
17void conditional(bool ready) {
18 { if (ready) { print('ready'); } }
Remove this unnecessary block without changing the enclosed statements.
21void scopedChild() {
22 {

Valid

example.dartdart
void variables() {
  final value = 1;
  { final value = 2; print(value); }
  print(value);
}

void patterns() {
  { var (first, second) = (1, 2); print(first + second); }
}

void functions() {
  { void local() {} local(); }
}

void controlBodies(bool ready) {
  if (ready) { print('ready'); } else { print('waiting'); }
  while (ready) { break; }
  for (var i = 0; i < 2; i++) { print(i); }
  do { print('once'); } while (false);
}

void exceptionBodies() {
  try { print('try'); } catch (error) { print(error); } finally { print('finally'); }
}

void labels() {
  exit: { print('labeled'); break exit; }
}

void laterDeclaration() {
  { print('before'); final value = 1; print(value); }
}

Configure avoid-unnecessary-block

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-unnecessary-block": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/avoid-unnecessary-block: <reason>