summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/significant_drop_in_scrutinee.txt
blob: f869def0ddb7c28eec21f8eb08b381f48ca33cdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
### What it does
Check for temporaries returned from function calls in a match scrutinee that have the
`clippy::has_significant_drop` attribute.

### Why is this bad?
The `clippy::has_significant_drop` attribute can be added to types whose Drop impls have
an important side-effect, such as unlocking a mutex, making it important for users to be
able to accurately understand their lifetimes. When a temporary is returned in a function
call in a match scrutinee, its lifetime lasts until the end of the match block, which may
be surprising.

For `Mutex`es this can lead to a deadlock. This happens when the match scrutinee uses a
function call that returns a `MutexGuard` and then tries to lock again in one of the match
arms. In that case the `MutexGuard` in the scrutinee will not be dropped until the end of
the match block and thus will not unlock.

### Example
```
let mutex = Mutex::new(State {});

match mutex.lock().unwrap().foo() {
    true => {
        mutex.lock().unwrap().bar(); // Deadlock!
    }
    false => {}
};

println!("All done!");
```
Use instead:
```
let mutex = Mutex::new(State {});

let is_foo = mutex.lock().unwrap().foo();
match is_foo {
    true => {
        mutex.lock().unwrap().bar();
    }
    false => {}
};

println!("All done!");
```