Skip to content

redundant_async_block results in broken code for async fn f(&self) #10482

@mattsse

Description

@mattsse

Summary

auto fix of redundant_async_block can lead to broken code in some circumstances

Lint Name

redundant_async_block

Reproducer

I tried this code:

#[derive(Debug, Clone)]
struct F {}

impl F {

    async fn run(&self) {}
}

pub async fn run() {
    let f = F{};
    let c = f.clone();
    tokio::task::spawn(async move {c.run().await});
    let _f = f;
}

repro: https://github.com/mattsse/redundant-async-block-broken-code

I saw this happen:

after fixes were automatically applied the compiler reported errors within these files:

  * src/lib.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/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[E0597]: `c` does not live long enough
  --> src/lib.rs:16:24
   |
16 |     tokio::task::spawn(c.run());
   |                        ^^^^^^^
   |                        |
   |                        borrowed value does not live long enough
   |                        argument requires that `c` is borrowed for `'static`
17 |     let _f = f;
18 | }
   | - `c` dropped here while still borrowed

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.
Original diagnostics will follow.

warning: this async expression only awaits a single future
  --> src/lib.rs:16:24
   |
16 |     tokio::task::spawn(async move {c.run().await});
   |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can reduce it to: `c.run()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_async_block
   = note: `#[warn(clippy::redundant_async_block)]` on by default

I expected to see this happen:

Version

cargo 1.70.0-nightly (7d3033d2e 2023-03-08)
rustc 1.70.0-nightly (ff4b772f8 2023-03-10)

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions