about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-09-02 18:54:06 +0000
committerbors <bors@rust-lang.org>2022-09-02 18:54:06 +0000
commitc36696ac0229ed706d3640177d6d90e232c6d615 (patch)
tree2bcea333276ebb6db188a0149611d42b9af3c8d6
parent958a9cf297d313c04c40b4b5164679b516e92885 (diff)
parentffc75af4cd01740e9daac43ff24d777525e506c1 (diff)
downloadrust-c36696ac0229ed706d3640177d6d90e232c6d615.tar.gz
rust-c36696ac0229ed706d3640177d6d90e232c6d615.zip
Auto merge of #9418 - lukaslueg:issue9415, r=llogiq
Fix `mut_mutex_lock` when Mutex is behind immutable deref

I *think* the problem here is the `if let ty::Ref(_, _, Mutability::Mut) = cx.typeck_results().expr_ty(recv).kind()` line tries to check if the `Mutex` can be mutably borrowed (there already is a test for `Arc<Mutex<_>>`), but gets bamboozled by the `&mut Arc` indirection. And I *think* checking the deref-adjustment to filter immutable-adjust (the deref through the `Arc`, starting from `&mut Arc`) is the correct fix.

Fixes #9415

changelog: Fix `mut_mutex_lock` when Mutex is behind immutable deref
-rw-r--r--clippy_lints/src/methods/mut_mutex_lock.rs3
-rw-r--r--tests/ui/mut_mutex_lock.fixed7
-rw-r--r--tests/ui/mut_mutex_lock.rs7
3 files changed, 16 insertions, 1 deletions
diff --git a/clippy_lints/src/methods/mut_mutex_lock.rs b/clippy_lints/src/methods/mut_mutex_lock.rs
index bd8458a222e..b9593b3687d 100644
--- a/clippy_lints/src/methods/mut_mutex_lock.rs
+++ b/clippy_lints/src/methods/mut_mutex_lock.rs
@@ -1,5 +1,5 @@
 use clippy_utils::diagnostics::span_lint_and_sugg;
-use clippy_utils::ty::is_type_diagnostic_item;
+use clippy_utils::{expr_custom_deref_adjustment, ty::is_type_diagnostic_item};
 use if_chain::if_chain;
 use rustc_errors::Applicability;
 use rustc_hir::{Expr, Mutability};
@@ -11,6 +11,7 @@ use super::MUT_MUTEX_LOCK;
 
 pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &'tcx Expr<'tcx>, recv: &'tcx Expr<'tcx>, name_span: Span) {
     if_chain! {
+        if matches!(expr_custom_deref_adjustment(cx, recv), None | Some(Mutability::Mut));
         if let ty::Ref(_, _, Mutability::Mut) = cx.typeck_results().expr_ty(recv).kind();
         if let Some(method_id) = cx.typeck_results().type_dependent_def_id(ex.hir_id);
         if let Some(impl_id) = cx.tcx.impl_of_method(method_id);
diff --git a/tests/ui/mut_mutex_lock.fixed b/tests/ui/mut_mutex_lock.fixed
index 36bc52e3374..ecad10a8290 100644
--- a/tests/ui/mut_mutex_lock.fixed
+++ b/tests/ui/mut_mutex_lock.fixed
@@ -18,4 +18,11 @@ fn no_owned_mutex_lock() {
     *value += 1;
 }
 
+fn issue9415() {
+    let mut arc_mutex = Arc::new(Mutex::new(42_u8));
+    let arc_mutex: &mut Arc<Mutex<u8>> = &mut arc_mutex;
+    let mut guard = arc_mutex.lock().unwrap();
+    *guard += 1;
+}
+
 fn main() {}
diff --git a/tests/ui/mut_mutex_lock.rs b/tests/ui/mut_mutex_lock.rs
index ea60df5ae1b..f2b1d6fbfbc 100644
--- a/tests/ui/mut_mutex_lock.rs
+++ b/tests/ui/mut_mutex_lock.rs
@@ -18,4 +18,11 @@ fn no_owned_mutex_lock() {
     *value += 1;
 }
 
+fn issue9415() {
+    let mut arc_mutex = Arc::new(Mutex::new(42_u8));
+    let arc_mutex: &mut Arc<Mutex<u8>> = &mut arc_mutex;
+    let mut guard = arc_mutex.lock().unwrap();
+    *guard += 1;
+}
+
 fn main() {}