avoid-unnecessary-return

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

Flags bare returns at the end of a function or its final if/block branches.

Falling through already exits the function. Only paths through final block statements and if branches are followed; loops, switch, try/finally, and value-returning statements are deliberately excluded. Nested functions have their own boundary, so their returns cannot escape into an enclosing body. Identical if/else branches are left to no-equal-then-else.

Invalid

example.dartdart
void direct() {
  print('work');
  return;
}
void branch(bool ready) {
  if (ready) {
    return;
  }
}
void branches(bool ready) {
  if (ready) {
    return;
  } else {
    print('work');
    return;
  }
}
class Example {
  void method() {
    return;
  }
  set value(int value) {
    print(value);
    return;
  }
}
void closures() {
  final callback = () {
    return;
  };
  callback();
}
void local() {
  void nested() {
    return;
  }
  nested();
}
class Constructed {
  Constructed() {
    return;
  }
}
This trailing return is unnecessary; let the function finish naturally.
2 print('work');
3 return;
This trailing return is unnecessary; let the function finish naturally.
6 if (ready) {
7 return;
This trailing return is unnecessary; let the function finish naturally.
11 if (ready) {
12 return;
This trailing return is unnecessary; let the function finish naturally.
14 print('work');
15 return;
This trailing return is unnecessary; let the function finish naturally.
19 void method() {
20 return;
This trailing return is unnecessary; let the function finish naturally.
23 print(value);
24 return;
This trailing return is unnecessary; let the function finish naturally.
28 final callback = () {
29 return;
This trailing return is unnecessary; let the function finish naturally.
34 void nested() {
35 return;
This trailing return is unnecessary; let the function finish naturally.
40 Constructed() {
41 return;

Valid

example.dartdart
void guard(bool ready) {
  if (ready) return;
  print('work');
}
int value() { return 1; }
void loop(bool ready) {
  while (ready) { return; }
}
void counted() {
  for (var i = 0; i < 3; i++) { return; }
}
void handling() {
  try { return; } finally { print('cleanup'); }
}
void selection(int value) {
  switch (value) {
    case 1: return;
    default: print('other');
  }
}
void nested(bool ready) {
  if (ready) {
    if (ready) return;
    print('work');
  }
}
void identical(bool ready) {
  if (ready) { return; } else { return; }
}
void identicalWork(bool ready) {
  if (ready) { print('work'); return; }
  else { print('work'); return; }
}

Configure avoid-unnecessary-return

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

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

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

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