]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | #![warn(clippy::if_let_mutex)] |
2 | ||
3 | use std::ops::Deref; | |
4 | use std::sync::Mutex; | |
5 | ||
6 | fn do_stuff<T>(_: T) {} | |
7 | ||
8 | fn if_let() { | |
9 | let m = Mutex::new(1_u8); | |
10 | if let Err(locked) = m.lock() { | |
11 | do_stuff(locked); | |
12 | } else { | |
13 | let lock = m.lock().unwrap(); | |
14 | do_stuff(lock); | |
15 | }; | |
16 | } | |
17 | ||
18 | // This is the most common case as the above case is pretty | |
19 | // contrived. | |
20 | fn if_let_option() { | |
21 | let m = Mutex::new(Some(0_u8)); | |
22 | if let Some(locked) = m.lock().unwrap().deref() { | |
23 | do_stuff(locked); | |
24 | } else { | |
25 | let lock = m.lock().unwrap(); | |
26 | do_stuff(lock); | |
27 | }; | |
28 | } | |
29 | ||
30 | // When mutexs are different don't warn | |
31 | fn if_let_different_mutex() { | |
32 | let m = Mutex::new(Some(0_u8)); | |
33 | let other = Mutex::new(None::<u8>); | |
34 | if let Some(locked) = m.lock().unwrap().deref() { | |
35 | do_stuff(locked); | |
36 | } else { | |
37 | let lock = other.lock().unwrap(); | |
38 | do_stuff(lock); | |
39 | }; | |
40 | } | |
41 | ||
42 | fn main() {} |