Skip to content

function_always_returns_same_value

v1.0.0 Warning Code Quality

This rule flags a function where every return yields the same constant, so the branching around them decides nothing.

Whatever the caller passes, the answer is fixed. Either a branch was meant to return something else and does not — the usual case, and a silent one — or the function should be a constant and its parameters dropped.

Only literal constants are compared, and only when there are at least two returns; one return of a constant is an ordinary function. A return; with no value, or a return inside a nested closure, means the rule cannot prove one fixed answer and stays quiet.

Some callbacks are supposed to return the same value on every path, because the value is a signal to a framework rather than an answer. onNotification must return false throughout to let a notification keep bubbling; the method exists for its side effect.

Two checks cover them: a set of known names (onNotification, shouldRepaint, …) plus any on... method, and — because a protocol callback can be given a descriptive name — the shape, where any parameter typed ...Notification marks the method as a listener.

int scoreFor(Player player) {
if (player.isWinner) return 3;
return 3; // the branch changes nothing
}
int scoreFor(Player player) {
if (player.isWinner) return 3;
return 0;
}

This rule is in the recommended preset, so it is on with preset: recommended or preset: opinionated.

To disable this rule:

many_lints.yaml
rules:
function_always_returns_same_value: false

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