Skip to content

Commit 6a73a45

Browse files
committed
Fix if_let_mutex not checking Mutexes behind refs
Fixes #9193
1 parent f7e2cb4 commit 6a73a45

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

clippy_lints/src/if_let_mutex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ fn is_mutex_lock_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Opt
129129
if_chain! {
130130
if let ExprKind::MethodCall(path, [self_arg, ..], _) = &expr.kind;
131131
if path.ident.as_str() == "lock";
132-
let ty = cx.typeck_results().expr_ty(self_arg);
132+
let ty = cx.typeck_results().expr_ty(self_arg).peel_refs();
133133
if is_type_diagnostic_item(cx, ty, sym::Mutex);
134134
then {
135135
Some(self_arg)

tests/ui/if_let_mutex.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,12 @@ fn if_let_different_mutex() {
3939
};
4040
}
4141

42+
fn mutex_ref(mutex: &Mutex<i32>) {
43+
if let Ok(i) = mutex.lock() {
44+
do_stuff(i);
45+
} else {
46+
let _x = mutex.lock();
47+
};
48+
}
49+
4250
fn main() {}

tests/ui/if_let_mutex.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,17 @@ LL | | };
2525
|
2626
= help: move the lock call outside of the `if let ...` expression
2727

28-
error: aborting due to 2 previous errors
28+
error: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a deadlock
29+
--> $DIR/if_let_mutex.rs:43:5
30+
|
31+
LL | / if let Ok(i) = mutex.lock() {
32+
LL | | do_stuff(i);
33+
LL | | } else {
34+
LL | | let _x = mutex.lock();
35+
LL | | };
36+
| |_____^
37+
|
38+
= help: move the lock call outside of the `if let ...` expression
39+
40+
error: aborting due to 3 previous errors
2941

0 commit comments

Comments
 (0)