Skip to main content

avoid_empty_else

Empty statements are not allowed in an 'else' clause.

Description

#

The analyzer produces this diagnostic when the statement after an else is an empty statement (a semicolon).

For more information, see the documentation for avoid_empty_else.

Example

#

The following code produces this diagnostic because the statement following the else is an empty statement:

dart
void f(int x, int y) {
  if (x > y)
    print("1");
  else ;
    print("2");
}

Common fixes

#

If the statement after the empty statement is intended to be executed only when the condition is false, then remove the empty statement:

dart
void f(int x, int y) {
  if (x > y)
    print("1");
  else
    print("2");
}

If there is no code that is intended to be executed only when the condition is false, then remove the whole else clause:

dart
void f(int x, int y) {
  if (x > y)
    print("1");
  print("2");
}