rust-lang / rust-lang/rust-clippy

Avoid using `let else` for `Result`

Open
#9,792 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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; }
    };
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the issue's Rust examples and the proposed lint name, let_else_to_result. Determine the cases where let-else matches a Result and define tests showing the lint reports them; done means the lint detects the described pattern without flagging unrelated let-else usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.