about summary refs log tree commit diff
diff options
context:
space:
mode:
authory21 <30553356+y21@users.noreply.github.com>2024-04-29 17:27:40 +0200
committery21 <30553356+y21@users.noreply.github.com>2024-04-29 17:30:01 +0200
commitf0beaedf832c574042bb8ec1957e462b97373758 (patch)
tree2fc518682a47417ceca3828204a0f529056bdd8e
parentc6bf9548d53233f8a08928955a996f4bf30ea1bd (diff)
downloadrust-f0beaedf832c574042bb8ec1957e462b97373758.tar.gz
rust-f0beaedf832c574042bb8ec1957e462b97373758.zip
suppress `readonly_write_lock` for underscore-prefixed bindings
-rw-r--r--clippy_lints/src/methods/readonly_write_lock.rs7
-rw-r--r--tests/ui/readonly_write_lock.fixed4
-rw-r--r--tests/ui/readonly_write_lock.rs4
3 files changed, 14 insertions, 1 deletions
diff --git a/clippy_lints/src/methods/readonly_write_lock.rs b/clippy_lints/src/methods/readonly_write_lock.rs
index 9b0180d9369..774aaec1afd 100644
--- a/clippy_lints/src/methods/readonly_write_lock.rs
+++ b/clippy_lints/src/methods/readonly_write_lock.rs
@@ -4,7 +4,7 @@ use clippy_utils::mir::{enclosing_mir, visit_local_usage};
 use clippy_utils::source::snippet;
 use clippy_utils::ty::is_type_diagnostic_item;
 use rustc_errors::Applicability;
-use rustc_hir::{Expr, ExprKind, Node};
+use rustc_hir::{Expr, ExprKind, Node, PatKind};
 use rustc_lint::LateContext;
 use rustc_middle::mir::{Location, START_BLOCK};
 use rustc_span::sym;
@@ -25,6 +25,11 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, receiver
         && is_unwrap_call(cx, unwrap_call_expr)
         && let parent = cx.tcx.parent_hir_node(unwrap_call_expr.hir_id)
         && let Node::LetStmt(local) = parent
+        && let PatKind::Binding(.., ident, _) = local.pat.kind
+        // if the binding is prefixed with `_`, it typically means
+        // that this guard only exists to protect a section of code
+        // rather than the contained data
+        && !ident.as_str().starts_with('_')
         && let Some(mir) = enclosing_mir(cx.tcx, expr.hir_id)
         && let Some((local, _)) = mir
             .local_decls
diff --git a/tests/ui/readonly_write_lock.fixed b/tests/ui/readonly_write_lock.fixed
index 76f4a43ae53..4db13482ac7 100644
--- a/tests/ui/readonly_write_lock.fixed
+++ b/tests/ui/readonly_write_lock.fixed
@@ -43,3 +43,7 @@ fn main() {
         *writer1 = *writer2;
     }
 }
+
+fn issue12733(rw: &RwLock<()>) {
+    let _write_guard = rw.write().unwrap();
+}
diff --git a/tests/ui/readonly_write_lock.rs b/tests/ui/readonly_write_lock.rs
index 3d1d3855fe1..66ba1b2d696 100644
--- a/tests/ui/readonly_write_lock.rs
+++ b/tests/ui/readonly_write_lock.rs
@@ -43,3 +43,7 @@ fn main() {
         *writer1 = *writer2;
     }
 }
+
+fn issue12733(rw: &RwLock<()>) {
+    let _write_guard = rw.write().unwrap();
+}