Skip to content

Commit 62c1623

Browse files
Use sugg_lint_and_help
1 parent 978a2ca commit 62c1623

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

clippy_lints/src/mut_mutex_lock.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use crate::utils::{is_type_diagnostic_item, span_lint_and_help};
1+
use crate::utils::{is_type_diagnostic_item, span_lint_and_sugg};
22
use if_chain::if_chain;
3+
use rustc_errors::Applicability;
34
use rustc_hir::{Expr, ExprKind, Mutability};
45
use rustc_lint::{LateContext, LateLintPass};
56
use rustc_middle::ty;
@@ -9,7 +10,9 @@ declare_clippy_lint! {
910
/// **What it does:** Checks for `&mut Mutex::lock` calls
1011
///
1112
/// **Why is this bad?** `Mutex::lock` is less efficient than
12-
/// calling `Mutex::get_mut`
13+
/// calling `Mutex::get_mut`. In addition you also have a statically
14+
/// guarantee that the mutex isn't locked, instead of just a runtime
15+
/// guarantee.
1316
///
1417
/// **Known problems:** None.
1518
///
@@ -44,19 +47,20 @@ declare_lint_pass!(MutMutexLock => [MUT_MUTEX_LOCK]);
4447
impl<'tcx> LateLintPass<'tcx> for MutMutexLock {
4548
fn check_expr(&mut self, cx: &LateContext<'tcx>, ex: &'tcx Expr<'tcx>) {
4649
if_chain! {
47-
if let ExprKind::MethodCall(path, _span, args, _) = &ex.kind;
50+
if let ExprKind::MethodCall(path, method_span, args, _) = &ex.kind;
4851
if path.ident.name == sym!(lock);
4952
let ty = cx.typeck_results().expr_ty(&args[0]);
5053
if let ty::Ref(_, inner_ty, Mutability::Mut) = ty.kind();
5154
if is_type_diagnostic_item(cx, inner_ty, sym!(mutex_type));
5255
then {
53-
span_lint_and_help(
56+
span_lint_and_sugg(
5457
cx,
5558
MUT_MUTEX_LOCK,
56-
ex.span,
59+
*method_span,
5760
"calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference",
58-
None,
59-
"use `&mut Mutex::get_mut` instead",
61+
"change this to",
62+
"get_mut".to_owned(),
63+
Applicability::MachineApplicable,
6064
);
6165
}
6266
}

tests/ui/mut_mutex_lock.fixed

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// run-rustfix
2+
#![warn(clippy::mut_mutex_lock)]
3+
4+
use std::sync::{Arc, Mutex};
5+
6+
fn mut_mutex_lock() {
7+
let mut value_rc = Arc::new(Mutex::new(42_u8));
8+
let value_mutex = Arc::get_mut(&mut value_rc).unwrap();
9+
10+
let mut value = value_mutex.get_mut().unwrap();
11+
*value += 1;
12+
}
13+
14+
fn no_owned_mutex_lock() {
15+
let mut value_rc = Arc::new(Mutex::new(42_u8));
16+
let mut value = value_rc.lock().unwrap();
17+
*value += 1;
18+
}
19+
20+
fn main() {}

tests/ui/mut_mutex_lock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// run-rustfix
12
#![warn(clippy::mut_mutex_lock)]
23

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

tests/ui/mut_mutex_lock.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
2-
--> $DIR/mut_mutex_lock.rs:9:21
2+
--> $DIR/mut_mutex_lock.rs:10:33
33
|
44
LL | let mut value = value_mutex.lock().unwrap();
5-
| ^^^^^^^^^^^^^^^^^^
5+
| ^^^^ help: change this to: `get_mut`
66
|
77
= note: `-D clippy::mut-mutex-lock` implied by `-D warnings`
8-
= help: use `&mut Mutex::get_mut` instead
98

109
error: aborting due to previous error
1110

0 commit comments

Comments
 (0)