Skip to content

clippy::option_if_let_else suggests invalid code fix #11059

@u9g

Description

@u9g

Summary

The code produced by clippy is in theory correct in my eyes, but in reality the type checker doesn't understand it.

Reproducer

I tried this code:

if let Some(id) = &v.as_class().unwrap().id {
    Box::new(std::iter::once(Vertex::Span(id.span)))
} else {
    Box::new(std::iter::empty())
}

I expected to see this happen: to get working code after a quick fix.
Instead, this happened: The quick fix produces the following non-working code:

v.as_class().unwrap().id.as_ref().map_or_else(
    || Box::new(std::iter::empty()),
    |id| Box::new(std::iter::once(Vertex::Span(id.span))),
)

Which is invalid because:

error[E0308]: mismatched types
   --> crates\oxc_linter\src\lint_adapter2.rs:217:35
    |
217 |                     |id| Box::new(std::iter::once(Vertex::Span(id.span))),
    |                          -------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Empty<_>`, found `Once<Vertex<'_>>`
    |                          |
    |                          arguments to this function are incorrect
    |
    = note: expected struct `std::iter::Empty<_>`
               found struct `std::iter::Once<lint_adapter2::Vertex<'_>>`
note: associated function defined here
   --> C:\Users\jason\.rustup\toolchains\nightly-2023-06-02-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\alloc\src\boxed.rs:215:12
    |
215 |     pub fn new(x: T) -> Self {
    |            ^^^

Version

rustc 1.72.0-nightly (d59363ad0 2023-06-01)
binary: rustc
commit-hash: d59363ad0b6391b7fc5bbb02c9ccf9300eef3753
commit-date: 2023-06-01
host: x86_64-pc-windows-msvc
release: 1.72.0-nightly
LLVM version: 16.0.4

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-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