Skip to content

prefer_returning_condition

v1.0.0 Warning Control Flow

This rule flags an if that returns true with a following return false (or the reverse).

if (x > 0) return true; return false; is the condition itself, spelled out in three lines. return x > 0; says it once, and the reader does not have to check that the two branches really are opposites — which is exactly the check that gets skipped when one of them is later edited.

Both branches returning the same literal is a different mistake, reported by function_always_returns_same_value. A pattern case (if (x case ...)) is skipped, since it binds variables the returned expression may use.

bool isEligible(Player player) {
if (player.rating > 1200) {
return true;
}
return false;
}
bool isEligible(Player player) => player.rating > 1200;

This rule is in the opinionated preset.

To disable this rule:

many_lints.yaml
rules:
prefer_returning_condition: false

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