Skip to content

Commit

Permalink
Fix if_let_mutex not checking Mutexes behind refs
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslueg committed Aug 10, 2022
1 parent f7e2cb4 commit 6a73a45
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/if_let_mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/if_let_mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
14 changes: 13 additions & 1 deletion tests/ui/if_let_mutex.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6a73a45

Please sign in to comment.