Skip to content

option_if_let_else suggests map_or instead of map_or_else even if else branch contains a function call #5821

Open
@Arnavion

Description

@Arnavion
#[deny(clippy::option_if_let_else)]
pub fn foo(o: Option<i32>, f: impl FnOnce(i32), g: impl FnOnce()) {
    if let Some(i) = o {
        f(i)
    }
    else {
        g()
    }
}

The lint suggests o.map_or(g(), |i| f(i)), which is wrong because the else branch should be evaluated lazily because it's a function call. It ought to have suggested o.map_or_else(|| g(), |i| f(i))

Meta

  • cargo clippy -V: clippy 0.0.212 (39d5a61 2020-07-17)
  • rustc -Vv:
    rustc 1.47.0-nightly (39d5a61f2 2020-07-17)
    binary: rustc
    commit-hash: 39d5a61f2e4e237123837f5162cc275c2fd7e625
    commit-date: 2020-07-17
    host: x86_64-unknown-linux-gnu
    release: 1.47.0-nightly
    LLVM version: 10.0
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingC-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.I-false-negativeIssue: The lint should have been triggered on code, but wasn'tL-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