Rules / Suspicious / avoid-empty-else

avoid-empty-else

lint/suspicious/avoid-empty-else
recommended
Same as avoid_empty_else · Dart lints

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 ;
}
Empty `else` clause — remove the `else` or give it a body
3 print('a');
4 } else ;
Empty `else` clause — remove the `else` or give it a body
7void f2(int n) {
8 if (n > 0) print('pos'); else ;
Empty `else` clause — remove the `else` or give it a body
14 } else
15 ;
Empty `else` clause — remove the `else` or give it a body
22 print('b');
23 } else ;
Empty `else` clause — remove the `else` or give it a body
29 print('x');
30 } else ;
Empty `else` clause — remove the `else` 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');
    }
  }
}

How to configure

Set the severity of avoid-empty-else in your falcon.json:

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