-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add lint for &mut Mutex::lock
#6103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+133
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f82f9c2
Add lint for `&mut Mutex::lock`
FrancisMurillo fb8a9cb
Change lint doc test
FrancisMurillo ec0c3af
Run update_lints
FrancisMurillo 77e34a6
Inline is_mut_mutex_lock_call
FrancisMurillo 292cb9b
Use `sugg_lint_and_help`
FrancisMurillo e7e03b7
Change from correctness to style and MaybeIncorrect instead of Machin…
FrancisMurillo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.