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
This commit is contained in:
bors 2022-09-02 18:54:06 +00:00
commit c36696ac02
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::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);

View file

@ -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() {}

View file

@ -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() {}