Skip to content

option_if_let_else generates wrong suggestion for Results #10335

@EliasHolzmann

Description

@EliasHolzmann

Summary

The suggestions generated by the option_if_let_else lint for a Result with an impure expression in the else branch does not compile.

Reproducer

I tried this code:

#![deny(clippy::option_if_let_else)]
fn main() {
    let variable: Result<u32, &str> = Ok(42);
    if let Ok(binding) = variable {
        println!("Ok {binding}");
    } else {
        println!("Err");
    }
}

Playground

I expected Clippy to suggest the following fix:

variable.map_or_else(|_| {
    println!("Err");
}, |binding| {
    println!("Ok {binding}");
})

Instead, Clippy outputs this (notice the missing _ in the arguments to the first closure):

[...]
help: try
  |
4 ~     variable.map_or_else(|| {
5 +         println!("Err");
6 +     }, |binding| {
7 +         println!("Ok {binding}");
8 +     })
  |

Version

rustc 1.67.1 (d5a82bbd2 2023-02-07)
binary: rustc
commit-hash: d5a82bbd26e1ad8b7401f6a718a9c57c96905483
commit-date: 2023-02-07
host: x86_64-unknown-linux-gnu
release: 1.67.1
LLVM version: 15.0.6

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions