Fix if_let_mutex not checking Mutexes behind refs

Fixes #9193
This commit is contained in:
Lukas Lueg 2022-08-10 19:40:42 +02:00
parent f7e2cb4470
commit 6a73a45418
3 changed files with 22 additions and 2 deletions

View File

@ -129,7 +129,7 @@ fn is_mutex_lock_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Opt
if_chain! {
if let ExprKind::MethodCall(path, [self_arg, ..], _) = &expr.kind;
if path.ident.as_str() == "lock";
let ty = cx.typeck_results().expr_ty(self_arg);
let ty = cx.typeck_results().expr_ty(self_arg).peel_refs();
if is_type_diagnostic_item(cx, ty, sym::Mutex);
then {
Some(self_arg)

View File

@ -39,4 +39,12 @@ fn if_let_different_mutex() {
};
}
fn mutex_ref(mutex: &Mutex<i32>) {
if let Ok(i) = mutex.lock() {
do_stuff(i);
} else {
let _x = mutex.lock();
};
}
fn main() {}

View File

@ -25,5 +25,17 @@ LL | | };
|
= help: move the lock call outside of the `if let ...` expression
error: aborting due to 2 previous errors
error: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a deadlock
--> $DIR/if_let_mutex.rs:43:5
|
LL | / if let Ok(i) = mutex.lock() {
LL | | do_stuff(i);
LL | | } else {
LL | | let _x = mutex.lock();
LL | | };
| |_____^
|
= help: move the lock call outside of the `if let ...` expression
error: aborting due to 3 previous errors