mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-24 21:05:12 +00:00
Auto merge of #6103 - FrancisMurillo:mut_mutex_lock, r=flip1995
Add lint for `&mut Mutex::lock` Fixes #1765 changelog: Add lint [`mut_mutex_lock`] for `&mut Mutex::lock` and suggests using `&mut Mutex::get_mut` instead.
This commit is contained in:
commit
399732bf79
@ -1843,6 +1843,7 @@ Released 2018-09-13
|
||||
[`must_use_unit`]: https://rust-lang.github.io/rust-clippy/master/index.html#must_use_unit
|
||||
[`mut_from_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_from_ref
|
||||
[`mut_mut`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_mut
|
||||
[`mut_mutex_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_mutex_lock
|
||||
[`mut_range_bound`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_range_bound
|
||||
[`mutable_key_type`]: https://rust-lang.github.io/rust-clippy/master/index.html#mutable_key_type
|
||||
[`mutex_atomic`]: https://rust-lang.github.io/rust-clippy/master/index.html#mutex_atomic
|
||||
|
@ -255,6 +255,7 @@ mod modulo_arithmetic;
|
||||
mod multiple_crate_versions;
|
||||
mod mut_key;
|
||||
mod mut_mut;
|
||||
mod mut_mutex_lock;
|
||||
mod mut_reference;
|
||||
mod mutable_debug_assertion;
|
||||
mod mutex_atomic;
|
||||
@ -746,6 +747,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||
&multiple_crate_versions::MULTIPLE_CRATE_VERSIONS,
|
||||
&mut_key::MUTABLE_KEY_TYPE,
|
||||
&mut_mut::MUT_MUT,
|
||||
&mut_mutex_lock::MUT_MUTEX_LOCK,
|
||||
&mut_reference::UNNECESSARY_MUT_PASSED,
|
||||
&mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL,
|
||||
&mutex_atomic::MUTEX_ATOMIC,
|
||||
@ -1115,6 +1117,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||
store.register_late_pass(|| box future_not_send::FutureNotSend);
|
||||
store.register_late_pass(|| box utils::internal_lints::CollapsibleCalls);
|
||||
store.register_late_pass(|| box if_let_mutex::IfLetMutex);
|
||||
store.register_late_pass(|| box mut_mutex_lock::MutMutexLock);
|
||||
store.register_late_pass(|| box match_on_vec_items::MatchOnVecItems);
|
||||
store.register_early_pass(|| box manual_non_exhaustive::ManualNonExhaustive);
|
||||
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:
|
||||
LintId::of(&misc_early::UNNEEDED_WILDCARD_PATTERN),
|
||||
LintId::of(&misc_early::ZERO_PREFIXED_LITERAL),
|
||||
LintId::of(&mut_key::MUTABLE_KEY_TYPE),
|
||||
LintId::of(&mut_mutex_lock::MUT_MUTEX_LOCK),
|
||||
LintId::of(&mut_reference::UNNECESSARY_MUT_PASSED),
|
||||
LintId::of(&mutex_atomic::MUTEX_ATOMIC),
|
||||
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:
|
||||
LintId::of(&misc_early::DUPLICATE_UNDERSCORE_ARGUMENT),
|
||||
LintId::of(&misc_early::MIXED_CASE_HEX_LITERALS),
|
||||
LintId::of(&misc_early::REDUNDANT_PATTERN),
|
||||
LintId::of(&mut_mutex_lock::MUT_MUTEX_LOCK),
|
||||
LintId::of(&mut_reference::UNNECESSARY_MUT_PASSED),
|
||||
LintId::of(&neg_multiply::NEG_MULTIPLY),
|
||||
LintId::of(&new_without_default::NEW_WITHOUT_DEFAULT),
|
||||
|
68
clippy_lints/src/mut_mutex_lock.rs
Normal file
68
clippy_lints/src/mut_mutex_lock.rs
Normal file
@ -0,0 +1,68 @@
|
||||
use crate::utils::{is_type_diagnostic_item, span_lint_and_sugg};
|
||||
use if_chain::if_chain;
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{Expr, ExprKind, Mutability};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_middle::ty;
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// **What it does:** Checks for `&mut Mutex::lock` calls
|
||||
///
|
||||
/// **Why is this bad?** `Mutex::lock` is less efficient than
|
||||
/// calling `Mutex::get_mut`. In addition you also have a statically
|
||||
/// guarantee that the mutex isn't locked, instead of just a runtime
|
||||
/// guarantee.
|
||||
///
|
||||
/// **Known problems:** None.
|
||||
///
|
||||
/// **Example:**
|
||||
///
|
||||
/// ```rust
|
||||
/// use std::sync::{Arc, Mutex};
|
||||
///
|
||||
/// let mut value_rc = Arc::new(Mutex::new(42_u8));
|
||||
/// let value_mutex = Arc::get_mut(&mut value_rc).unwrap();
|
||||
///
|
||||
/// let mut value = value_mutex.lock().unwrap();
|
||||
/// *value += 1;
|
||||
/// ```
|
||||
/// Use instead:
|
||||
/// ```rust
|
||||
/// use std::sync::{Arc, Mutex};
|
||||
///
|
||||
/// let mut value_rc = Arc::new(Mutex::new(42_u8));
|
||||
/// let value_mutex = Arc::get_mut(&mut value_rc).unwrap();
|
||||
///
|
||||
/// let value = value_mutex.get_mut().unwrap();
|
||||
/// *value += 1;
|
||||
/// ```
|
||||
pub MUT_MUTEX_LOCK,
|
||||
style,
|
||||
"`&mut Mutex::lock` does unnecessary locking"
|
||||
}
|
||||
|
||||
declare_lint_pass!(MutMutexLock => [MUT_MUTEX_LOCK]);
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for MutMutexLock {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, ex: &'tcx Expr<'tcx>) {
|
||||
if_chain! {
|
||||
if let ExprKind::MethodCall(path, method_span, args, _) = &ex.kind;
|
||||
if path.ident.name == sym!(lock);
|
||||
let ty = cx.typeck_results().expr_ty(&args[0]);
|
||||
if let ty::Ref(_, inner_ty, Mutability::Mut) = ty.kind();
|
||||
if is_type_diagnostic_item(cx, inner_ty, sym!(mutex_type));
|
||||
then {
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
MUT_MUTEX_LOCK,
|
||||
*method_span,
|
||||
"calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference",
|
||||
"change this to",
|
||||
"get_mut".to_owned(),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1502,6 +1502,13 @@ vec![
|
||||
deprecation: None,
|
||||
module: "mut_mut",
|
||||
},
|
||||
Lint {
|
||||
name: "mut_mutex_lock",
|
||||
group: "style",
|
||||
desc: "`&mut Mutex::lock` does unnecessary locking",
|
||||
deprecation: None,
|
||||
module: "mut_mutex_lock",
|
||||
},
|
||||
Lint {
|
||||
name: "mut_range_bound",
|
||||
group: "complexity",
|
||||
|
21
tests/ui/mut_mutex_lock.fixed
Normal file
21
tests/ui/mut_mutex_lock.fixed
Normal file
@ -0,0 +1,21 @@
|
||||
// run-rustfix
|
||||
#![allow(dead_code, unused_mut)]
|
||||
#![warn(clippy::mut_mutex_lock)]
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
fn mut_mutex_lock() {
|
||||
let mut value_rc = Arc::new(Mutex::new(42_u8));
|
||||
let value_mutex = Arc::get_mut(&mut value_rc).unwrap();
|
||||
|
||||
let mut value = value_mutex.get_mut().unwrap();
|
||||
*value += 1;
|
||||
}
|
||||
|
||||
fn no_owned_mutex_lock() {
|
||||
let mut value_rc = Arc::new(Mutex::new(42_u8));
|
||||
let mut value = value_rc.lock().unwrap();
|
||||
*value += 1;
|
||||
}
|
||||
|
||||
fn main() {}
|
21
tests/ui/mut_mutex_lock.rs
Normal file
21
tests/ui/mut_mutex_lock.rs
Normal file
@ -0,0 +1,21 @@
|
||||
// run-rustfix
|
||||
#![allow(dead_code, unused_mut)]
|
||||
#![warn(clippy::mut_mutex_lock)]
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
fn mut_mutex_lock() {
|
||||
let mut value_rc = Arc::new(Mutex::new(42_u8));
|
||||
let value_mutex = Arc::get_mut(&mut value_rc).unwrap();
|
||||
|
||||
let mut value = value_mutex.lock().unwrap();
|
||||
*value += 1;
|
||||
}
|
||||
|
||||
fn no_owned_mutex_lock() {
|
||||
let mut value_rc = Arc::new(Mutex::new(42_u8));
|
||||
let mut value = value_rc.lock().unwrap();
|
||||
*value += 1;
|
||||
}
|
||||
|
||||
fn main() {}
|
10
tests/ui/mut_mutex_lock.stderr
Normal file
10
tests/ui/mut_mutex_lock.stderr
Normal file
@ -0,0 +1,10 @@
|
||||
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
|
||||
--> $DIR/mut_mutex_lock.rs:11:33
|
||||
|
|
||||
LL | let mut value = value_mutex.lock().unwrap();
|
||||
| ^^^^ help: change this to: `get_mut`
|
||||
|
|
||||
= note: `-D clippy::mut-mutex-lock` implied by `-D warnings`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
Loading…
Reference in New Issue
Block a user