rust/tests/ui/if_let_mutex.rs

54 lines
1.3 KiB
Rust
Raw Normal View History

#![warn(clippy::if_let_mutex)]
use std::ops::Deref;
use std::sync::Mutex;
2020-03-17 20:51:43 -05:00
fn do_stuff<T>(_: T) {}
fn if_let() {
let m = Mutex::new(1_u8);
2020-03-17 20:51:43 -05:00
if let Err(locked) = m.lock() {
//~^ ERROR: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a d
2020-03-17 20:51:43 -05:00
do_stuff(locked);
} else {
2020-03-17 20:51:43 -05:00
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);
};
}
2022-07-13 09:48:32 -05:00
// 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() {}