New lint suggestion: You're using a blocking operation when you could be using an async one
@J-ZhengLi is already working on this.
Since Sep 25, 2023.
Assessment
This issue has not been assessed yet.
Description
What it does
A lint which checks for blocking functions inside an async block.
One I know of (and the cause of a bug I ran into today) is std::thread::sleep, but there are others. (file operations, networking, ...). If such a function has an async counterpart, then the user could be warned of this fact.
Lint Name
unwise-block
Category
suspicious
Advantage
- warn of badly behaving async programs
Drawbacks
- might be too noisy
Example
use std::thread::sleep;
use std::time::Duration;
use tokio::signal::unix::signal;
use tokio::signal::unix::SignalKind;
use tokio::sync::mpsc;
async fn rx(rx: &mut mpsc::Receiver<()>) {
loop {
sleep(Duration::new(1, 0));
if rx.try_recv().is_ok() {
println!("got one splivet");
}
}
}
async fn tx(tx: mpsc::Sender<()>) {
let mut stream = signal(SignalKind::user_defined1()).unwrap();
loop {
stream.recv().await;
println!("sending one splivet");
tx.send(()).await.unwrap();
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
let (stx, mut srx) = mpsc::channel::<()>(5);
tokio::join!(tx(stx), rx(&mut srx));
}
Could be written as:
use std::thread::sleep;
use std::time::Duration;
use tokio::signal::unix::signal;
use tokio::signal::unix::SignalKind;
use tokio::sync::mpsc;
async fn rx(rx: &mut mpsc::Receiver<()>) {
loop {
tokio::time::sleep(Duration::new(1, 0)).await;
if rx.try_recv().is_ok() {
println!("got one splivet");
}
}
}
async fn tx(tx: mpsc::Sender<()>) {
let mut stream = signal(SignalKind::user_defined1()).unwrap();
loop {
stream.recv().await;
println!("sending one splivet");
tx.send(()).await.unwrap();
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
let (stx, mut srx) = mpsc::channel::<()>(5);
tokio::join!(tx(stx), rx(&mut srx));
}
Note the different way to sleep in the rx function.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from rust-lang/rust-clippy
-
C-bug L-suggestion
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
rust-lang/rust-clippy#17674 · 5 comments ·
-
C-bug L-suggestion
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
rust-lang/rust-clippy#17673 · 3 comments ·
-
C-bug I-false-positive
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
rust-lang/rust-clippy#17566 ·
-
A-documentation
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
rust-lang/rust-clippy#17259 · 3 comments ·
-
A-documentation A-website C-bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
rust-lang/rust-clippy#16981 · 1 reaction ·
All issues in rust-lang/rust-clippy
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
kwakseongjae/auto-hwp#319 ·
-
area:cli bug filter-quality good first issue priority:medium
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
bevyengine/bevy#25861 ·
-
comp-datalake
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
ClickHouse/ClickHouse#121222 ·
-
enhancement remote
Difficulty 2/5 1-3 hours Newbie friendliness 68/100