]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_lint/src/let_underscore.rs
Rollup merge of #101423 - mkroening:hermit-warnings, r=sanxiyn
[rust.git] / compiler / rustc_lint / src / let_underscore.rs
index 520c865cc1922d690c6860de17d953a0afa14d15..7e885e6c51aad42261b5da1eeb11ee618712875c 100644 (file)
@@ -1,7 +1,7 @@
 use crate::{LateContext, LateLintPass, LintContext};
-use rustc_errors::Applicability;
+use rustc_errors::{Applicability, LintDiagnosticBuilder, MultiSpan};
 use rustc_hir as hir;
-use rustc_middle::{lint::LintDiagnosticBuilder, ty};
+use rustc_middle::ty;
 use rustc_span::Symbol;
 
 declare_lint! {
@@ -119,7 +119,16 @@ fn check_local(&mut self, cx: &LateContext<'_>, local: &hir::Local<'_>) {
             };
 
             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,
@@ -150,15 +159,17 @@ fn build_and_emit_lint(
     lint.build(msg)
         .span_suggestion_verbose(
             local.pat.span,
-            "consider binding to an unused variable",
+            "consider binding to an unused variable to avoid immediately dropping the value",
             "_unused",
             Applicability::MachineApplicable,
         )
-        .span_suggestion_verbose(
-            init_span,
-            "consider explicitly droping with `std::mem::drop`",
-            "drop(...)",
-            Applicability::HasPlaceholders,
+        .multipart_suggestion(
+            "consider immediately dropping the value",
+            vec![
+                (local.span.until(init_span), "drop(".to_string()),
+                (init_span.shrink_to_hi(), ")".to_string()),
+            ],
+            Applicability::MachineApplicable,
         )
         .emit();
 }