Skip to content

option_if_let_else does not take into account automatic temporary lifetime extension #15142

Open
@Veetaha

Description

@Veetaha

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-nurseryLint: Currently in the nursery group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions