prefer-early-return

Group
Style
Path
lint/style/prefer-early-return
Default
Off, opt in
Origin
Falcon
Rule
349 of 512

Flags a trailing if that wraps multiple statements in a void function.

Invert the condition and return early to remove a level of indentation. Only a function body's final statement is considered, with no else, a plain expression condition, and a block with multiple statements or a nested if. Explicitly void functions, methods, operators and local functions, setters, and constructors are checked. Non-void and inferred-return functions, closures, generators, pattern conditions, and single-statement guard clauses are left alone: a bare return must not change a value-returning contract.

Invalid

example.dartdart
void top(bool ready) {
  if (ready) {
    print('ready');
    print('work');
  }
}
void afterWork(bool ready) {
  print('start');
  if (ready) {
    print('ready');
    print('work');
  }
}
class Worker {
  Worker(bool ready) {
    if (ready) {
      print('ready');
      print('work');
    }
  }
  void run(bool ready) {
    if (ready) {
      print('ready');
      print('work');
    }
  }
  set ready(bool value) {
    if (value) {
      print('ready');
      print('work');
    }
  }
}
void outer() {
  void local(bool ready) {
    if (ready) {
      print('ready');
      print('work');
    }
  }
  local(true);
}
void nested(bool first, bool second) {
  if (first) {
    if (second) { print('one'); print('two'); }
  }
}
set ready(bool value) {
  if (value) {
    print('one'); print('two');
  }
}
void asynchronous(bool value) async {
  if (value) {
    print('one'); print('two');
  }
}
class Indexed {
  void operator []=(int index, bool value) {
    if (value) {
      print(index); print(value);
    }
  }
}
Invert this condition and return early to reduce nesting.
1void top(bool ready) {
2 if (ready) {
Invert this condition and return early to reduce nesting.
8 print('start');
9 if (ready) {
Invert this condition and return early to reduce nesting.
15 Worker(bool ready) {
16 if (ready) {
Invert this condition and return early to reduce nesting.
21 void run(bool ready) {
22 if (ready) {
Invert this condition and return early to reduce nesting.
27 set ready(bool value) {
28 if (value) {
Invert this condition and return early to reduce nesting.
35 void local(bool ready) {
36 if (ready) {
Invert this condition and return early to reduce nesting.
43void nested(bool first, bool second) {
44 if (first) {
Invert this condition and return early to reduce nesting.
48set ready(bool value) {
49 if (value) {
Invert this condition and return early to reduce nesting.
53void asynchronous(bool value) async {
54 if (value) {
Invert this condition and return early to reduce nesting.
59 void operator []=(int index, bool value) {
60 if (value) {

Valid

example.dartdart
void guard(bool ready) {
  if (!ready) return;
  print('ready');
  print('work');
}
void single(bool ready) {
  if (ready) { print('one'); }
}
void withElse(bool ready) {
  if (ready) { print('one'); print('two'); }
  else { print('other'); }
}
void notLast(bool ready) {
  if (ready) { print('one'); print('two'); }
  print('end');
}
int value(bool ready) {
  if (ready) { print('one'); return 1; }
  return 0;
}
void pattern(Object value) {
  if (value case int number) { print(number); print('int'); }
}
void empty(bool ready) { if (ready) {} }
void expression(bool ready) { if (ready) print('one'); }
void inferred(bool value) {
  final callback = () {
    if (value) { print('one'); print('two'); }
  };
  callback();
}
dynamic inferredReturn(bool value) {
  if (value) { print('one'); return 1; }
}
Future<void> future(bool value) async {
  if (value) { print('one'); print('two'); }
}
Iterable<int> generator(bool value) sync* {
  if (value) { yield 1; yield 2; }
}
class Value {
  int method(bool value) {
    if (value) { print('one'); return 1; }
    return 0;
  }
}

Configure prefer-early-return

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-early-return": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/prefer-early-return: <reason>
  • prefer-equatable-mixinequatable

    Prefer EquatableMixin to directly extending package:equatable's Equatable.

  • prefer-final-fieldsprefer_final_fieldsrecommended

    Flags a private field that is initialized but never reassigned, so it could be final (prefer-final-fields, adopted from package:lints).

  • prefer-final-in-for-eachprefer_final_in_for_eachrecommended

    Prefer final for a for-each loop variable that is never reassigned.

  • prefer-final-localsprefer_final_localsrecommended

    Prefer final for local variable declarations that are never reassigned.

  • prefer-final-parametersprefer_final_parameters

    Prefer final for parameters that are never reassigned.

  • prefer-firstprefer-first

    Flags [0] index access that should use the .first getter.

  • prefer-for-in

    Flags zero-based, unit-step index loops over a syntactically known list.

  • prefer-for-loop-in-childrenflutter

    Prefers collection-for elements over eager map/toList chains in Flutter children.