Skip to content

Commit 399732b

Browse files
committed
Auto merge of rust-lang#6103 - FrancisMurillo:mut_mutex_lock, r=flip1995
Add lint for `&mut Mutex::lock` Fixes rust-lang#1765 changelog: Add lint [`mut_mutex_lock`] for `&mut Mutex::lock` and suggests using `&mut Mutex::get_mut` instead.
2 parents fd62c18 + e7e03b7 commit 399732b

File tree

7 files changed

+133
-0
lines changed

7 files changed

+133
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,7 @@ Released 2018-09-13
18431843
[`must_use_unit`]: https://rust-lang.github.io/rust-clippy/master/index.html#must_use_unit
18441844
[`mut_from_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_from_ref
18451845
[`mut_mut`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_mut
1846+
[`mut_mutex_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_mutex_lock
18461847
[`mut_range_bound`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_range_bound
18471848
[`mutable_key_type`]: https://rust-lang.github.io/rust-clippy/master/index.html#mutable_key_type
18481849
[`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
@@ -255,6 +255,7 @@ mod modulo_arithmetic;
255255
mod multiple_crate_versions;
256256
mod mut_key;
257257
mod mut_mut;
258+
mod mut_mutex_lock;
258259
mod mut_reference;
259260
mod mutable_debug_assertion;
260261
mod mutex_atomic;
@@ -746,6 +747,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
746747
&multiple_crate_versions::MULTIPLE_CRATE_VERSIONS,
747748
&mut_key::MUTABLE_KEY_TYPE,
748749
&mut_mut::MUT_MUT,
750+
&mut_mutex_lock::MUT_MUTEX_LOCK,
749751
&mut_reference::UNNECESSARY_MUT_PASSED,
750752
&mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL,
751753
&mutex_atomic::MUTEX_ATOMIC,
@@ -1115,6 +1117,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11151117
store.register_late_pass(|| box future_not_send::FutureNotSend);
11161118
store.register_late_pass(|| box utils::internal_lints::CollapsibleCalls);
11171119
store.register_late_pass(|| box if_let_mutex::IfLetMutex);
1120+
store.register_late_pass(|| box mut_mutex_lock::MutMutexLock);
11181121
store.register_late_pass(|| box match_on_vec_items::MatchOnVecItems);
11191122
store.register_early_pass(|| box manual_non_exhaustive::ManualNonExhaustive);
11201123
store.register_late_pass(|| box manual_async_fn::ManualAsyncFn);
@@ -1451,6 +1454,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14511454
LintId::of(&misc_early::UNNEEDED_WILDCARD_PATTERN),
14521455
LintId::of(&misc_early::ZERO_PREFIXED_LITERAL),
14531456
LintId::of(&mut_key::MUTABLE_KEY_TYPE),
1457+
LintId::of(&mut_mutex_lock::MUT_MUTEX_LOCK),
14541458
LintId::of(&mut_reference::UNNECESSARY_MUT_PASSED),
14551459
LintId::of(&mutex_atomic::MUTEX_ATOMIC),
14561460
LintId::of(&needless_arbitrary_self_type::NEEDLESS_ARBITRARY_SELF_TYPE),
@@ -1623,6 +1627,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16231627
LintId::of(&misc_early::DUPLICATE_UNDERSCORE_ARGUMENT),
16241628
LintId::of(&misc_early::MIXED_CASE_HEX_LITERALS),
16251629
LintId::of(&misc_early::REDUNDANT_PATTERN),
1630+
LintId::of(&mut_mutex_lock::MUT_MUTEX_LOCK),
16261631
LintId::of(&mut_reference::UNNECESSARY_MUT_PASSED),
16271632
LintId::of(&neg_multiply::NEG_MULTIPLY),
16281633
LintId::of(&new_without_default::NEW_WITHOUT_DEFAULT),

clippy_lints/src/mut_mutex_lock.rs

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

src/lintlist/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,13 @@ vec![
15021502
deprecation: None,
15031503
module: "mut_mut",
15041504
},
1505+
Lint {
1506+
name: "mut_mutex_lock",
1507+
group: "style",
1508+
desc: "`&mut Mutex::lock` does unnecessary locking",
1509+
deprecation: None,
1510+
module: "mut_mutex_lock",
1511+
},
15051512
Lint {
15061513
name: "mut_range_bound",
15071514
group: "complexity",

tests/ui/mut_mutex_lock.fixed

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

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+
// run-rustfix
2+
#![allow(dead_code, unused_mut)]
3+
#![warn(clippy::mut_mutex_lock)]
4+
5+
use std::sync::{Arc, Mutex};
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 mut value = value_mutex.lock().unwrap();
12+
*value += 1;
13+
}
14+
15+
fn no_owned_mutex_lock() {
16+
let mut value_rc = Arc::new(Mutex::new(42_u8));
17+
let mut value = value_rc.lock().unwrap();
18+
*value += 1;
19+
}
20+
21+
fn main() {}

tests/ui/mut_mutex_lock.stderr

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

0 commit comments

Comments
 (0)