avoid_unnecessary_return
v1.0.0 Warning Control Flow
This rule flags a bare return; written as the last statement of a function that returns nothing, where control leaves the function whether it is there or not.
Why use this rule
Section titled “Why use this rule”The statement changes nothing, but it does not read as though it changes nothing. return announces an early exit, so a reader stops to look for what is being skipped, and finds the closing brace.
It is usually a leftover from a change that moved or deleted the statements it once guarded. An early return; that genuinely skips later code is doing real work and is left alone.
void process(Order order) { send(order); return; // nothing follows}void process(Order order) { send(order);}An early return stays:
void process(Order order) { if (order.isCancelled) return; // skips the call below send(order);}Turning this rule off
Section titled “Turning this rule off”This rule is in the opinionated preset.
To disable this rule:
rules: avoid_unnecessary_return: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_early_return— Replace a body-wrapping if with an early-return guard.prefer_return_await— Detect missing await on returned Futures inside try-catch.avoid_cascade_after_if_null— Detect cascades after if-null operators without parentheses.avoid_collapsible_if— Merge nested if statements with &&.