Skip to main content

prefer-returning-shorthands

effort: 2m
dart 3.10+
has IDE fix
has auto-fix
starter+

Suggests returning dot shorthands from an expression function body.

Function and methods declarations already have an explicit return type and in cases when that type is the same as the returned instance, the instance can be simplified to a dot shorthand without reducing readability.

Example

❌ Bad:

// LINT: This instance type matches the return type and can be replaced with a dot shorthand.
// Try using the dot shorthand constructor.
SomeClass getInstance() => SomeClass('val');

// LINT: This instance type matches the return type and can be replaced with a dot shorthand.
// Try using the dot shorthand constructor.
SomeClass getInstance() => SomeClass.named('val');

// LINT: This instance type matches the return type and can be replaced with a dot shorthand.
// Try using the dot shorthand constructor.
SomeClass getInstance(bool flag) =>
flag ? SomeClass('value') : SomeClass.named('val');

class SomeClass {
final String value;

const SomeClass(this.value);

const SomeClass.named(this.value);
}

✅ Good:

SomeClass getInstance() => .new('val');

SomeClass getInstance() => .named('val');

SomeClass getInstance(bool flag) =>
flag ? .new('value') : .named('val');