Skip to main content

prefer-immediate-return

has auto-fix
free+

Warns when a method or a function returns a variable declared right before the return statement.

Declaring a local variable only to immediately return it can be considered bad practice. The name of a declaration with its return type should give enough information about what it returns.

Example

❌ Bad:

void calculateSum(int a, int b) {
final sum = a + b;
// LINT: Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.
return sum;
}

void calculateArea(int width, int height) {
final result = width * height;
// LINT: Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.
return result;
}

✅ Good:

void calculateSum(int a, int b) {
return a + b;
}

void calculateArea(int width, int height) => width * height;