rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
10.89k stars 1.46k forks source link

option_if_let_else results in code that doesn't compile #12723

Open SUPERCILEX opened 2 weeks ago

SUPERCILEX commented 2 weeks ago

Summary

The lint triggers even when both sides of the conditional use the same variable and consume it.

Lint Name

option_if_let_else

Reproducer

I tried this code:

if let Ok(data) = str::from_utf8(&loaded) {
    ExportData::Human(data.into())
} else {
    ExportData::Bytes(loaded.into_inner())
}

I saw this happen:

str::from_utf8(&loaded).map_or_else(|_| ExportData::Bytes(loaded.into_inner()), |data| ExportData::Human(data.into()))
// Doesn't compile: cannot move out of `loaded` because it is borrowed [E0505] move out of `loaded` occurs here

Version

rustc 1.79.0-nightly (3a36386dc 2024-04-25)
binary: rustc
commit-hash: 3a36386dc1075018dc7ca2640a2656adb31a61fe
commit-date: 2024-04-25
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.4

Additional Labels

No response