Fix mut_mutex_lock for Mutex behind imm deref

Fixes #9415
This commit is contained in:
Lukas Lueg 2022-09-02 20:28:00 +02:00
parent 334be18a74
commit ffc75af4cd
3 changed files with 16 additions and 1 deletions

View file

@ -1,5 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_sugg; 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 if_chain::if_chain;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::{Expr, Mutability}; 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) { pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &'tcx Expr<'tcx>, recv: &'tcx Expr<'tcx>, name_span: Span) {
if_chain! { 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 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(method_id) = cx.typeck_results().type_dependent_def_id(ex.hir_id);
if let Some(impl_id) = cx.tcx.impl_of_method(method_id); if let Some(impl_id) = cx.tcx.impl_of_method(method_id);

View file

@ -18,4 +18,11 @@ fn no_owned_mutex_lock() {
*value += 1; *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() {} fn main() {}

View file

@ -18,4 +18,11 @@ fn no_owned_mutex_lock() {
*value += 1; *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() {} fn main() {}