-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn'tIssue: The lint should have been triggered on code, but wasn't
Description
Summary
When passing an async closure to std::thread::spawn
, there's no warning to indicate that the thread will not run if the future is not awaited or without a runtime. This was confusing behaviour when I was expecting the thread to spawn and run (my mistake as it shouldn't have been an sync closure, but wondering if this can be linted).
Lint Name
unused_async
Reproducer
I tried this code:
use std::thread;
fn main() {
let handle = thread::spawn(|| async move {
println!("Hello 1, world thread!");
println!("Hello 2, world thread!");
println!("Hello 3, world thread!");
});
println!("Hello 1, world main function!");
println!("Hello 2, world main function!");
let _ = handle.join();
}
I expected to see a clippy warning for the unawaited future of the async move. This would highlight that the closure does not get awaited and so the spawned thread will do nothing. Instead of || async move {...}
the correct syntax I needed was move || {...}
.
This code compiles successfully and produces the following output with cargo run
:
Compiling test-thread v0.1.0 (/Users/benschof/personal/test-thread)
Finished dev [unoptimized + debuginfo] target(s) in 0.15s
Running `target/debug/test-thread`
Hello 1, world main function!
Hello 2, world main function!
Version
rustc 1.74.1 (a28077b28 2023-12-04)
binary: rustc
commit-hash: a28077b28a02b92985b3a3faecf92813155f1ea1
commit-date: 2023-12-04
host: aarch64-apple-darwin
release: 1.74.1
LLVM version: 17.0.4
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn'tIssue: The lint should have been triggered on code, but wasn't