about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAaron Kofsky <aaronko@umich.edu>2022-08-04 17:00:48 -0400
committerAaron Kofsky <aaronko@umich.edu>2022-08-04 17:00:48 -0400
commita9f1b7bd2a25e34de29eb88f81550690f4fec5dc (patch)
tree3a5ac965a3c4c7e45af33eb97b258870b1242b0a
parenta9095ff2139fb305b15006d1f2a1ff16da0b6c29 (diff)
downloadrust-a9f1b7bd2a25e34de29eb88f81550690f4fec5dc.tar.gz
rust-a9f1b7bd2a25e34de29eb88f81550690f4fec5dc.zip
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.
-rw-r--r--compiler/rustc_lint/src/let_underscore.rs13
-rw-r--r--src/test/ui/lint/let_underscore/let_underscore_lock.stderr6
2 files changed, 15 insertions, 4 deletions
diff --git a/compiler/rustc_lint/src/let_underscore.rs b/compiler/rustc_lint/src/let_underscore.rs
index 2ba79aacace..79d1443dc35 100644
--- a/compiler/rustc_lint/src/let_underscore.rs
+++ b/compiler/rustc_lint/src/let_underscore.rs
@@ -1,5 +1,5 @@
 use crate::{LateContext, LateLintPass, LintContext};
-use rustc_errors::Applicability;
+use rustc_errors::{Applicability, MultiSpan};
 use rustc_hir as hir;
 use rustc_middle::{lint::LintDiagnosticBuilder, ty};
 use rustc_span::Symbol;
@@ -119,7 +119,16 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
             };
 
             if is_sync_lock {
-                cx.struct_span_lint(LET_UNDERSCORE_LOCK, local.span, |lint| {
+                let mut span = MultiSpan::from_spans(vec![local.pat.span, init.span]);
+                span.push_span_label(
+                    local.pat.span,
+                    "this lock is not assigned to a binding and is immediately dropped".to_string(),
+                );
+                span.push_span_label(
+                    init.span,
+                    "this binding will immediately drop the value assigned to it".to_string(),
+                );
+                cx.struct_span_lint(LET_UNDERSCORE_LOCK, span, |lint| {
                     build_and_emit_lint(
                         lint,
                         local,
diff --git a/src/test/ui/lint/let_underscore/let_underscore_lock.stderr b/src/test/ui/lint/let_underscore/let_underscore_lock.stderr
index 7aa119003b4..fb58af0a42f 100644
--- a/src/test/ui/lint/let_underscore/let_underscore_lock.stderr
+++ b/src/test/ui/lint/let_underscore/let_underscore_lock.stderr
@@ -1,8 +1,10 @@
 error: non-binding let on a synchronization lock
-  --> $DIR/let_underscore_lock.rs:6:5
+  --> $DIR/let_underscore_lock.rs:6:9
    |
 LL |     let _ = data.lock().unwrap();
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |         ^   ^^^^^^^^^^^^^^^^^^^^ this binding will immediately drop the value assigned to it
+   |         |
+   |         this lock is not assigned to a binding and is immediately dropped
    |
    = note: `#[deny(let_underscore_lock)]` on by default
 help: consider binding to an unused variable to avoid immediately dropping the value