Skip to content

Commit 4261e0b

Browse files
committed
Auto merge of rust-lang#12734 - y21:issue12733, r=Manishearth
suppress `readonly_write_lock` for underscore-prefixed bindings Fixes rust-lang#12733 Unsure if there's a better way to prevent this kind of false positive but this is the one that made most sense to me. In my experience, prefixing bindings with an underscore is the usual way to name variables that aren't used and that exist purely for executing drop code at the end of the scope. ------- changelog: suppress [`readonly_write_lock`] for underscore-prefixed bindings
2 parents 70e74b1 + f0beaed commit 4261e0b

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

clippy_lints/src/methods/readonly_write_lock.rs

+6-1
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

+4
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

+4
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)