Skip to content

proposal: no_else_after_return #59148

Open
@goderbauer

Description

@goderbauer

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packagelinter-lint-proposallinter-status-pendingtype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions