Skip to content

Commit f82f9c2

Browse files
Add lint for &mut Mutex::lock
1 parent a675778 commit f82f9c2

File tree

6 files changed

+128
-0
lines changed

6 files changed

+128
-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: 7 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;
@@ -744,6 +745,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
744745
&multiple_crate_versions::MULTIPLE_CRATE_VERSIONS,
745746
&mut_key::MUTABLE_KEY_TYPE,
746747
&mut_mut::MUT_MUT,
748+
&mut_mutex_lock::MUT_MUTEX_LOCK,
747749
&mut_reference::UNNECESSARY_MUT_PASSED,
748750
&mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL,
749751
&mutex_atomic::MUTEX_ATOMIC,
@@ -1112,6 +1114,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11121114
store.register_late_pass(|| box future_not_send::FutureNotSend);
11131115
store.register_late_pass(|| box utils::internal_lints::CollapsibleCalls);
11141116
store.register_late_pass(|| box if_let_mutex::IfLetMutex);
1117+
store.register_late_pass(|| box mut_mutex_lock::MutMutexLock);
11151118
store.register_late_pass(|| box match_on_vec_items::MatchOnVecItems);
11161119
store.register_early_pass(|| box manual_non_exhaustive::ManualNonExhaustive);
11171120
store.register_late_pass(|| box manual_async_fn::ManualAsyncFn);
@@ -1446,6 +1449,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14461449
LintId::of(&misc_early::UNNEEDED_WILDCARD_PATTERN),
14471450
LintId::of(&misc_early::ZERO_PREFIXED_LITERAL),
14481451
LintId::of(&mut_key::MUTABLE_KEY_TYPE),
1452+
LintId::of(&mut_mutex_lock::MUT_MUTEX_LOCK),
14491453
LintId::of(&mut_reference::UNNECESSARY_MUT_PASSED),
14501454
LintId::of(&mutex_atomic::MUTEX_ATOMIC),
14511455
LintId::of(&needless_arbitrary_self_type::NEEDLESS_ARBITRARY_SELF_TYPE),
@@ -1780,6 +1784,9 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
17801784
LintId::of(&misc::FLOAT_CMP),
17811785
LintId::of(&misc::MODULO_ONE),
17821786
LintId::of(&mut_key::MUTABLE_KEY_TYPE),
1787+
LintId::of(&mut_mutex_lock::MUT_MUTEX_LOCK),
1788+
LintId::of(&non_copy_const::BORROW_INTERIOR_MUTABLE_CONST),
1789+
LintId::of(&non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST),
17831790
LintId::of(&open_options::NONSENSICAL_OPEN_OPTIONS),
17841791
LintId::of(&option_env_unwrap::OPTION_ENV_UNWRAP),
17851792
LintId::of(&ptr::MUT_FROM_REF),

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
@@ -1502,6 +1502,13 @@ vec![
15021502
deprecation: None,
15031503
module: "mut_mut",
15041504
},
1505+
Lint {
1506+
name: "mut_mutex_lock",
1507+
group: "correctness",
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.rs

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

tests/ui/mut_mutex_lock.stderr

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0596]: cannot borrow `value` as mutable, as it is not declared as mutable
2+
--> $DIR/mut_mutex_lock.rs:10:6
3+
|
4+
LL | let value = value_mutex.lock().unwrap();
5+
| ----- help: consider changing this to be mutable: `mut value`
6+
LL | *value += 1;
7+
| ^^^^^ cannot borrow as mutable
8+
9+
error[E0596]: cannot borrow `value` as mutable, as it is not declared as mutable
10+
--> $DIR/mut_mutex_lock.rs:16:6
11+
|
12+
LL | let value = value_rc.lock().unwrap();
13+
| ----- help: consider changing this to be mutable: `mut value`
14+
LL | *value += 1;
15+
| ^^^^^ cannot borrow as mutable
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0596`.

0 commit comments

Comments
 (0)