mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-28 01:34:21 +00:00
6c25c3c381
Fixes #4226 This introduces the lint await_holding_lock. For async functions, we iterate over all types in generator_interior_types and look for types named MutexGuard, RwLockReadGuard, or RwLockWriteGuard. If we find one then we emit a lint.
43 lines
671 B
Rust
43 lines
671 B
Rust
// edition:2018
|
|
#![warn(clippy::await_holding_lock)]
|
|
|
|
use std::sync::Mutex;
|
|
|
|
async fn bad(x: &Mutex<u32>) -> u32 {
|
|
let guard = x.lock().unwrap();
|
|
baz().await
|
|
}
|
|
|
|
async fn good(x: &Mutex<u32>) -> u32 {
|
|
{
|
|
let guard = x.lock().unwrap();
|
|
let y = *guard + 1;
|
|
}
|
|
baz().await;
|
|
let guard = x.lock().unwrap();
|
|
47
|
|
}
|
|
|
|
async fn baz() -> u32 {
|
|
42
|
|
}
|
|
|
|
async fn also_bad(x: &Mutex<u32>) -> u32 {
|
|
let first = baz().await;
|
|
|
|
let guard = x.lock().unwrap();
|
|
|
|
let second = baz().await;
|
|
|
|
let third = baz().await;
|
|
|
|
first + second + third
|
|
}
|
|
|
|
fn main() {
|
|
let m = Mutex::new(100);
|
|
good(&m);
|
|
bad(&m);
|
|
also_bad(&m);
|
|
}
|