Skip to content

New Lint: Suggest replacing early-return let ... else with if let #17767

Description

@alisterd51

What it does

Detects let ... else { return; } statements where the remaining function body could be enclosed in a clearer if let block (or if let chain).

Advantage

  • Makes the execution scope and intent clearer by explicitly grouping statements that depend on the matched values.
  • Reduces boilerplate and vertical space when wrapping short tail logic or function calls.

Drawbacks

  • Combining multiple conditions (if let A && let B) relies on the let_chains feature, which may not be stabilized/available on all Rust toolchains.
  • Increases rightward drift (indentation) for longer function bodies.

Example

let Some(foo) = foo() else {
    return;
};
other(foo);
let Some(foo) = foo() else {
    return;
};
let Some(bar) = bar() else {
    return;
};
other(foo, bar);

Could be written as:

if let Some(foo) = foo() {
    other(foo);
}
if let Some(foo) = foo()
    && let Some(bar) = bar() {
    other(foo, bar);
}

Comparison with existing lints

Unlike clippy::manual_let_else (which targets match or if let value assignments), this lint targets let ... else { return; } guard statements and wraps subsequent code in if let scopes.

Additional Context

deally, the restriction lint group could offer two complementary lints so that teams can enforce their preferred coding convention: one favoring if let scoping, and another favoring let ... else guard clauses.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions