Skip to content

option_if_let_else suggests code that doesn't compile #14484

Open
@edwloef

Description

@edwloef

Summary

Clippy suggests code that does not compile.

Lint Name

option_if_let_else

Reproducer

I tried this code:

fn fp(a: Option<impl Any + 'static>) -> Option<Box<dyn Any>> {
    match a {
        Some(a) => Some(Box::from(a)),
        None => None,
    }
}

I saw this happen:

Clippy suggests

fn fp(a: Option<impl Any + 'static>) -> Option<Box<dyn Any>> {
    a.map_or_else(|| None, |a| Some(Box::from(a)))
}

which does not compile:

note: expected enum `std::option::Option<std::boxed::Box<(dyn std::any::Any + 'static)>>`
         found enum `std::option::Option<std::boxed::Box<impl Any + 'static>>`

I expected to see this happen:

Suggestion compiles, or no suggestion.

Version

rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: x86_64-unknown-linux-gnu
release: 1.85.1
LLVM version: 19.1.7

Additional Labels

@rustbot label +I-suggestion-causes-error

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 applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions