Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FP redundant_closure_call with async closure #12174

Open
matthiaskrgr opened this issue Jan 19, 2024 · 1 comment
Open

FP redundant_closure_call with async closure #12174

matthiaskrgr opened this issue Jan 19, 2024 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@matthiaskrgr
Copy link
Member

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

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jan 19, 2024
@y21
Copy link
Member

y21 commented Mar 16, 2024

Was it a compiler bug that the reproducer was even accepted in the first place? I'm not sure that it should compile, and it is a compile error now. As far as I know, capture modes should be the same for closures and async blocks (x should be captured by reference here), unless async closures work differently?

In any case, this isn't reproducible anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

2 participants