prefer-immediate-return

Same as prefer-immediate-return in Dart Code Metrics

Path
lint/complexity/prefer-immediate-return
Default
Off, opt in
Origin
Dart Code Metrics
Rule
368 of 512

Flags a local variable that is returned on the immediately following statement.

Assigning a value to a variable only to return it on the next line adds an intermediate name that carries no information; return the expression directly. The rule fires when a single-declarator local variable declaration is immediately followed by a return of that same variable. Every statement list is checked, including closures, operators, switch cases, and finally blocks.

Invalid

example.dartdart
// Bad: unnecessary intermediate variable before return
String getName() {
  final result = compute();
  return result;
}

int getValue() {
  final value = 42;
  return value;
}

class Helper {
  Future<String> fetchData() async {
    final data = await http.get(url);
    return data;
  }

  List<String> getNames(List<User> users) {
    final names = users.map((u) => u.name).toList();
    return names;
  }
}

bool validate(String input) {
  final isValid = input.isNotEmpty && input.length > 3;
  return isValid;
}
Prefer returning the value directly instead of assigning to a variable first.
2String getName() {
3 final result = compute();
Prefer returning the value directly instead of assigning to a variable first.
7int getValue() {
8 final value = 42;
Prefer returning the value directly instead of assigning to a variable first.
13 Future<String> fetchData() async {
14 final data = await http.get(url);
Prefer returning the value directly instead of assigning to a variable first.
18 List<String> getNames(List<User> users) {
19 final names = users.map((u) => u.name).toList();
Prefer returning the value directly instead of assigning to a variable first.
24bool validate(String input) {
25 final isValid = input.isNotEmpty && input.length > 3;

Valid

example.dartdart
// Good: immediate return
String getName() {
  return compute();
}

int getValue() {
  return 42;
}

class Helper {
  Future<String> fetchData() async {
    return await http.get(url);
  }

  List<String> getNames(List<User> users) {
    return users.map((u) => u.name).toList();
  }
}

bool validate(String input) {
  return input.isNotEmpty && input.length > 3;
}

// OK: variable used multiple times
String processAndLog(String input) {
  final result = transform(input);
  log('Transformed: $result');
  return result;
}

// OK: variable used in conditional
int getCount(List<String> items) {
  final count = items.length;
  if (count == 0) {
    print('Empty');
  }
  return count;
}

// OK: variable needs intermediate computation
Future<String> getData(bool force) async {
  final result = await (force ? fetchRemote() : getLocal());
  return result.trim();
}

Configure prefer-immediate-return

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "complexity": {
        "prefer-immediate-return": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/complexity/prefer-immediate-return: <reason>
  • prefer-iterable-anyprefer_iterable_anyrecommended

    Flags iterable.where(...).isNotEmpty.

  • prefer-iterable-everyprefer_iterable_everyrecommended

    Flags iterable.where(predicate).isEmpty and filtered-length comparisons.

  • prefer-moving-to-variableprefer-moving-to-variable

    Flags repeated property/getter chains in local variable initializers.

  • prefer-null-aware-operatorsprefer_null_aware_operatorsrecommended

    Flags x == null ? null : x.y (and the inverted x != null ? x.y : null).

  • unnecessary-constunnecessary_constrecommended

    Flags a redundant const keyword inside an already-const context.

  • unnecessary-getters-settersunnecessary_getters_settersrecommended

    Flags a trivial getter/setter pair that only reads and writes a private field.

  • unnecessary-lambdasunnecessary_lambdasrecommended

    Flags a closure that only forwards its parameters to a function a tear-off could name.

  • unnecessary-overridesunnecessary_overridesrecommended

    Flags a member that does nothing but forward to super with the same arguments and resolved contract.