avoid-redundant-else

Group
Style
Path
lint/style/avoid-redundant-else
Default
Off, opt in
Origin
Falcon
Rule
122 of 512

Flags else branches after an if body that unconditionally returns or throws.

The then branch must directly end in return, throw, or rethrow. Keep the else body's scope when moving it after the if. Identical branches belong to no-equal-then-else; a trailing bare return belongs to avoid-unnecessary-return instead, because removing that return is the smaller simplification. Only if statements directly inside blocks are checked, not nested unbraced branches or else-if links, where moving statements can change control flow.

Invalid

example.dartdart
int returning(bool ready) {
  if (ready) { return 1; }
  else { print('other'); }
  return 2;
}
void early(bool ready) {
  if (ready) { return; }
  else { print('other'); }
  print('after');
}
int unbraced(bool ready) {
  if (ready) return 1;
  else return 2;
}
void throwing(bool ready) {
  if (ready) { throw StateError('stop'); }
  else { print('other'); }
}
void rethrowing(bool ready) {
  try { throw StateError('stop'); } catch (error) {
    if (ready) { rethrow; }
    else { print(error); }
  }
}
int comments(bool ready) {
  if (ready) { return 1; /* explanation */ }
  else { return 1; }
}
int exitingChain(bool first, bool second) {
  if (first) return 1;
  else if (second) return 2;
  else return 3;
}
This else is redundant after an exiting branch; move its body after the if.
2 if (ready) { return 1; }
3 else { print('other'); }
This else is redundant after an exiting branch; move its body after the if.
7 if (ready) { return; }
8 else { print('other'); }
This else is redundant after an exiting branch; move its body after the if.
12 if (ready) return 1;
13 else return 2;
This else is redundant after an exiting branch; move its body after the if.
16 if (ready) { throw StateError('stop'); }
17 else { print('other'); }
This else is redundant after an exiting branch; move its body after the if.
21 if (ready) { rethrow; }
22 else { print(error); }
This else is redundant after an exiting branch; move its body after the if.
26 if (ready) { return 1; /* explanation */ }
27 else { return 1; }
This else is redundant after an exiting branch; move its body after the if.
30 if (first) return 1;
31 else if (second) return 2;

Valid

example.dartdart
void work(bool ready) {
  if (ready) { print('first'); } else { print('second'); }
}
void trailing(bool ready) {
  if (ready) { return; } else { print('second'); }
}
void conditional(bool ready, bool other) {
  if (ready) { if (other) return; } else { print('second'); }
  print('after');
}
int identical(bool ready) {
  if (ready) { return 1; } else { return 1; }
}
void noElse(bool ready) {
  if (ready) return;
  print('after');
}
void loop(bool ready) {
  while (ready) {
    if (ready) { break; } else { print('second'); }
  }
}
void chain(bool first, bool second) {
  if (first) { print('first'); }
  else if (second) { return; }
  else { print('third'); }
  print('after');
}
int nestedUnbraced(bool first, bool second) {
  if (first) if (second) return 1; else return 2;
  return 3;
}
void loopUnbraced(bool ready) {
  while (ready) if (ready) return; else print('step');
}

Configure avoid-redundant-else

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-redundant-else": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/avoid-redundant-else: <reason>