rust-lang / rust-lang/rust-clippy
`option_if_let_else` does not take into account automatic temporary lifetime extension
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
The lint doesn't take into account the automatic temporary lifetime extension feature of if-else that was added in Rust 1.79.0. It suggests using the closure syntax that does not support such temporary lifetime extension, and thus would fail to compile.
Lint Name
option_if_let_else
Reproducer
I tried this code:
fn example(input: Option<usize>) -> String {
let string: &dyn std::fmt::Display = if let Some(input) = input {
&format!("input: {input}")
} else {
&"{none}"
};
format!("Example: {string}")
}
I saw this happen:
warning: use Option::map_or instead of an if let/else
--> crates/sandbox/src/main.rs:2:42
|
2 | let string: &dyn std::fmt::Display = if let Some(input) = input {
| __________________________________________^
3 | | &format!("input: {input}")
4 | | } else {
5 | | &"{none}"
6 | | };
| |_____^ help: try: `input.map_or(&"{none}", |input| &format!("input: {input}"))`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#option_if_let_else
= note: requested on the command line with `-W clippy::option-if-let-else`
I expected to see this happen:
No lint should be produced
Version
rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1
Additional Labels
@rustbot label +I-suggestion-causes-error
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 locating the implementation and regression tests for the option_if_let_else lint, then run the provided Rust reproducer with the lint enabled. The fix is done when this automatic temporary lifetime extension case produces no lint, while existing valid suggestions remain covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100