Skip to content

Commit c36696a

Browse files
committed
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
2 parents 958a9cf + ffc75af commit c36696a

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

clippy_lints/src/methods/mut_mutex_lock.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::ty::is_type_diagnostic_item;
2+
use clippy_utils::{expr_custom_deref_adjustment, ty::is_type_diagnostic_item};
33
use if_chain::if_chain;
44
use rustc_errors::Applicability;
55
use rustc_hir::{Expr, Mutability};
@@ -11,6 +11,7 @@ use super::MUT_MUTEX_LOCK;
1111

1212
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &'tcx Expr<'tcx>, recv: &'tcx Expr<'tcx>, name_span: Span) {
1313
if_chain! {
14+
if matches!(expr_custom_deref_adjustment(cx, recv), None | Some(Mutability::Mut));
1415
if let ty::Ref(_, _, Mutability::Mut) = cx.typeck_results().expr_ty(recv).kind();
1516
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(ex.hir_id);
1617
if let Some(impl_id) = cx.tcx.impl_of_method(method_id);

tests/ui/mut_mutex_lock.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@ fn no_owned_mutex_lock() {
1818
*value += 1;
1919
}
2020

21+
fn issue9415() {
22+
let mut arc_mutex = Arc::new(Mutex::new(42_u8));
23+
let arc_mutex: &mut Arc<Mutex<u8>> = &mut arc_mutex;
24+
let mut guard = arc_mutex.lock().unwrap();
25+
*guard += 1;
26+
}
27+
2128
fn main() {}

tests/ui/mut_mutex_lock.rs

+7
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@ fn no_owned_mutex_lock() {
1818
*value += 1;
1919
}
2020

21+
fn issue9415() {
22+
let mut arc_mutex = Arc::new(Mutex::new(42_u8));
23+
let arc_mutex: &mut Arc<Mutex<u8>> = &mut arc_mutex;
24+
let mut guard = arc_mutex.lock().unwrap();
25+
*guard += 1;
26+
}
27+
2128
fn main() {}

0 commit comments

Comments
 (0)