Skip to content

Commit a42bd7a

Browse files
Add lint for &mut Mutex::lock
1 parent abce9e7 commit a42bd7a

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,7 @@ Released 2018-09-13
17211721
[`must_use_unit`]: https://rust-lang.github.io/rust-clippy/master/index.html#must_use_unit
17221722
[`mut_from_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_from_ref
17231723
[`mut_mut`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_mut
1724+
[`mut_mutex_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_mutex_lock
17241725
[`mut_range_bound`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_range_bound
17251726
[`mutable_key_type`]: https://rust-lang.github.io/rust-clippy/master/index.html#mutable_key_type
17261727
[`mutex_atomic`]: https://rust-lang.github.io/rust-clippy/master/index.html#mutex_atomic

clippy_lints/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ mod modulo_arithmetic;
254254
mod multiple_crate_versions;
255255
mod mut_key;
256256
mod mut_mut;
257+
mod mut_mutex_lock;
257258
mod mut_reference;
258259
mod mutable_debug_assertion;
259260
mod mutex_atomic;
@@ -736,6 +737,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
736737
&multiple_crate_versions::MULTIPLE_CRATE_VERSIONS,
737738
&mut_key::MUTABLE_KEY_TYPE,
738739
&mut_mut::MUT_MUT,
740+
&mut_mutex_lock::MUT_MUTEX_LOCK,
739741
&mut_reference::UNNECESSARY_MUT_PASSED,
740742
&mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL,
741743
&mutex_atomic::MUTEX_ATOMIC,
@@ -1101,6 +1103,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11011103
store.register_late_pass(|| box future_not_send::FutureNotSend);
11021104
store.register_late_pass(|| box utils::internal_lints::CollapsibleCalls);
11031105
store.register_late_pass(|| box if_let_mutex::IfLetMutex);
1106+
store.register_late_pass(|| box mut_mutex_lock::MutMutexLock);
11041107
store.register_late_pass(|| box match_on_vec_items::MatchOnVecItems);
11051108
store.register_early_pass(|| box manual_non_exhaustive::ManualNonExhaustive);
11061109
store.register_late_pass(|| box manual_async_fn::ManualAsyncFn);
@@ -1430,6 +1433,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14301433
LintId::of(&misc_early::UNNEEDED_WILDCARD_PATTERN),
14311434
LintId::of(&misc_early::ZERO_PREFIXED_LITERAL),
14321435
LintId::of(&mut_key::MUTABLE_KEY_TYPE),
1436+
LintId::of(&mut_mutex_lock::MUT_MUTEX_LOCK),
14331437
LintId::of(&mut_reference::UNNECESSARY_MUT_PASSED),
14341438
LintId::of(&mutex_atomic::MUTEX_ATOMIC),
14351439
LintId::of(&needless_arbitrary_self_type::NEEDLESS_ARBITRARY_SELF_TYPE),
@@ -1760,6 +1764,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
17601764
LintId::of(&misc::FLOAT_CMP),
17611765
LintId::of(&misc::MODULO_ONE),
17621766
LintId::of(&mut_key::MUTABLE_KEY_TYPE),
1767+
LintId::of(&mut_mutex_lock::MUT_MUTEX_LOCK),
17631768
LintId::of(&non_copy_const::BORROW_INTERIOR_MUTABLE_CONST),
17641769
LintId::of(&non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST),
17651770
LintId::of(&open_options::NONSENSICAL_OPEN_OPTIONS),

clippy_lints/src/mut_mutex_lock.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use crate::utils::{is_type_diagnostic_item, span_lint_and_help};
2+
use if_chain::if_chain;
3+
use rustc_hir::{Expr, ExprKind, Mutability};
4+
use rustc_lint::{LateContext, LateLintPass};
5+
use rustc_middle::ty;
6+
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
8+
declare_clippy_lint! {
9+
/// **What it does:** Checks for `&mut Mutex::lock` calls
10+
///
11+
/// **Why is this bad?** `Mutex::lock` is less efficient than
12+
/// calling `Mutex::get_mut`
13+
///
14+
/// **Known problems:** None.
15+
///
16+
/// **Example:**
17+
///
18+
/// ```rust
19+
/// use std::sync::{Arc, Mutex};
20+
///
21+
/// let mut value_rc = Arc::new(Mutex::new(42_u8));
22+
/// let value_mutex = Arc::get_mut(&mut value_rc).unwrap();
23+
///
24+
/// let value = value_mutex.lock().unwrap();
25+
/// do_stuff(value);
26+
/// ```
27+
/// Use instead:
28+
/// ```rust
29+
/// use std::sync::{Arc, Mutex};
30+
///
31+
/// let mut value_rc = Arc::new(Mutex::new(42_u8));
32+
/// let value_mutex = Arc::get_mut(&mut value_rc).unwrap();
33+
///
34+
/// let value = value_mutex.get_mut().unwrap();
35+
/// do_stuff(value);
36+
/// ```
37+
pub MUT_MUTEX_LOCK,
38+
correctness,
39+
"`&mut Mutex::lock` does unnecessary locking"
40+
}
41+
42+
declare_lint_pass!(MutMutexLock => [MUT_MUTEX_LOCK]);
43+
44+
impl<'tcx> LateLintPass<'tcx> for MutMutexLock {
45+
fn check_expr(&mut self, cx: &LateContext<'tcx>, ex: &'tcx Expr<'tcx>) {
46+
if_chain! {
47+
if is_mut_mutex_lock_call(cx, ex).is_some();
48+
then {
49+
span_lint_and_help(
50+
cx,
51+
MUT_MUTEX_LOCK,
52+
ex.span,
53+
"calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference",
54+
None,
55+
"use `&mut Mutex::get_mut` instead",
56+
);
57+
}
58+
}
59+
}
60+
}
61+
62+
fn is_mut_mutex_lock_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
63+
if_chain! {
64+
if let ExprKind::MethodCall(path, _span, args, _) = &expr.kind;
65+
if path.ident.name == sym!(lock);
66+
let ty = cx.typeck_results().expr_ty(&args[0]);
67+
if let ty::Ref(_, inner_ty, Mutability::Mut) = ty.kind();
68+
if is_type_diagnostic_item(cx, inner_ty, sym!(mutex_type));
69+
then {
70+
Some(&args[0])
71+
} else {
72+
None
73+
}
74+
}
75+
}

src/lintlist/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,13 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
14801480
deprecation: None,
14811481
module: "mut_mut",
14821482
},
1483+
Lint {
1484+
name: "mut_mutex_lock",
1485+
group: "correctness",
1486+
desc: "`&mut Mutex::lock` does unnecessary locking",
1487+
deprecation: None,
1488+
module: "mut_mutex_lock",
1489+
},
14831490
Lint {
14841491
name: "mut_range_bound",
14851492
group: "complexity",

tests/ui/mut_mutex_lock.rs

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

tests/ui/mut_mutex_lock.stderr

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

0 commit comments

Comments
 (0)