Skip to content

redundant closure lint fails for FnMut where FnOnce is expected #3898

Closed

Description

This compiles (playground):

pub fn bar(mut x: Box<dyn FnMut()>) {
    foo(|| x())
}

pub fn foo<T: FnOnce()>(_: T) {}

but clippy suggests:

warning: redundant closure found
 --> src/lib.rs:2:9
  |
2 |     foo(|| x())
  |         ^^^^^^ help: remove closure as shown: `x`
  |
  = 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

which fails to compile with (playground):

error[E0277]: expected a `std::ops::FnOnce<()>` closure, found `std::boxed::Box<dyn std::ops::FnMut()>`
 --> src/lib.rs:2:5
  |
2 |     foo(x)
  |     ^^^ expected an `FnOnce<()>` closure, found `std::boxed::Box<dyn std::ops::FnMut()>`
  |
  = help: the trait `std::ops::FnOnce<()>` is not implemented for `std::boxed::Box<dyn std::ops::FnMut()>`
  = note: wrap the `std::boxed::Box<dyn std::ops::FnMut()>` in a closure with no arguments: `|| { /* code */ }
note: required by `foo`
 --> src/lib.rs:5:1
  |
5 | pub fn foo<T: FnOnce()>(_: T) {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions