Skip to content

prefer_immediate_return

v0.8.0 Warning Fix Code Quality

This rule flags a local variable that is declared and then returned on the very next line, with no other use.

The variable adds a name but no information — the return statement already says what the value is for. It also adds a line that has to be kept in sync: rename the variable and two places change instead of none.

Future<User> loadUser(String id) async {
final user = await repository.fetchUser(id);
return user;
}
Future<User> loadUser(String id) async {
return repository.fetchUser(id);
}

The rule reports only when the variable is provably throwaway:

  • It is the second-to-last statement, directly followed by return name;.
  • The declaration declares exactly one variable — var a = 1, b = 2; is skipped.
  • It has an initializer and is not late.
  • The returned identifier resolves to that declaration, not a field or outer variable with the same name.
  • The variable is referenced exactly once in the whole function body — the return itself.

A variable kept deliberately as documentation (final isEligible = ...; return isEligible;) will be reported. If the name earns its place, suppress the diagnostic on that line.

This rule is in the opinionated preset, so it is on with preset: opinionated, or by name:

many_lints.yaml
rules:
prefer_immediate_return: true

To turn it off again:

many_lints.yaml
rules:
prefer_immediate_return: false

To keep the rule on but skip certain paths, use per-rule exclude.