]> git.lizzy.rs Git - rust.git/commitdiff
lint all guard types, not just lock functions
authorAreredify <misha-babenko@yandex.ru>
Thu, 30 Jan 2020 15:10:19 +0000 (18:10 +0300)
committerAreredify <misha-babenko@yandex.ru>
Thu, 30 Jan 2020 15:46:22 +0000 (18:46 +0300)
clippy_lints/src/let_underscore.rs
clippy_lints/src/utils/paths.rs
tests/ui/let_underscore_lock.rs
tests/ui/let_underscore_lock.stderr

index a43674316a1e5cedd5d656036f25e65fcbcf699b..c2a404ebee7cc0830296946a00f52b6ee3149777 100644 (file)
@@ -4,7 +4,7 @@
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_session::{declare_lint_pass, declare_tool_lint};
 
-use crate::utils::{is_must_use_func_call, is_must_use_ty, match_def_path, paths, span_lint_and_help};
+use crate::utils::{is_must_use_func_call, is_must_use_ty, match_type, paths, span_lint_and_help};
 
 declare_clippy_lint! {
     /// **What it does:** Checks for `let _ = <expr>`
 }
 
 declare_clippy_lint! {
-    /// **What it does:** Checks for `let _ = sync_primitive.lock()`
+    /// **What it does:** Checks for `let _ = sync_lock`
     ///
-    /// **Why is this bad?** This statement locks the synchronization
-    /// primitive and immediately drops the lock, which is probably
-    /// not intended. To extend lock lifetime to the end of the scope,
-    /// use an underscore-prefixed name instead (i.e. _lock).
+    /// **Why is this bad?** This statement immediately drops the lock instead of
+    /// extending it's lifetime to the end of the scope, which is often not intended.
+    /// To extend lock lifetime to the end of the scope, use an underscore-prefixed
+    /// name instead (i.e. _lock). If you want to explicitly drop the lock,
+    /// `std::mem::drop` conveys your intention better and is less error-prone.
     ///
     /// **Known problems:** None.
     ///
 
 declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK]);
 
-const LOCK_METHODS_PATHS: [&[&str]; 3] = [&paths::MUTEX_LOCK, &paths::RWLOCK_READ, &paths::RWLOCK_WRITE];
+const SYNC_GUARD_PATHS: [&[&str]; 3] = [
+    &paths::MUTEX_GUARD,
+    &paths::RWLOCK_READ_GUARD,
+    &paths::RWLOCK_WRITE_GUARD,
+];
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnderscore {
     fn check_stmt(&mut self, cx: &LateContext<'_, '_>, stmt: &Stmt<'_>) {
@@ -71,37 +76,32 @@ fn check_stmt(&mut self, cx: &LateContext<'_, '_>, stmt: &Stmt<'_>) {
             if let PatKind::Wild = local.pat.kind;
             if let Some(ref init) = local.init;
             then {
-                if_chain! {
-                    if let ExprKind::MethodCall(_, _, _) = init.kind;
-                    let method_did = cx.tables.type_dependent_def_id(init.hir_id).unwrap();
-                    if LOCK_METHODS_PATHS.iter().any(|path| match_def_path(cx, method_did, path));
-                    then {
-                        span_lint_and_help(
-                            cx,
-                            LET_UNDERSCORE_LOCK,
-                            stmt.span,
-                            "non-binding let on a synchronization lock",
-                            "consider using an underscore-prefixed named binding"
-                        )
-                    } else {
-                        if is_must_use_ty(cx, cx.tables.expr_ty(init)) {
-                            span_lint_and_help(
-                                cx,
-                                LET_UNDERSCORE_MUST_USE,
-                                stmt.span,
-                                "non-binding let on an expression with `#[must_use]` type",
-                                "consider explicitly using expression value"
-                            )
-                        } else if is_must_use_func_call(cx, init) {
-                            span_lint_and_help(
-                                cx,
-                                LET_UNDERSCORE_MUST_USE,
-                                stmt.span,
-                                "non-binding let on a result of a `#[must_use]` function",
-                                "consider explicitly using function result"
-                            )
-                        }
-                    }
+                let check_ty = |ty| SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, ty, path));
+                if cx.tables.expr_ty(init).walk().any(check_ty) {
+                    span_lint_and_help(
+                        cx,
+                        LET_UNDERSCORE_LOCK,
+                        stmt.span,
+                        "non-binding let on a synchronization lock",
+                        "consider using an underscore-prefixed named \
+                            binding or dropping explicitly with `std::mem::drop`"
+                    )
+                } else if is_must_use_ty(cx, cx.tables.expr_ty(init)) {
+                    span_lint_and_help(
+                        cx,
+                        LET_UNDERSCORE_MUST_USE,
+                        stmt.span,
+                        "non-binding let on an expression with `#[must_use]` type",
+                        "consider explicitly using expression value"
+                    )
+                } else if is_must_use_func_call(cx, init) {
+                    span_lint_and_help(
+                        cx,
+                        LET_UNDERSCORE_MUST_USE,
+                        stmt.span,
+                        "non-binding let on a result of a `#[must_use]` function",
+                        "consider explicitly using function result"
+                    )
                 }
             }
         }
index ff8acb321a4609e3daf1f6f8e3406beacd301fd4..0af7f946fa9d5238ec4c4a91d3fb25f5973ff548 100644 (file)
@@ -58,7 +58,7 @@
 pub const MEM_UNINITIALIZED: [&str; 3] = ["core", "mem", "uninitialized"];
 pub const MEM_ZEROED: [&str; 3] = ["core", "mem", "zeroed"];
 pub const MUTEX: [&str; 4] = ["std", "sync", "mutex", "Mutex"];
-pub const MUTEX_LOCK: [&str; 5] = ["std", "sync", "mutex", "Mutex", "lock"];
+pub const MUTEX_GUARD: [&str; 4] = ["std", "sync", "mutex", "MutexGuard"];
 pub const OPEN_OPTIONS: [&str; 3] = ["std", "fs", "OpenOptions"];
 pub const OPS_MODULE: [&str; 2] = ["core", "ops"];
 pub const OPTION: [&str; 3] = ["core", "option", "Option"];
 pub const RESULT: [&str; 3] = ["core", "result", "Result"];
 pub const RESULT_ERR: [&str; 4] = ["core", "result", "Result", "Err"];
 pub const RESULT_OK: [&str; 4] = ["core", "result", "Result", "Ok"];
-pub const RWLOCK_READ: [&str; 5] = ["std", "sync", "rwlock", "RwLock", "read"];
-pub const RWLOCK_WRITE: [&str; 5] = ["std", "sync", "rwlock", "RwLock", "write"];
+pub const RWLOCK_READ_GUARD: [&str; 4] = ["std", "sync", "rwlock", "RwLockReadGuard"];
+pub const RWLOCK_WRITE_GUARD: [&str; 4] = ["std", "sync", "rwlock", "RwLockWriteGuard"];
 pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"];
 pub const SLICE_INTO_VEC: [&str; 4] = ["alloc", "slice", "<impl [T]>", "into_vec"];
 pub const SLICE_ITER: [&str; 3] = ["core", "slice", "Iter"];
index f4a33c3606605826eab2e59ca55b86fcc096de47..88fb216a74329597eb2c0d2fe29f696b952ca30f 100644 (file)
@@ -7,4 +7,7 @@ fn main() {
     let _ = m.lock();
     let _ = rw.read();
     let _ = rw.write();
+    let _ = m.try_lock();
+    let _ = rw.try_read();
+    let _ = rw.try_write();
 }
index bd297f6020cc5beeda4fb1141559ffd2db377c02..5d5f6059ef13e93b51387b3a2adca96142526727 100644 (file)
@@ -5,7 +5,7 @@ LL |     let _ = m.lock();
    |     ^^^^^^^^^^^^^^^^^
    |
    = note: `-D clippy::let-underscore-lock` implied by `-D warnings`
-   = help: consider using an underscore-prefixed named binding
+   = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
 
 error: non-binding let on a synchronization lock
   --> $DIR/let_underscore_lock.rs:8:5
@@ -13,7 +13,7 @@ error: non-binding let on a synchronization lock
 LL |     let _ = rw.read();
    |     ^^^^^^^^^^^^^^^^^^
    |
-   = help: consider using an underscore-prefixed named binding
+   = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
 
 error: non-binding let on a synchronization lock
   --> $DIR/let_underscore_lock.rs:9:5
@@ -21,7 +21,31 @@ error: non-binding let on a synchronization lock
 LL |     let _ = rw.write();
    |     ^^^^^^^^^^^^^^^^^^^
    |
-   = help: consider using an underscore-prefixed named binding
+   = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
 
-error: aborting due to 3 previous errors
+error: non-binding let on a synchronization lock
+  --> $DIR/let_underscore_lock.rs:10:5
+   |
+LL |     let _ = m.try_lock();
+   |     ^^^^^^^^^^^^^^^^^^^^^
+   |
+   = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
+
+error: non-binding let on a synchronization lock
+  --> $DIR/let_underscore_lock.rs:11:5
+   |
+LL |     let _ = rw.try_read();
+   |     ^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
+
+error: non-binding let on a synchronization lock
+  --> $DIR/let_underscore_lock.rs:12:5
+   |
+LL |     let _ = rw.try_write();
+   |     ^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
+
+error: aborting due to 6 previous errors