rust-lang / rust-lang/rust-clippy

FP redundant_closure_call with async closure

Open
#12,174 1 comment 0 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

.

Lint Name

redundant_closure_call

Reproducer

I tried this code:

#![feature(async_closure)]
fn foo() -> Box<dyn std::future::Future<Output = u32>> {
    let x = 0u32;
    Box::new((async || x)())
}

fn main() {
}

I saw this happen:

warning: function `foo` is never used
 --> src/main.rs:5:4
  |
5 | fn foo() -> Box<dyn std::future::Future<Output = u32>> {
  |    ^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: try not to call a closure in the expression where it is declared
 --> src/main.rs:7:14
  |
7 |     Box::new((async || x)())
  |              ^^^^^^^^^^^^^^ help: try doing something like: `async { x }`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_call
  = note: `#[warn(clippy::redundant_closure_call)]` on by default

warning: `o` (bin "o" test) generated 2 warnings (run `cargo clippy --fix --bin "o" --tests` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `o`

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[E0373]: async block may outlive the current function, but it borrows `x`, which is owned by the current function
 --> src/main.rs:7:14
  |
7 |     Box::new(async { x })
  |              ^^^^^^^^-^^
  |              |       |
  |              |       `x` is borrowed here
  |              may outlive borrowed value `x`
  |
note: async block is returned here
 --> src/main.rs:7:5
  |
7 |     Box::new(async { x })
  |     ^^^^^^^^^^^^^^^^^^^^^
help: to force the async block to take ownership of `x` (and any other referenced variables), use the `move` keyword
  |
7 |     Box::new(async move { x })
  |                    ++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0373`.
Original diagnostics will follow.
Version
rustc 1.77.0-nightly (25f8d01fd 2024-01-18)
binary: rustc
commit-hash: 25f8d01fd8bda339612d0c0a8844173a09205f7c
commit-date: 2024-01-18
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6
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 by reproducing the async-closure example from the issue and then locate the redundant_closure_call lint implementation and its suggestion logic in this repository. Done means the lint no longer emits an invalid replacement for this case, with a regression test covering the reported compiler error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.