Skip to content

Commit

Permalink
Fix mut_mutex_lock for Mutex behind imm deref
Browse files Browse the repository at this point in the history
Fixes #9415
  • Loading branch information
lukaslueg committed Sep 2, 2022
1 parent 334be18 commit ffc75af
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clippy_lints/src/methods/mut_mutex_lock.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/mut_mutex_lock.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
7 changes: 7 additions & 0 deletions tests/ui/mut_mutex_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

0 comments on commit ffc75af

Please sign in to comment.