-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniel Smith
committed
Oct 21, 2020
1 parent
86f2b29
commit 4d33225
Showing
6 changed files
with
309 additions
and
301 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 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 | ||
} | ||
|
||
async fn not_good(x: &Mutex<u32>) -> u32 { | ||
let first = baz().await; | ||
|
||
let second = { | ||
let guard = x.lock().unwrap(); | ||
baz().await | ||
}; | ||
|
||
let third = baz().await; | ||
|
||
first + second + third | ||
} | ||
|
||
#[allow(clippy::manual_async_fn)] | ||
fn block_bad(x: &Mutex<u32>) -> impl std::future::Future<Output = u32> + '_ { | ||
async move { | ||
let guard = x.lock().unwrap(); | ||
baz().await | ||
} | ||
} | ||
|
||
fn main() { | ||
let m = Mutex::new(100); | ||
good(&m); | ||
bad(&m); | ||
also_bad(&m); | ||
not_good(&m); | ||
block_bad(&m); | ||
} |
Oops, something went wrong.