blob: cb6915e0ebaaa2c41eb12e39b533384793c32770 (
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
44
45
46
47
48
49
50
51
52
53
|
#![warn(clippy::if_let_mutex)]
use std::ops::Deref;
use std::sync::Mutex;
fn do_stuff<T>(_: T) {}
fn if_let() {
let m = Mutex::new(1_u8);
if let Err(locked) = m.lock() {
//~^ ERROR: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a d
do_stuff(locked);
} else {
let lock = m.lock().unwrap();
do_stuff(lock);
};
}
// This is the most common case as the above case is pretty
// contrived.
fn if_let_option() {
let m = Mutex::new(Some(0_u8));
if let Some(locked) = m.lock().unwrap().deref() {
//~^ ERROR: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a d
do_stuff(locked);
} else {
let lock = m.lock().unwrap();
do_stuff(lock);
};
}
// When mutexes are different don't warn
fn if_let_different_mutex() {
let m = Mutex::new(Some(0_u8));
let other = Mutex::new(None::<u8>);
if let Some(locked) = m.lock().unwrap().deref() {
do_stuff(locked);
} else {
let lock = other.lock().unwrap();
do_stuff(lock);
};
}
fn mutex_ref(mutex: &Mutex<i32>) {
if let Ok(i) = mutex.lock() {
//~^ ERROR: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a d
do_stuff(i);
} else {
let _x = mutex.lock();
};
}
fn main() {}
|