Skip to content

Avoid using let else for Result #9792

@koka831

Description

@koka831

What it does

source: https://twitter.com/nick_r_cameron/status/1588205358803259392

As suggested in the OP, using let-else syntax for Result type would be a risk of overlooking proper handling of Err.
Also, refactoring, such as replacing Option types with Result is common thing.
This kind of work is sometimes overlooked, so it is worthwhile to cover in lint.

This lint will help you to notice when let-else is applied to the Result type.

Lint Name

let_else_to_result

Category

pedantic

Advantage

It detects potential risk of missing error handling

Drawbacks

No response

Example

enum MyErr { A, B }
fn foo() -> Result<String, MyErr> {
    Ok("foo".into())
}

fn bar() {
    let Ok(foo) = foo() else { return; };
}

Could be written as:

enum MyErr { A, B }
fn foo() -> Result<String, MyErr> {
    Ok("foo".into())
}

fn bar() {
    let foo = match foo() {
        Ok(foo) => foo,
        e => { return; }
    };
}

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