Skip to content

Commit ffc75af

Browse files
committed
Fix mut_mutex_lock for Mutex behind imm deref
Fixes rust-lang#9415
1 parent 334be18 commit ffc75af

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

clippy_lints/src/methods/mut_mutex_lock.rs

Lines changed: 2 additions & 1 deletion
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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)