rust-lang / rust-lang/rust-clippy

`redundant_closure` false positive when interacting with `FnMut` closures

Open
#8,098 7 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

This is related to https://github.com/rust-lang/rust-clippy/issues/8073 but I believe it is sufficiently different:

Lint Name

redundant_closure

Reproducer

I tried this code:

#![allow(unused)]
fn minimal<L>(mut logic: L)
where
    L: FnMut(()),
{
    let _: Box<dyn FnMut()> = Box::new(move || {
        Vec::new().into_iter().map(|d| logic(d));
    });
}

I saw this happen:

> it compiled

Clippy suggests this closure is a false positive, but if you take its suggestion, it fails to compile:

cannot move out of `logic`, a captured variable in an `FnMut` closure

because you are moving logic into the box, instead of moving a closure that references logic into it

playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7d085db100dcb0f863e3e6f72dd3523a

Version
rustc 1.57
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the redundant_closure lint and reproduce the behavior using the Rust 1.57 example or its linked Playground. Compare Clippy's suggested rewrite with the original FnMut closure and confirm that the corrected behavior avoids the reported compile failure while retaining the lint's intended cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.