]> git.lizzy.rs Git - rust.git/blob - src/docs/if_let_mutex.txt
[Arithmetic] Consider literals
[rust.git] / src / docs / if_let_mutex.txt
1 ### What it does
2 Checks for `Mutex::lock` calls in `if let` expression
3 with lock calls in any of the else blocks.
4
5 ### Why is this bad?
6 The Mutex lock remains held for the whole
7 `if let ... else` block and deadlocks.
8
9 ### Example
10 ```
11 if let Ok(thing) = mutex.lock() {
12     do_thing();
13 } else {
14     mutex.lock();
15 }
16 ```
17 Should be written
18 ```
19 let locked = mutex.lock();
20 if let Ok(thing) = locked {
21     do_thing(thing);
22 } else {
23     use_locked(locked);
24 }
25 ```