Skip to content

Commit f0beaed

Browse files
committed
suppress readonly_write_lock for underscore-prefixed bindings
1 parent c6bf954 commit f0beaed

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

clippy_lints/src/methods/readonly_write_lock.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::mir::{enclosing_mir, visit_local_usage};
44
use clippy_utils::source::snippet;
55
use clippy_utils::ty::is_type_diagnostic_item;
66
use rustc_errors::Applicability;
7-
use rustc_hir::{Expr, ExprKind, Node};
7+
use rustc_hir::{Expr, ExprKind, Node, PatKind};
88
use rustc_lint::LateContext;
99
use rustc_middle::mir::{Location, START_BLOCK};
1010
use rustc_span::sym;
@@ -25,6 +25,11 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, receiver
2525
&& is_unwrap_call(cx, unwrap_call_expr)
2626
&& let parent = cx.tcx.parent_hir_node(unwrap_call_expr.hir_id)
2727
&& let Node::LetStmt(local) = parent
28+
&& let PatKind::Binding(.., ident, _) = local.pat.kind
29+
// if the binding is prefixed with `_`, it typically means
30+
// that this guard only exists to protect a section of code
31+
// rather than the contained data
32+
&& !ident.as_str().starts_with('_')
2833
&& let Some(mir) = enclosing_mir(cx.tcx, expr.hir_id)
2934
&& let Some((local, _)) = mir
3035
.local_decls

tests/ui/readonly_write_lock.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ fn main() {
4343
*writer1 = *writer2;
4444
}
4545
}
46+
47+
fn issue12733(rw: &RwLock<()>) {
48+
let _write_guard = rw.write().unwrap();
49+
}

tests/ui/readonly_write_lock.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ fn main() {
4343
*writer1 = *writer2;
4444
}
4545
}
46+
47+
fn issue12733(rw: &RwLock<()>) {
48+
let _write_guard = rw.write().unwrap();
49+
}

0 commit comments

Comments
 (0)