]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_lint/src/let_underscore.rs
Rollup merge of #107656 - jonhoo:bump-rust-installer, r=Mark-Simulacrum
[rust.git] / compiler / rustc_lint / src / let_underscore.rs
index 7e885e6c51aad42261b5da1eeb11ee618712875c..b83a9665fc0c60ef48e3fd1d0639bad6b9f41eaa 100644 (file)
@@ -1,5 +1,8 @@
-use crate::{LateContext, LateLintPass, LintContext};
-use rustc_errors::{Applicability, LintDiagnosticBuilder, MultiSpan};
+use crate::{
+    lints::{NonBindingLet, NonBindingLetSub},
+    LateContext, LateLintPass, LintContext,
+};
+use rustc_errors::MultiSpan;
 use rustc_hir as hir;
 use rustc_middle::ty;
 use rustc_span::Symbol;
@@ -11,7 +14,8 @@
     /// scope.
     ///
     /// ### Example
-    /// ```
+    ///
+    /// ```rust
     /// struct SomeStruct;
     /// impl Drop for SomeStruct {
     ///     fn drop(&mut self) {
@@ -56,7 +60,7 @@
     /// of at end of scope, which is typically incorrect.
     ///
     /// ### Example
-    /// ```compile_fail
+    /// ```rust,compile_fail
     /// use std::sync::{Arc, Mutex};
     /// use std::thread;
     /// let data = Arc::new(Mutex::new(0));
@@ -118,6 +122,11 @@ fn check_local(&mut self, cx: &LateContext<'_>, local: &hir::Local<'_>) {
                 _ => false,
             };
 
+            let sub = NonBindingLetSub {
+                suggestion: local.pat.span,
+                multi_suggestion_start: local.span.until(init.span),
+                multi_suggestion_end: init.span.shrink_to_hi(),
+            };
             if is_sync_lock {
                 let mut span = MultiSpan::from_spans(vec![local.pat.span, init.span]);
                 span.push_span_label(
@@ -128,48 +137,14 @@ fn check_local(&mut self, cx: &LateContext<'_>, local: &hir::Local<'_>) {
                     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,
-                        init.span,
-                        "non-binding let on a synchronization lock",
-                    )
-                })
+                cx.emit_spanned_lint(LET_UNDERSCORE_LOCK, span, NonBindingLet::SyncLock { sub });
             } else {
-                cx.struct_span_lint(LET_UNDERSCORE_DROP, local.span, |lint| {
-                    build_and_emit_lint(
-                        lint,
-                        local,
-                        init.span,
-                        "non-binding let on a type that implements `Drop`",
-                    );
-                })
+                cx.emit_spanned_lint(
+                    LET_UNDERSCORE_DROP,
+                    local.span,
+                    NonBindingLet::DropType { sub },
+                );
             }
         }
     }
 }
-
-fn build_and_emit_lint(
-    lint: LintDiagnosticBuilder<'_, ()>,
-    local: &hir::Local<'_>,
-    init_span: rustc_span::Span,
-    msg: &str,
-) {
-    lint.build(msg)
-        .span_suggestion_verbose(
-            local.pat.span,
-            "consider binding to an unused variable to avoid immediately dropping the value",
-            "_unused",
-            Applicability::MachineApplicable,
-        )
-        .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();
-}