]> git.lizzy.rs Git - rust.git/blob - src/docs/significant_drop_in_scrutinee.txt
Auto merge of #9421 - xphoniex:fix-#9420, r=giraffate
[rust.git] / src / docs / significant_drop_in_scrutinee.txt
1 ### What it does
2 Check for temporaries returned from function calls in a match scrutinee that have the
3 `clippy::has_significant_drop` attribute.
4
5 ### Why is this bad?
6 The `clippy::has_significant_drop` attribute can be added to types whose Drop impls have
7 an important side-effect, such as unlocking a mutex, making it important for users to be
8 able to accurately understand their lifetimes. When a temporary is returned in a function
9 call in a match scrutinee, its lifetime lasts until the end of the match block, which may
10 be surprising.
11
12 For `Mutex`es this can lead to a deadlock. This happens when the match scrutinee uses a
13 function call that returns a `MutexGuard` and then tries to lock again in one of the match
14 arms. In that case the `MutexGuard` in the scrutinee will not be dropped until the end of
15 the match block and thus will not unlock.
16
17 ### Example
18 ```
19 let mutex = Mutex::new(State {});
20
21 match mutex.lock().unwrap().foo() {
22     true => {
23         mutex.lock().unwrap().bar(); // Deadlock!
24     }
25     false => {}
26 };
27
28 println!("All done!");
29 ```
30 Use instead:
31 ```
32 let mutex = Mutex::new(State {});
33
34 let is_foo = mutex.lock().unwrap().foo();
35 match is_foo {
36     true => {
37         mutex.lock().unwrap().bar();
38     }
39     false => {}
40 };
41
42 println!("All done!");
43 ```