Skip to content

Commit 1999a4c

Browse files
committed
lint: port unused diagnostics
Signed-off-by: David Wood <david.wood@huawei.com>
1 parent 0602729 commit 1999a4c

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,25 @@ lint-atomic-ordering-invalid-fail-success = `{$method}`'s success ordering must
251251
.fail-label = `{$fail_ordering}` failure ordering
252252
.success-label = `{$success_ordering}` success ordering
253253
.suggestion = consider using `{$success_suggestion}` success ordering instead
254+
255+
lint-unused-op = unused {$op} that must be used
256+
.label = the {$op} produces a value
257+
.suggestion = use `let _ = ...` to ignore the resulting value
258+
259+
lint-unused-result = unused result of type `{$ty}`
260+
261+
lint-unused-closure =
262+
unused {$pre}{$count ->
263+
[one] closure
264+
*[other] closures
265+
}{$post} that must be used
266+
.note = closures are lazy and do nothing unless called
267+
268+
lint-unused-generator =
269+
unused {$pre}{$count ->
270+
[one] generator
271+
*[other] generator
272+
}{$post} that must be used
273+
.note = generators are lazy and do nothing unless resumed
274+
275+
lint-unused-def = unused {$pre}`{$def}`{$post} that must be used

compiler/rustc_lint/src/unused.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}
33
use rustc_ast as ast;
44
use rustc_ast::util::{classify, parser};
55
use rustc_ast::{ExprKind, StmtKind};
6-
use rustc_errors::{pluralize, Applicability, MultiSpan};
6+
use rustc_errors::{fluent, pluralize, Applicability, MultiSpan};
77
use rustc_hir as hir;
88
use rustc_hir::def::{DefKind, Res};
99
use rustc_hir::def_id::DefId;
@@ -155,22 +155,23 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
155155

156156
if let Some(must_use_op) = must_use_op {
157157
cx.struct_span_lint(UNUSED_MUST_USE, expr.span, |lint| {
158-
let mut lint = lint.build(&format!("unused {} that must be used", must_use_op));
159-
lint.span_label(expr.span, &format!("the {} produces a value", must_use_op));
160-
lint.span_suggestion_verbose(
161-
expr.span.shrink_to_lo(),
162-
"use `let _ = ...` to ignore the resulting value",
163-
"let _ = ",
164-
Applicability::MachineApplicable,
165-
);
166-
lint.emit();
158+
lint.build(fluent::lint::unused_op)
159+
.set_arg("op", must_use_op)
160+
.span_label(expr.span, fluent::lint::label)
161+
.span_suggestion_verbose(
162+
expr.span.shrink_to_lo(),
163+
fluent::lint::suggestion,
164+
"let _ = ",
165+
Applicability::MachineApplicable,
166+
)
167+
.emit();
167168
});
168169
op_warned = true;
169170
}
170171

171172
if !(type_permits_lack_of_use || fn_warned || op_warned) {
172173
cx.struct_span_lint(UNUSED_RESULTS, s.span, |lint| {
173-
lint.build(&format!("unused result of type `{}`", ty)).emit();
174+
lint.build(fluent::lint::unused_result).set_arg("ty", ty).emit();
174175
});
175176
}
176177

@@ -267,23 +268,27 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
267268
},
268269
ty::Closure(..) => {
269270
cx.struct_span_lint(UNUSED_MUST_USE, span, |lint| {
270-
let mut err = lint.build(&format!(
271-
"unused {}closure{}{} that must be used",
272-
descr_pre, plural_suffix, descr_post,
273-
));
274-
err.note("closures are lazy and do nothing unless called");
275-
err.emit();
271+
// FIXME(davidtwco): this isn't properly translatable becauses of the
272+
// pre/post strings
273+
lint.build(fluent::lint::unused_closure)
274+
.set_arg("count", plural_len)
275+
.set_arg("pre", descr_pre)
276+
.set_arg("post", descr_post)
277+
.note(fluent::lint::note)
278+
.emit();
276279
});
277280
true
278281
}
279282
ty::Generator(..) => {
280283
cx.struct_span_lint(UNUSED_MUST_USE, span, |lint| {
281-
let mut err = lint.build(&format!(
282-
"unused {}generator{}{} that must be used",
283-
descr_pre, plural_suffix, descr_post,
284-
));
285-
err.note("generators are lazy and do nothing unless resumed");
286-
err.emit();
284+
// FIXME(davidtwco): this isn't properly translatable becauses of the
285+
// pre/post strings
286+
lint.build(fluent::lint::unused_generator)
287+
.set_arg("count", plural_len)
288+
.set_arg("pre", descr_pre)
289+
.set_arg("post", descr_post)
290+
.note(fluent::lint::note)
291+
.emit();
287292
});
288293
true
289294
}
@@ -305,13 +310,12 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
305310
) -> bool {
306311
if let Some(attr) = cx.tcx.get_attr(def_id, sym::must_use) {
307312
cx.struct_span_lint(UNUSED_MUST_USE, span, |lint| {
308-
let msg = format!(
309-
"unused {}`{}`{} that must be used",
310-
descr_pre_path,
311-
cx.tcx.def_path_str(def_id),
312-
descr_post_path
313-
);
314-
let mut err = lint.build(&msg);
313+
// FIXME(davidtwco): this isn't properly translatable becauses of the pre/post
314+
// strings
315+
let mut err = lint.build(fluent::lint::unused_def);
316+
err.set_arg("pre", descr_pre_path);
317+
err.set_arg("post", descr_post_path);
318+
err.set_arg("def", cx.tcx.def_path_str(def_id));
315319
// check for #[must_use = "..."]
316320
if let Some(note) = attr.value_str() {
317321
err.note(note.as_str());

0 commit comments

Comments
 (0)