Skip to content

redundant_closure suggested fix fails to compile due to lifetime requirements #14215

Open
@ian-h-chamberlain

Description

@ian-h-chamberlain

Summary

This looks similar/related to #13094 and #11604 but the failure mode is slightly different; this case seems to be specifically about the implicit for<'a> lifetime on the FnOnce bound. It may be possible to simplify this example a bit but this is about the extent of my ability to figure out how...

Lint Name

redundant_closure

Reproducer

I tried this code:

use core::marker::PhantomData;
use core::ops::FnOnce;

struct Guard<'a, S> {
    _data: PhantomData<&'a S>,
}

impl<S> Guard<'_, S> {
    fn do_something(_this: &mut Self) {}
}

fn takes_guard_fn<S, F>(_func: F)
where
    F: FnOnce(&mut Guard<S>),
{
}

fn main() {
    takes_guard_fn::<i32, _>(|s| Guard::do_something(s));
}

I saw this happen:

$ cargo clippy --fix --bin rs-sandbox
    Checking rs-sandbox v0.1.0 (/home/ian/Documents/Development/rs-sandbox)
warning: failed to automatically apply fixes suggested by rustc to crate `rs_sandbox`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error: implementation of `std::ops::FnOnce` is not general enough
  --> src/main.rs:19:5
   |
19 |     takes_guard_fn::<i32, _>(Guard::do_something);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `std::ops::FnOnce` is not general enough
   |
   = note: `for<'a> fn(&'a mut Guard<'2, i32>) {Guard::<'2, i32>::do_something}` must implement `std::ops::FnOnce<(&mut Guard<'1, i32>,)>`, for any lifetime `'1`...
   = note: ...but it actually implements `std::ops::FnOnce<(&mut Guard<'2, i32>,)>`, for some specific lifetime `'2`

error: aborting due to 1 previous error

Original diagnostics will follow.

warning: redundant closure
  --> src/main.rs:19:30
   |
19 |     takes_guard_fn::<i32, _>(|s| Guard::do_something(s));
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `Guard::do_something`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure
   = note: `#[warn(clippy::redundant_closure)]` on by default

warning: `rs-sandbox` (bin "rs-sandbox") generated 1 warning (run `cargo clippy --fix --bin "rs-sandbox"` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.12s
$ 

I expected to see this happen:

No errors/warnings emitted

Version

rustc 1.84.1 (e71f9a9a9 2025-01-27)
binary: rustc
commit-hash: e71f9a9a98b0faf423844bf0ba7438f29dc27d58
commit-date: 2025-01-27
host: x86_64-unknown-linux-gnu
release: 1.84.1
LLVM version: 19.1.5

Additional Labels

@rustbot label I-suggestion-causes-error

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