Skip to content

False positive for redundant_closure lint #1439

Closed
@samestep

Description

@samestep

I wrote a simple thunk implementation:

struct Thunk<T>(Box<FnMut() -> T>);

impl<T> Thunk<T> {
    fn new<F: 'static + FnOnce() -> T>(f: F) -> Thunk<T> {
        let mut option = Some(f);
        Thunk(Box::new(move || option.take().unwrap()()))
    }

    fn unwrap(self) -> T {
        let Thunk(mut f) = self;
        f()
    }
}

fn main() {
    let thunk = Thunk::new(|| println!("Hello, world!"));
    thunk.unwrap()
}

However, clippy complains that the closure in Thunk::new is redundant:

warning: redundant closure found, #[warn(redundant_closure)] on by default
 --> src/main.rs:6:24
  |
6 |         Thunk(Box::new(move || option.take().unwrap()()))
  |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
help: remove closure as shown:
  |         Thunk(Box::new(option.take().unwrap()))
  = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#redundant_closure

But if I use clippy's suggestion, I get this error:

error[E0277]: the trait bound `F: std::ops::FnMut<()>` is not satisfied
 --> src/main.rs:6:15
  |
6 |         Thunk(Box::new(option.take().unwrap()))
  |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::ops::FnMut<()>` is not implemented for `F`
  |
  = help: consider adding a `where F: std::ops::FnMut<()>` bound
  = note: required for the cast to the object type `std::ops::FnMut() -> T + 'static`

This seems like a bug in the redundant_closure lint.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions