2024-11-16 09:52:42 +00:00
|
|
|
//@ compile-flags: -Zunstable-options
|
|
|
|
|
|
|
|
//@revisions: edition2021 edition2024
|
|
|
|
//@[edition2021] edition:2021
|
|
|
|
//@[edition2024] edition:2024
|
2020-03-05 13:36:19 +00:00
|
|
|
#![warn(clippy::if_let_mutex)]
|
2024-07-28 16:27:53 +00:00
|
|
|
#![allow(clippy::redundant_pattern_matching)]
|
2020-03-05 13:36:19 +00:00
|
|
|
|
2020-04-15 21:08:26 +00:00
|
|
|
use std::ops::Deref;
|
2020-03-05 13:36:19 +00:00
|
|
|
use std::sync::Mutex;
|
|
|
|
|
2020-03-18 01:51:43 +00:00
|
|
|
fn do_stuff<T>(_: T) {}
|
2020-03-05 13:36:19 +00:00
|
|
|
|
2020-03-18 22:13:06 +00:00
|
|
|
fn if_let() {
|
2020-04-15 21:08:26 +00:00
|
|
|
let m = Mutex::new(1_u8);
|
2020-03-18 01:51:43 +00:00
|
|
|
if let Err(locked) = m.lock() {
|
2024-11-16 09:52:42 +00:00
|
|
|
//~[edition2021]^ if_let_mutex
|
2020-03-18 01:51:43 +00:00
|
|
|
do_stuff(locked);
|
2020-03-05 13:36:19 +00:00
|
|
|
} else {
|
2020-03-18 01:51:43 +00:00
|
|
|
let lock = m.lock().unwrap();
|
|
|
|
do_stuff(lock);
|
2020-03-05 13:36:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-20 19:08:44 +00:00
|
|
|
// This is the most common case as the above case is pretty
|
|
|
|
// contrived.
|
2020-04-15 21:08:26 +00:00
|
|
|
fn if_let_option() {
|
|
|
|
let m = Mutex::new(Some(0_u8));
|
|
|
|
if let Some(locked) = m.lock().unwrap().deref() {
|
2024-11-16 09:52:42 +00:00
|
|
|
//~[edition2021]^ if_let_mutex
|
2020-04-15 21:08:26 +00:00
|
|
|
do_stuff(locked);
|
|
|
|
} else {
|
|
|
|
let lock = m.lock().unwrap();
|
|
|
|
do_stuff(lock);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:48:32 +00:00
|
|
|
// When mutexes are different don't warn
|
2020-04-20 19:08:44 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-10 17:40:42 +00:00
|
|
|
fn mutex_ref(mutex: &Mutex<i32>) {
|
|
|
|
if let Ok(i) = mutex.lock() {
|
2024-11-16 09:52:42 +00:00
|
|
|
//~[edition2021]^ if_let_mutex
|
2022-08-10 17:40:42 +00:00
|
|
|
do_stuff(i);
|
|
|
|
} else {
|
|
|
|
let _x = mutex.lock();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-07-28 16:27:53 +00:00
|
|
|
fn multiple_mutexes(m1: &Mutex<()>, m2: &Mutex<()>) {
|
|
|
|
if let Ok(_) = m1.lock() {
|
|
|
|
m2.lock();
|
|
|
|
} else {
|
|
|
|
m1.lock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 13:36:19 +00:00
|
|
|
fn main() {}
|