Skip to content

Commit a9f1b7b

Browse files
committed
Explain why let-underscoring a lock guard is incorrect.
Currently, the let_underscore_lock lint simply tells what is wrong, but not why it is wrong. We fix this by using a `MultiSpan` to explain specifically that doing `let _ = ` immediately drops the lock guard because it does not assign the lock guard to a binding.
1 parent a9095ff commit a9f1b7b

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

compiler/rustc_lint/src/let_underscore.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{LateContext, LateLintPass, LintContext};
2-
use rustc_errors::Applicability;
2+
use rustc_errors::{Applicability, MultiSpan};
33
use rustc_hir as hir;
44
use rustc_middle::{lint::LintDiagnosticBuilder, ty};
55
use rustc_span::Symbol;
@@ -119,7 +119,16 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
119119
};
120120

121121
if is_sync_lock {
122-
cx.struct_span_lint(LET_UNDERSCORE_LOCK, local.span, |lint| {
122+
let mut span = MultiSpan::from_spans(vec![local.pat.span, init.span]);
123+
span.push_span_label(
124+
local.pat.span,
125+
"this lock is not assigned to a binding and is immediately dropped".to_string(),
126+
);
127+
span.push_span_label(
128+
init.span,
129+
"this binding will immediately drop the value assigned to it".to_string(),
130+
);
131+
cx.struct_span_lint(LET_UNDERSCORE_LOCK, span, |lint| {
123132
build_and_emit_lint(
124133
lint,
125134
local,

src/test/ui/lint/let_underscore/let_underscore_lock.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error: non-binding let on a synchronization lock
2-
--> $DIR/let_underscore_lock.rs:6:5
2+
--> $DIR/let_underscore_lock.rs:6:9
33
|
44
LL | let _ = data.lock().unwrap();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^ ^^^^^^^^^^^^^^^^^^^^ this binding will immediately drop the value assigned to it
6+
| |
7+
| this lock is not assigned to a binding and is immediately dropped
68
|
79
= note: `#[deny(let_underscore_lock)]` on by default
810
help: consider binding to an unused variable to avoid immediately dropping the value

0 commit comments

Comments
 (0)