Skip to content
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

Redundant closue lint can lose type annotation #8548

Closed
neverRare opened this issue Mar 16, 2022 · 4 comments · Fixed by #8685
Closed

Redundant closue lint can lose type annotation #8548

neverRare opened this issue Mar 16, 2022 · 4 comments · Fixed by #8685
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

Comments

@neverRare
Copy link

neverRare commented Mar 16, 2022

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

@neverRare neverRare 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 16, 2022
@dswij
Copy link
Member

dswij commented Mar 16, 2022

@rustbot claim

@dswij
Copy link
Member

dswij commented May 2, 2022

@rustbot release-assignment

@IceTDrinker
Copy link

Same kind of issue with a function pointer coming from C through the FFI that is passed as a Fn(u64) -> u64 through a wrapping closure (triggering the lint), correcting the lint by passing the function extern "C" fn(u64) -> u64 results in a compiler error expected an ``Fn<(u64,)>`` closure, found extern "C" fn(u64) -> u64

@profi248
Copy link

I've ran into this issue with clippy --fix, where Clippy replaced the closure with a direct mapping and broke the code.

This code:

pub async fn get_backup_path(&mut self) -> anyhow::Result<Option<PathBuf>> {
    let path = sqlx::query("select value from config where key = 'backup_path'")
        .fetch_optional(&mut self.transaction)
        .await?
        .map(|row| row.get(0));

    Ok(path.map(|path: String| PathBuf::from(path)))
}

Was "fixed" by Clippy by doing this:

pub async fn get_backup_path(&mut self) -> anyhow::Result<Option<PathBuf>> {
    let path = sqlx::query("select value from config where key = 'backup_path'")
        .fetch_optional(&mut self.transaction)
        .await?
        .map(|row| row.get(0));

    Ok(path.map(PathBuf::from))
}

Which produced this error:

error[E0282]: type annotations needed for `std::option::Option<_>`
  --> client/src/config/backup.rs:63:13
   |
63 |         let path = sqlx::query("select value from config where key = 'backup_path'")
   |             ^^^^
   |
help: consider giving `path` an explicit type, where the type for type parameter `U` is specified
   |
63 |         let path: std::option::Option<_> = sqlx::query("select value from config where key = 'backup_path'")
   |                 ++++++++++++++++++++++++

error[E0283]: type annotations needed for `std::option::Option<_>`
  --> client/src/config/backup.rs:63:13
   |
63 |         let path = sqlx::query("select value from config where key = 'backup_path'")
   |             ^^^^
...
68 |         Ok(path.map(PathBuf::from))
   |                     ------------- type must be known at this point
   |
   = note: multiple `impl`s satisfying `std::path::PathBuf: std::convert::From<_>` found in the following crates: `core`, `std`:
           - impl std::convert::From<std::boxed::Box<std::path::Path>> for std::path::PathBuf;
           - impl std::convert::From<std::ffi::OsString> for std::path::PathBuf;
           - impl std::convert::From<std::string::String> for std::path::PathBuf;
           - impl<'a> std::convert::From<std::borrow::Cow<'a, std::path::Path>> for std::path::PathBuf;
           - impl<T> std::convert::From<!> for T;
           - impl<T> std::convert::From<&T> for std::path::PathBuf
             where T: std::convert::AsRef<std::ffi::OsStr>, T: ?Sized;
           - impl<T> std::convert::From<T> for T;
help: consider giving `path` an explicit type, where the type for type parameter `T` is specified
   |
63 |         let path: std::option::Option<_> = sqlx::query("select value from config where key = 'backup_path'")
   |                 ++++++++++++++++++++++++

error[E0283]: type annotations needed for `std::option::Option<_>`
  --> client/src/config/backup.rs:63:13
   |
63 |         let path = sqlx::query("select value from config where key = 'backup_path'")
   |             ^^^^
...
68 |         Ok(path.map(PathBuf::from))
   |            ----------------------- type must be known at this point
   |
   = note: multiple `impl`s satisfying `std::path::PathBuf: std::convert::From<_>` found in the following crates: `core`, `std`:
           - impl std::convert::From<std::boxed::Box<std::path::Path>> for std::path::PathBuf;
           - impl std::convert::From<std::ffi::OsString> for std::path::PathBuf;
           - impl std::convert::From<std::string::String> for std::path::PathBuf;
           - impl<'a> std::convert::From<std::borrow::Cow<'a, std::path::Path>> for std::path::PathBuf;
           - impl<T> std::convert::From<!> for T;
           - impl<T> std::convert::From<&T> for std::path::PathBuf
             where T: std::convert::AsRef<std::ffi::OsStr>, T: ?Sized;
           - impl<T> std::convert::From<T> for T;
help: consider giving `path` an explicit type, where the type for type parameter `T` is specified
   |
63 |         let path: std::option::Option<_> = sqlx::query("select value from config where key = 'backup_path'")
   |                 ++++++++++++++++++++++++

@bors bors closed this as completed in 4c2f460 Jul 30, 2023
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants