Open
Description
no_else_after_return
Description
Move the code of the else branch below the if
block.
Details
When the if
case always ends with a return
, the else branch is not necessary and the code can instead be placed below the if
block. This decreases code complexity and indentation and overall improves readability.
Kind
style advice, maybe?
Bad Examples
if (foo) {
// lots of other code...
return 'foo';
} else {
return 'not foo';
}
Good Examples
if (foo) {
// lots of other code...
return 'foo';
}
return 'not foo';
Discussion
Real world example: flutter/flutter#126935 (comment)
Discussion checklist
- List any existing rules this proposal modifies, complements, overlaps or conflicts with.
- List any relevant issues (reported here, the SDK Tracker, or elsewhere).
- If there's any prior art (e.g., in other linters), please add references here.
- If this proposal corresponds to Effective Dart or Flutter Style Guide advice, please call it out. (If there isn't any corresponding advice, should there be?)
- If this proposal is motivated by real-world examples, please provide as many details as you can. Demonstrating potential impact is especially valuable.