prefer-immediate-return
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;
return sum; // LINT: Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.
}
void calculateArea(int width, int height) {
final result = width * height;
return result; // LINT: Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.
}
✅ Good:
void calculateSum(int a, int b) {
return a + b;
}
void calculateArea(int width, int height) => width * height;