Rust-clippy: False positive for redundant_closure involving lifetimes

Created on 13 May 2020  路  1Comment  路  Source: rust-lang/rust-clippy

I hit a false positive when contributing to rusqlite. The code in the PR can not be written without that closure as it doesn't seem to unify for some reason. Not sure if it's a bug in the compiler or some kind of deficiency in my code (please look at that TryFrom impl, if you think you could help). Would love to file issue against rustc, if relevant, but I don't even know what keywords to use for searching for existing issues. :(

Without the closure I got:

   --> src/row.rs:382:76
    |
382 |         let val = conn.query_row("SELECT a FROM test", std::iter::empty(), <(u32,)>::try_from)
    |                                                                            ^^^^^^^^^^^^^^^^^^
    |                                                                            |
    |                                                                            expected signature of `for<'r, 's> fn(&'r row::Row<'s>) -> _`
    |                                                                            found signature of `fn(_) -> _`

error[E0271]: type mismatch resolving `for<'r, 's> <fn(_) -> std::result::Result<(u32,), <(u32,) as std::convert::TryFrom<_>>::Error> {<(u32,) as std::convert::TryFrom<_>>::try_from} as std::ops::FnOnce<(&'r row::Row<'s>,)>>::Output == std::result::Result<_, error::Error>`

Edit: sorry for forgetting to paste version. I don't know the version from rusqlite CI, but same issue is present in clippy 0.0.212 (69f99e7 2019-12-14). I also tried to impl TryFrom using a single lifetime - same problem.

E-hard L-bug L-false-positive

Most helpful comment

Minimal example:

struct S;

impl From<&S> for u32 {
    fn from(_: &S) -> Self {
        42
    }
}

fn query<T, F>(s: S, f: F) -> T
where
    F: FnOnce(&S) -> T,
{
    f(&s)
}

fn main() {
    query(S, |s| <u32>::from(s));
    // this is suggested by Clippy, but fails because the HRTBs of the `&S` type
    // cannot be inferred
    // query(S, <u32>::from);
}

Playground

>All comments

Minimal example:

struct S;

impl From<&S> for u32 {
    fn from(_: &S) -> Self {
        42
    }
}

fn query<T, F>(s: S, f: F) -> T
where
    F: FnOnce(&S) -> T,
{
    f(&s)
}

fn main() {
    query(S, |s| <u32>::from(s));
    // this is suggested by Clippy, but fails because the HRTBs of the `&S` type
    // cannot be inferred
    // query(S, <u32>::from);
}

Playground

Was this page helpful?
0 / 5 - 0 ratings