Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option_if_let_else generates wrong suggestion for Results #10335

Open
EliasHolzmann opened this issue Feb 12, 2023 · 2 comments · Fixed by #10337 · May be fixed by #14429
Open

option_if_let_else generates wrong suggestion for Results #10335

EliasHolzmann opened this issue Feb 12, 2023 · 2 comments · Fixed by #10337 · May be fixed by #14429
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@EliasHolzmann
Copy link
Contributor

EliasHolzmann commented Feb 12, 2023

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

@EliasHolzmann EliasHolzmann added the C-bug Category: Clippy is not doing the correct thing label Feb 12, 2023
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Feb 12, 2023
@EliasHolzmann
Copy link
Contributor Author

@rustbot claim

@dgulotta
Copy link

It looks like this is broken again. The code in the original report no longer triggers a warning, but here is a new example that triggers the issue.

#![warn(clippy::option_if_let_else)]
pub fn test(r: Result<&[u8],&[u8]>) -> Vec<u8>  {
    match r {
        Ok(s) => s.to_owned(),
        Err(_) => Vec::new(),
    }
}

pub fn test2(r: Result<&[u8],&[u8]>) -> Vec<u8>  {
    if let Ok(s) = r { s.to_owned() }
    else { Vec::new() }
}

These both result in the suggestion:

try: `r.map_or_else(Vec::new, |s| s.to_owned())`

Playground link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
4 participants