Skip to content

option_if_let_else results in code that doesn't compile #12723

@SUPERCILEX

Description

@SUPERCILEX

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

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