Skip to content

Commit e610047

Browse files
committed
migrate: let_underscore.rs
fix: NonBindingLetSub
1 parent 80df25e commit e610047

File tree

3 files changed

+74
-33
lines changed

3 files changed

+74
-33
lines changed

compiler/rustc_error_messages/locales/en-US/lint.ftl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ lint_enum_intrinsics_mem_variant =
1616
lint_expectation = this lint expectation is unfulfilled
1717
.note = the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message
1818
19+
lint_non_binding_let_on_sync_lock =
20+
non-binding let on a synchronization lock
21+
22+
lint_non_binding_let_on_drop_type =
23+
non-binding let on a type that implements `Drop`
24+
25+
lint_non_binding_let_suggestion =
26+
consider binding to an unused variable to avoid immediately dropping the value
27+
28+
lint_non_binding_let_multi_suggestion =
29+
consider immediately dropping the value
30+
1931
lint_deprecated_lint_name =
2032
lint name `{$name}` is deprecated and may not have an effect in the future.
2133
.suggestion = change it to

compiler/rustc_lint/src/let_underscore.rs

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
use crate::{LateContext, LateLintPass, LintContext};
2-
use rustc_errors::{Applicability, DiagnosticBuilder, MultiSpan};
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
3+
use crate::{
4+
lints::{NonBindingLet, NonBindingLetSub},
5+
LateContext, LateLintPass, LintContext,
6+
};
7+
use rustc_errors::MultiSpan;
38
use rustc_hir as hir;
49
use rustc_middle::ty;
510
use rustc_span::Symbol;
@@ -119,6 +124,11 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
119124
_ => false,
120125
};
121126

127+
let sub = NonBindingLetSub {
128+
suggestion: local.pat.span,
129+
multi_suggestion_start: local.span.until(init.span),
130+
multi_suggestion_end: init.span.shrink_to_hi(),
131+
};
122132
if is_sync_lock {
123133
let mut span = MultiSpan::from_spans(vec![local.pat.span, init.span]);
124134
span.push_span_label(
@@ -129,41 +139,14 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
129139
init.span,
130140
"this binding will immediately drop the value assigned to it".to_string(),
131141
);
132-
cx.struct_span_lint(
133-
LET_UNDERSCORE_LOCK,
134-
span,
135-
"non-binding let on a synchronization lock",
136-
|lint| build_lint(lint, local, init.span),
137-
)
142+
cx.emit_spanned_lint(LET_UNDERSCORE_LOCK, span, NonBindingLet::SyncLock { sub });
138143
} else {
139-
cx.struct_span_lint(
144+
cx.emit_spanned_lint(
140145
LET_UNDERSCORE_DROP,
141146
local.span,
142-
"non-binding let on a type that implements `Drop`",
143-
|lint| build_lint(lint, local, init.span),
144-
)
147+
NonBindingLet::DropType { sub },
148+
);
145149
}
146150
}
147151
}
148152
}
149-
150-
fn build_lint<'a, 'b>(
151-
lint: &'a mut DiagnosticBuilder<'b, ()>,
152-
local: &hir::Local<'_>,
153-
init_span: rustc_span::Span,
154-
) -> &'a mut DiagnosticBuilder<'b, ()> {
155-
lint.span_suggestion_verbose(
156-
local.pat.span,
157-
"consider binding to an unused variable to avoid immediately dropping the value",
158-
"_unused",
159-
Applicability::MachineApplicable,
160-
)
161-
.multipart_suggestion(
162-
"consider immediately dropping the value",
163-
vec![
164-
(local.span.until(init_span), "drop(".to_string()),
165-
(init_span.shrink_to_hi(), ")".to_string()),
166-
],
167-
Applicability::MachineApplicable,
168-
)
169-
}

compiler/rustc_lint/src/lints.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,52 @@ pub struct EnumIntrinsicsMemVariant<'a> {
4949
pub ty_param: Ty<'a>,
5050
}
5151

52+
// let_underscore.rs
53+
#[derive(LintDiagnostic)]
54+
pub enum NonBindingLet {
55+
#[diag(lint::non_binding_let_on_sync_lock)]
56+
SyncLock {
57+
#[subdiagnostic]
58+
sub: NonBindingLetSub,
59+
},
60+
#[diag(lint::non_binding_let_on_drop_type)]
61+
DropType {
62+
#[subdiagnostic]
63+
sub: NonBindingLetSub,
64+
},
65+
}
66+
67+
pub struct NonBindingLetSub {
68+
pub suggestion: Span,
69+
pub multi_suggestion_start: Span,
70+
pub multi_suggestion_end: Span,
71+
}
72+
73+
impl AddToDiagnostic for NonBindingLetSub {
74+
fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, _: F)
75+
where
76+
F: Fn(
77+
&mut rustc_errors::Diagnostic,
78+
rustc_errors::SubdiagnosticMessage,
79+
) -> rustc_errors::SubdiagnosticMessage,
80+
{
81+
diag.span_suggestion_verbose(
82+
self.suggestion,
83+
fluent::lint::non_binding_let_suggestion,
84+
"_unused",
85+
Applicability::MachineApplicable,
86+
);
87+
diag.multipart_suggestion(
88+
fluent::lint::non_binding_let_multi_suggestion,
89+
vec![
90+
(self.multi_suggestion_start, "drop(".to_string()),
91+
(self.multi_suggestion_end, ")".to_string()),
92+
],
93+
Applicability::MachineApplicable,
94+
);
95+
}
96+
}
97+
5298
// levels.rs
5399
#[derive(LintDiagnostic)]
54100
#[diag(lint_overruled_attribute)]

0 commit comments

Comments
 (0)