Skip to content

redundant_closure: FnOnce is not general enough #7983

Closed
@jonhoo

Description

@jonhoo

Lint name: redundant_closure

I tried this code:

fn main() {
    std::iter::once(String::new()).find(|s| check(s));
}

pub fn check<T: AsRef<str>>(_: T) -> bool {
    true
}

Clippy recommended I rewrite it to:

std::iter::once(String::new()).find(check);

I expected to see this happen: if I apply Clippy's suggestion, my code would compile.

Instead, this happened: applying Clippy's suggested edit causes the code to no longer compile, yielding:

error: implementation of `std::ops::FnOnce` is not general enough
 --> src/main.rs:2:36
  |
2 |     std::iter::once(String::new()).find(check);
  |                                    ^^^^ implementation of `std::ops::FnOnce` is not general enough
  |
  = note: `fn(&'2 std::string::String) -> bool {check::<&'2 std::string::String>}` must implement `std::ops::FnOnce<(&'1 std::string::String,)>`, for any lifetime `'1`...
  = note: ...but it actually implements `std::ops::FnOnce<(&'2 std::string::String,)>`, for some specific lifetime `'2`

FWIW, the actual function causing me this issue was cargo::util::restricted_names::is_glob_pattern.

Meta

Rust version (rustc -Vv):

rustc 1.56.1 (59eed8a2a 2021-11-01)
binary: rustc
commit-hash: 59eed8a2aac0230a8b53e89d4e99d55912ba6b35
commit-date: 2021-11-01
host: x86_64-apple-darwin
release: 1.56.1
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