]> git.lizzy.rs Git - rust.git/commitdiff
Use `sugg_lint_and_help`
authorFrancis Murillo <francis.murillo@protonmail.com>
Sat, 10 Oct 2020 10:07:47 +0000 (18:07 +0800)
committerFrancis Murillo <francis.murillo@protonmail.com>
Sun, 25 Oct 2020 09:41:30 +0000 (17:41 +0800)
clippy_lints/src/mut_mutex_lock.rs
tests/ui/mut_mutex_lock.fixed [new file with mode: 0644]
tests/ui/mut_mutex_lock.rs
tests/ui/mut_mutex_lock.stderr

index ca3371a5d75c2a17a1b4ee48a1c12a55762e7b78..82ed2d6d69c35bc85f64848806416dfd8d88675a 100644 (file)
@@ -1,5 +1,6 @@
-use crate::utils::{is_type_diagnostic_item, span_lint_and_help};
+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;
@@ -9,7 +10,9 @@
     /// **What it does:** Checks for `&mut Mutex::lock` calls
     ///
     /// **Why is this bad?** `Mutex::lock` is less efficient than
-    /// calling `Mutex::get_mut`
+    /// 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.
     ///
 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, _span, args, _) = &ex.kind;
+            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_help(
+                span_lint_and_sugg(
                     cx,
                     MUT_MUTEX_LOCK,
-                    ex.span,
+                    *method_span,
                     "calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference",
-                    None,
-                    "use `&mut Mutex::get_mut` instead",
+                    "change this to",
+                    "get_mut".to_owned(),
+                    Applicability::MachineApplicable,
                 );
             }
         }
diff --git a/tests/ui/mut_mutex_lock.fixed b/tests/ui/mut_mutex_lock.fixed
new file mode 100644 (file)
index 0000000..36bc52e
--- /dev/null
@@ -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() {}
index 9cd98e90c29df3b627e6f93484393140b76f7170..ea60df5ae1bbc92cda19453e4756aefea50833b3 100644 (file)
@@ -1,3 +1,5 @@
+// run-rustfix
+#![allow(dead_code, unused_mut)]
 #![warn(clippy::mut_mutex_lock)]
 
 use std::sync::{Arc, Mutex};
index d521ebb56c4310970de892bff615a2312c0d5132..21c1b3486cacf5f2a23a90c16e02e268b8ff0f6b 100644 (file)
@@ -1,11 +1,10 @@
 error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
-  --> $DIR/mut_mutex_lock.rs:9:21
+  --> $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`
-   = help: use `&mut Mutex::get_mut` instead
 
 error: aborting due to previous error