rust-lang / rust-lang/rust-clippy

Lint to suggest when let-else can be used to remove a level of indentation

Open
#9,807 3 comments 2 reactions 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

With the recent stabilization of let-else, blocks of code can now exit or unwrap a variable into the enclosing scope without having to put the non-exiting logic in an extra level of indentation. Until now, we had to either do an ugly manual let-else or indent the code. It would be nice to know all the places where that indent occurred in our codebases which can now be flatted out with a let-else.

Lint Name

flatten_with_let_else

Category

style

Advantage
  • Improves readability of code by avoiding levels of indentation when it would be unnecessary
  • Helps clean up existing codebases by listing all places where let-else can now be employed
Drawbacks

No response

Example

Based on the example from the Rust 1.65 announcement. This lint would apply twice to the following code, for the inner if-else and the outer match. This is just one example, and isn't exhaustive of all cases (for example, other diverging cases like break, return, and ? exist plus surely many others that I'm not experienced in Rust enough to list off). The diverging case might also be more than a single expression, and I suppose a decent heuristic would be to suggest this lint if the non-diverging case contains more lines of code than the diverging case.

fn get_count_item(s: &str) -> (u64, &str) {
    let mut it = s.split(' ');
    match (it.next(), it.next()) {
        (Some(count_str), Some(item)) => {
            if let Ok(count) = u64::from_str(count_str) {
                (count, item)
            } else {
                panic!("Can't parse integer: '{count_str}'");
            }
        }
        _ => panic!("Can't segment count item pair: '{s}'"),
    }
}
assert_eq!(get_count_item("3 chairs"), (3, "chairs"));

Could be written as:

fn get_count_item(s: &str) -> (u64, &str) {
    let mut it = s.split(' ');
    let (Some(count_str), Some(item)) = (it.next(), it.next()) else {
        panic!("Can't segment count item pair: '{s}'");
    };
    let Ok(count) = u64::from_str(count_str) else {
        panic!("Can't parse integer: '{count_str}'");
    };
    (count, item)
}
assert_eq!(get_count_item("3 chairs"), (3, "chairs"));

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 by reviewing the existing manual_let_else lint referenced in the issue and the Rust 1.65 let-else example. Define the supported diverging patterns and heuristic from the issue, then add the flatten_with_let_else lint with coverage for the proposed transformations and verify that its suggestions preserve behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.