Skip to content

Compiler Suggests Cloning an async Block #121547

Closed
@HTGAzureX1212

Description

@HTGAzureX1212

Code

// https://github.com/HTGAzureX1212/HarTex/blob/491fe72da9042d5a5c7f61d0d5cdaf6e9b112057/discord-frontend/hartex-discord-leader/src/kafka.rs#L79-L120

Current output

error[E0382]: use of moved value: `producer`
   --> hartex-discord-leader\src\kafka.rs:78:23
    |
68  |   async fn inbound<'a, Q>(shards: Vec<&'a mut Shard<Q>>, producer: FutureProducer) -> miette::Result<()>
    |                                                          -------- move occurs because `producer` has type `FutureProducer`, which does not implement the `Copy` trait
...
74  |       loop {
    |       ---- inside of this loop
...
77  |           for shard in shards {
    |           ------------------- inside of this loop
78  |               set.spawn(async move {
    |  _______________________^
79  | |                 while let Some(result) = shard.next().await {
80  | |                     match result {
81  | |                         Ok(message) => {
...   |
95  | |                             if let Err((error, _)) = producer
    | |                                                      -------- use occurs due to use in coroutine
...   |
119 | |                 }
120 | |             });
    | |_____________^ value moved here, in previous iteration of loop
    |
help: consider cloning the value if the performance cost is acceptable
    |
120 |             }.clone());
    |              ++++++++

Desired output

error[E0382]: use of moved value: `producer`
   --> hartex-discord-leader\src\kafka.rs:78:23
    |
68  |   async fn inbound<'a, Q>(shards: Vec<&'a mut Shard<Q>>, producer: FutureProducer) -> miette::Result<()>
    |                                                          -------- move occurs because `producer` has type `FutureProducer`, which does not implement the `Copy` trait
...
74  |       loop {
    |       ---- inside of this loop
...
77  |           for shard in shards {
    |           ------------------- inside of this loop
78  |               set.spawn(async move {
    |  _______________________^
79  | |                 while let Some(result) = shard.next().await {
80  | |                     match result {
81  | |                         Ok(message) => {
...   |
95  | |                             if let Err((error, _)) = producer
    | |                                                      -------- use occurs due to use in coroutine
...   |
119 | |                 }
120 | |             });
    | |_____________^ value moved here, in previous iteration of loop
    |
help: consider cloning the value if the performance cost is acceptable
    |
95  |                               if let Err((error, _)) = producer.clone()
    |                                                                ++++++++

Rationale and extra context

I expect that the .clone() invocation to be added for the value that is moved, but for some reason it is doing it for the entire async closure.

Other cases

No response

Rust Version

rustc 1.78.0-nightly (8f359beca 2024-02-23)
binary: rustc
commit-hash: 8f359beca4e58bc3ae795a666301a8f47023044c
commit-date: 2024-02-23
host: x86_64-pc-windows-msvc
release: 1.78.0-nightly
LLVM version: 18.1.0

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsS-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions