Rules / Complexity / prefer-immediate-return

prefer-immediate-return

lint/complexity/prefer-immediate-return
recommended
Same as prefer-immediate-return · Dart Code Metrics

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, and it descends into blocks and if, loop, and try/catch bodies to check nested statements.

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();
}

How to configure

Set the severity of prefer-immediate-return in your falcon.json:

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