X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=compiler%2Frustc_lint%2Fsrc%2Flet_underscore.rs;h=b83a9665fc0c60ef48e3fd1d0639bad6b9f41eaa;hb=00efb0cb960e4b89a80cad7d44fa0eefd223f513;hp=04d844d21dc38d9dc959218978a4ba1a13696211;hpb=bd20fc1fd657b32f7aa1d70d8723f04c87f21606;p=rust.git diff --git a/compiler/rustc_lint/src/let_underscore.rs b/compiler/rustc_lint/src/let_underscore.rs index 04d844d21dc..b83a9665fc0 100644 --- a/compiler/rustc_lint/src/let_underscore.rs +++ b/compiler/rustc_lint/src/let_underscore.rs @@ -1,5 +1,8 @@ -use crate::{LateContext, LateLintPass, LintContext}; -use rustc_errors::{Applicability, DiagnosticBuilder, 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; @@ -119,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( @@ -129,41 +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, - "non-binding let on a synchronization lock", - |lint| build_lint(lint, local, init.span), - ) + cx.emit_spanned_lint(LET_UNDERSCORE_LOCK, span, NonBindingLet::SyncLock { sub }); } else { - cx.struct_span_lint( + cx.emit_spanned_lint( LET_UNDERSCORE_DROP, local.span, - "non-binding let on a type that implements `Drop`", - |lint| build_lint(lint, local, init.span), - ) + NonBindingLet::DropType { sub }, + ); } } } } - -fn build_lint<'a, 'b>( - lint: &'a mut DiagnosticBuilder<'b, ()>, - local: &hir::Local<'_>, - init_span: rustc_span::Span, -) -> &'a mut DiagnosticBuilder<'b, ()> { - lint.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, - ) -}