Skip to content

Redundant closue lint can lose type annotation #8548

Closed
@neverRare

Description

@neverRare

Summary

Redundant closure lint can lose type annotation. This can lead to type annotation needed error.

Lint Name

redundant_closure

Reproducer

In this rather contrived code

fn id<T>(x: T) -> T {
    x
}
fn main() {
    let _ = None.map(|x: i32| id(x));
}

Clippy reports the following

warning: redundant closure
 [--> src/main.rs:5:22
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021#)  |
5 |     let _ = None.map(|x: i32| id(x));
  |                      ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `id`
  |
  = 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

But if I do replace it, I get the following error

error[[E0282]](https://doc.rust-lang.org/nightly/error-index.html#E0282): type annotations needed for `Option<T>`
 [--> src/lib.rs:5:13
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021#)  |
5 |     let _ = None.map(id);
  |         -   ^^^^ cannot infer type for type parameter `T` declared on the enum `Option`
  |         |
  |         consider giving this pattern the explicit type `Option<T>`, where the type parameter `T` is specified

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

Version

No response

Additional Labels

No response

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 have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions