Skip to content

Commit d7fd373

Browse files
committed
clean-up
1 parent 34fab5c commit d7fd373

File tree

5 files changed

+12
-14
lines changed

5 files changed

+12
-14
lines changed

clippy_lints/src/methods/mut_mutex_lock.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::expr_custom_deref_adjustment;
3-
use clippy_utils::res::MaybeDef;
3+
use clippy_utils::res::{MaybeDef, MaybeTypeckRes};
44
use clippy_utils::ty::peel_and_count_ty_refs;
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, Mutability};
@@ -9,12 +9,12 @@ use rustc_span::{Span, sym};
99

1010
use super::MUT_MUTEX_LOCK;
1111

12-
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &'tcx Expr<'tcx>, recv: &'tcx Expr<'tcx>, name_span: Span) {
12+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, recv: &'tcx Expr<'tcx>, name_span: Span) {
1313
if matches!(expr_custom_deref_adjustment(cx, recv), None | Some(Mutability::Mut))
14+
// NOTE: the reason we don't use `expr_ty_adjusted` here is that a call to `Mutex::lock` by itself
15+
// adjusts the receiver to be `&Mutex`
1416
&& let (_, _, Some(Mutability::Mut)) = peel_and_count_ty_refs(cx.typeck_results().expr_ty(recv))
15-
&& let Some(method_id) = cx.typeck_results().type_dependent_def_id(ex.hir_id)
16-
&& let Some(impl_id) = cx.tcx.impl_of_assoc(method_id)
17-
&& cx.tcx.type_of(impl_id).is_diag_item(cx, sym::Mutex)
17+
&& cx.ty_based_def(expr).opt_parent(cx).is_diag_item(cx, sym::Mutex)
1818
{
1919
span_lint_and_sugg(
2020
cx,

clippy_utils/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,9 @@ pub fn trait_ref_of_method<'tcx>(cx: &LateContext<'tcx>, owner: OwnerId) -> Opti
459459
/// This method will return tuple of projection stack and root of the expression,
460460
/// used in `can_mut_borrow_both`.
461461
///
462-
/// For example, if `e` represents the `v[0].a.b[x]`
463-
/// this method will return a tuple, composed of a `Vec`
464-
/// containing the `Expr`s for `v[0], v[0].a, v[0].a.b, v[0].a.b[x]`
465-
/// and an `Expr` for root of them, `v`
462+
/// For example, if `e` represents the `v[0].a.b[x]` this method will return a tuple, composed of:
463+
/// - a `Vec` containing the `Expr`s for `v[0], v[0].a, v[0].a.b, v[0].a.b[x]`
464+
/// - and an `Expr` for root of them, `v`
466465
fn projection_stack<'a, 'hir>(mut e: &'a Expr<'hir>) -> (Vec<&'a Expr<'hir>>, &'a Expr<'hir>) {
467466
let mut result = vec![];
468467
let root = loop {
@@ -488,7 +487,8 @@ pub fn expr_custom_deref_adjustment(cx: &LateContext<'_>, e: &Expr<'_>) -> Optio
488487
Adjust::Deref(None) => None,
489488
_ => Some(None),
490489
})
491-
.and_then(|x| x)
490+
// if there were no adjustments to begin with, trivially none of them are of the custom-deref kind
491+
.unwrap_or(None)
492492
}
493493

494494
/// Checks if two expressions can be mutably borrowed simultaneously

tests/ui/mut_mutex_lock.fixed

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![allow(dead_code, unused_mut)]
21
#![warn(clippy::mut_mutex_lock)]
32

43
use std::sync::{Arc, Mutex};

tests/ui/mut_mutex_lock.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![allow(dead_code, unused_mut)]
21
#![warn(clippy::mut_mutex_lock)]
32

43
use std::sync::{Arc, Mutex};

tests/ui/mut_mutex_lock.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
2-
--> tests/ui/mut_mutex_lock.rs:10:33
2+
--> tests/ui/mut_mutex_lock.rs:9:33
33
|
44
LL | let mut value = value_mutex.lock().unwrap();
55
| ^^^^ help: change this to: `get_mut`
@@ -8,7 +8,7 @@ LL | let mut value = value_mutex.lock().unwrap();
88
= help: to override `-D warnings` add `#[allow(clippy::mut_mutex_lock)]`
99

1010
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
11-
--> tests/ui/mut_mutex_lock.rs:16:43
11+
--> tests/ui/mut_mutex_lock.rs:15:43
1212
|
1313
LL | let mut value = mut_ref_mut_ref_mutex.lock().unwrap();
1414
| ^^^^ help: change this to: `get_mut`

0 commit comments

Comments
 (0)