Skip to content

False positive in redundant_closure with turbofish #7861

@Kobzol

Description

@Kobzol

Lint name: redundant_closure

I tried this code:

struct A;
trait Trait {}
impl Trait for A {}

fn main() {
    let _: Option<Box<dyn Trait>> = Some(A).map::<Box<dyn Trait>, _>(|v| Box::new(v));
}

I expected to see this happen:
No lint is emitted, because changing the corresponding code from map(|v| Box::new(v)) to map(Box::new) breaks compilation.

Instead, this happened:
The following lint was emitted:

warning: redundant closure
 --> src/main.rs:8:70
  |
8 |     let _: Option<Box<dyn Trait>> = Some(A).map::<Box<dyn Trait>, _>(|v| Box::new(v));
  |                                                                      ^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `Box::new`
  |
  = note: `#[warn(clippy::redundant_closure)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure

warning: `foo` (bin "foo") generated 1 warning

If the lint is applied, this error is produced during a build:

error[E0271]: type mismatch resolving `<fn(A) -> std::boxed::Box<A> {std::boxed::Box::<A>::new} as std::ops::FnOnce<(A,)>>::Output == std::boxed::Box<dyn Trait>`
 --> src/main.rs:8:45
  |
8 |     let _: Option<Box<dyn Trait>> = Some(A).map::<Box<dyn Trait>, _>(Box::new);
  |                                             ^^^ expected struct `A`, found trait object `dyn Trait`
  |
  = note: expected struct `std::boxed::Box<A>`
             found struct `std::boxed::Box<dyn Trait>`

For more information about this error, try `rustc --explain E0271`.

Meta

Rust version (rustc -Vv):

rustc 1.58.0-nightly (efd048394 2021-10-20)
binary: rustc
commit-hash: efd0483949496b067cd5f7569d1b28cd3d5d3c72
commit-date: 2021-10-20
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0

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