From 6a73a454186c6795e692108b878492f8e25cd558 Mon Sep 17 00:00:00 2001 From: Lukas Lueg Date: Wed, 10 Aug 2022 19:40:42 +0200 Subject: [PATCH] Fix if_let_mutex not checking Mutexes behind refs Fixes #9193 --- clippy_lints/src/if_let_mutex.rs | 2 +- tests/ui/if_let_mutex.rs | 8 ++++++++ tests/ui/if_let_mutex.stderr | 14 +++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/if_let_mutex.rs b/clippy_lints/src/if_let_mutex.rs index e950170078493..7c12a638529d8 100644 --- a/clippy_lints/src/if_let_mutex.rs +++ b/clippy_lints/src/if_let_mutex.rs @@ -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) diff --git a/tests/ui/if_let_mutex.rs b/tests/ui/if_let_mutex.rs index 6cbfafbb38b99..321feb0224ed1 100644 --- a/tests/ui/if_let_mutex.rs +++ b/tests/ui/if_let_mutex.rs @@ -39,4 +39,12 @@ fn if_let_different_mutex() { }; } +fn mutex_ref(mutex: &Mutex) { + if let Ok(i) = mutex.lock() { + do_stuff(i); + } else { + let _x = mutex.lock(); + }; +} + fn main() {} diff --git a/tests/ui/if_let_mutex.stderr b/tests/ui/if_let_mutex.stderr index e9c4d9163328f..a831090c1f98f 100644 --- a/tests/ui/if_let_mutex.stderr +++ b/tests/ui/if_let_mutex.stderr @@ -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