Skip to content

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.

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);
}

This rule is in the opinionated preset.

To disable this rule:

many_lints.yaml
rules:
avoid_unnecessary_return: false

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