avoid-empty-else

Same as avoid_empty_else in Dart lints

Path
lint/suspicious/avoid-empty-else
Default
On (recommended)
Origin
Dart lints
Rule
59 of 512
recommended

Flags an else clause whose body is an empty statement (else ;).

An else immediately followed by a semicolon binds that empty statement as the whole else branch, so the block meant to run instead executes unconditionally right after the if. This is nearly always a stray semicolon typed between else and the intended { ... }. Remove the semicolon so the following block becomes the else body, or delete the else entirely if no alternative branch is needed.

Invalid

example.dartdart
void f1(bool a) {
  if (a) {
    print('a');
  } else ;
}

void f2(int n) {
  if (n > 0) print('pos'); else ;
}

void f3(bool a) {
  if (a) {
    print('a');
  } else
    ;
}

void f4(bool a, bool b) {
  if (a) {
    print('a');
  } else if (b) {
    print('b');
  } else ;
}

void f5(bool a) {
  while (a) {
    if (a) {
      print('x');
    } else ;
  }
}

void f6(bool a) {
  if (a) print('y'); else ;
}
Remove the empty `else` clause or give it a body.
3 print('a');
4 } else ;
Remove the empty `else` clause or give it a body.
7void f2(int n) {
8 if (n > 0) print('pos'); else ;
Remove the empty `else` clause or give it a body.
14 } else
15 ;
Remove the empty `else` clause or give it a body.
22 print('b');
23 } else ;
Remove the empty `else` clause or give it a body.
29 print('x');
30 } else ;
Remove the empty `else` clause or give it a body.
34void f6(bool a) {
35 if (a) print('y'); else ;

Valid

example.dartdart
void g1(bool a) {
  if (a) {
    print('a');
  } else {
    print('b');
  }
}

void g2(int n) {
  if (n > 0) {
    print('pos');
  }
}

void g3(bool a, bool b) {
  if (a) {
    print('a');
  } else if (b) {
    print('b');
  } else {
    print('c');
  }
}

void g4(bool a) {
  if (a) print('y');
}

void g5(bool a) {
  if (a) {
    print('a');
  }
}

void g6(bool a) {
  while (a) {
    if (a) {
      print('x');
    } else {
      print('y');
    }
  }
}

Configure avoid-empty-else

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "avoid-empty-else": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/suspicious/avoid-empty-else: <reason>