Skip to content

option_if_let_else suggests code that doesn't compile #14484

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

Closed
edwloef opened this issue Mar 27, 2025 · 3 comments
Closed

option_if_let_else suggests code that doesn't compile #14484

edwloef opened this issue Mar 27, 2025 · 3 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@edwloef
Copy link

edwloef commented Mar 27, 2025

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

@edwloef edwloef added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 27, 2025
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Mar 27, 2025
@edwloef
Copy link
Author

edwloef commented Mar 27, 2025

This could possibly suggest

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

@profetia
Copy link
Contributor

This should be closed in #14389.

@edwloef
Copy link
Author

edwloef commented Mar 28, 2025

Thanks for the quick response :) I'll confirm whether that fixes things once it lands in nightly clippy.

@edwloef edwloef closed this as completed May 17, 2025
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-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

3 participants