Skip to content

prefer_returning_shorthands

v0.3.0 Warning Fix Shorthand Patterns

Flags expression function bodies that return an instance whose type matches the declared return type. Since the return type is already explicit, the class name in the constructor call is redundant and can be replaced with a dot shorthand (e.g., .new() or .symbol()).

When a function already declares its return type, repeating the class name in the returned constructor call adds visual noise without extra information. Dot shorthands reduce this redundancy, making arrow functions more concise. This also applies to both branches of conditional expressions.

See also: Dart language - Arrow syntax

class Currency {
final String code;
const Currency(this.code);
const Currency.symbol(this.code);
}
Currency getInstance() => Currency('USD');
Currency getNamedInstance() => Currency.symbol('USD');
Currency getConditional(bool flag) =>
flag ? Currency('EUR') : Currency.symbol('USD');
Currency? getNullable() => Currency('USD');
Currency getInstance() => .new('USD');
Currency getNamedInstance() => .symbol('USD');
Currency getConditional(bool flag) =>
flag ? .new('EUR') : .symbol('USD');
// Block function bodies are not flagged:
Currency getWithBlock() {
return Currency('USD');
}
// No explicit return type — not flagged:
getInstance() => Currency('USD');
// Dynamic return type — not flagged:
dynamic getDynamic() => Currency('USD');

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

many_lints.yaml
rules:
prefer_returning_shorthands: true

To turn it off again:

many_lints.yaml
rules:
prefer_returning_shorthands: false

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