rust-lang / rust-lang/rust-clippy
Lint to suggest when let-else can be used to remove a level of indentation
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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