-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not trigger if_let_mutex strating from Edition 2024
- Loading branch information
1 parent
b829d53
commit 2ce223a
Showing
4 changed files
with
81 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//@edition:2021 | ||
#![warn(clippy::if_let_mutex)] | ||
#![allow(clippy::redundant_pattern_matching)] | ||
|
||
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 multiple_mutexes(m1: &Mutex<()>, m2: &Mutex<()>) { | ||
if let Ok(_) = m1.lock() { | ||
m2.lock(); | ||
} else { | ||
m1.lock(); | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters