rust-lang / rust-lang/rust-clippy

Collapsing double `if let Some(…)` should also suggest `and_then()`

Open
#15,769 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

This is a request to extend the collapsible_if lint to also suggest using the and_then() function for scenarios where two if let Some(…) constructs would be collapsed into one if statement:

fn main() {

	// A double nested datastructure that will be something more complex in real code i.e. a regex capture
	let thingy: Option<Option<u8>> = Some(Some(1));

	if let Some(thing) = thingy {
		if let Some(value) = thing {
			println!("Value: {value}");
		}
	}

	// What clippy suggests
	if let Some(thing) = thingy
		&& let Some(value) = thing {
			println!("Value: {value}");
		}

	// What clippy should (also) suggest when unwrapping two `Option` types
	if let Some(value) = thingy
		.and_then(|thing| thing) {
			println!("Value: {value}");
		}
	
}

Note that in more complex code the placeholders thingy and thing are most likely not options themselves, but some datastructures that have a method called that results in an Option.

This might also be applicable to Results.

This applies to clippy 0.1.92 (54a8a1db60 2025-09-26)

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 existing collapsible_if lint referenced in the issue and reproduce the nested Option example. Determine whether the requested and_then() suggestion applies beyond Option, including the noted Result possibility. Done means the lint handles the intended nested cases without changing the existing && let suggestion.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.