prefer_returning_shorthands
v0.3.0 Warning Fix Shorthand Patterns
This rule flags an arrow function that constructs the very type it declares as its return type. The declared type already names the class, so .new(...) or .named(...) carries the same information in fewer characters. The quick fix drops the prefix.
See also: Dart language — dot shorthands
class Money { const Money(this.amount); const Money.zero() : amount = 0;
final int amount;}
Money parsePrice(String raw) => Money(int.parse(raw)); // LINT
Money emptyCart() => Money.zero(); // LINT
Money priceOr(String? raw, bool free) => free ? Money.zero() : Money(int.parse(raw!)); // LINT twiceMoney parsePrice(String raw) => .new(int.parse(raw));
Money emptyCart() => .zero();
Money priceOr(String? raw, bool free) => free ? .zero() : .new(int.parse(raw!));Both branches of a conditional are checked independently, so a mixed expression reports only the branches that construct the return type.
A nullable return type still counts
Section titled “A nullable return type still counts”Money? accepts a Money, so the shorthand is available there too:
// Don'tMoney? tryParse(String raw) => Money(int.parse(raw));
// DoMoney? tryParse(String raw) => .new(int.parse(raw));Methods and factory constructors
Section titled “Methods and factory constructors”The rule reads a method’s declared return type the same way, and treats a factory constructor’s class as its return type:
// Don'tclass Money { const Money(this.amount); const Money._(this.amount);
factory Money.fromCents(int cents) => Money._(cents ~/ 100); // LINT
final int amount;
Money doubled() => Money(amount * 2); // LINT}
// Doclass Money { const Money(this.amount); const Money._(this.amount);
factory Money.fromCents(int cents) => ._(cents ~/ 100);
final int amount;
Money doubled() => .new(amount * 2);}Known limitations
Section titled “Known limitations”Only arrow bodies. A block body is not reported, even when its single return constructs the declared type:
// Not reportedMoney parsePrice(String raw) { return Money(int.parse(raw));}A declared return type is required. With no annotation, or with dynamic or void, there is no context type and the shorthand would not compile — nothing is reported.
Only the outermost expression. Money('1').copy() returns a Money but the expression is a method call on a constructor, so it is left alone.
Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_returning_shorthands: trueTo turn it off again:
rules: prefer_returning_shorthands: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_nested_shorthands— Avoid nesting a dot shorthand inside another dot shorthand invocation.prefer_shorthands_with_constructors— Use dot shorthand constructors for common Flutter classes.prefer_shorthands_with_enums— Use dot shorthands instead of explicit enum prefixes.prefer_shorthands_with_static_fields— Use dot shorthands instead of explicit class prefixes for static fields.