Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e5ae9d0

Browse files
committed
migrate: unused.rs
1 parent d44ccaa commit e5ae9d0

File tree

2 files changed

+201
-103
lines changed

2 files changed

+201
-103
lines changed

compiler/rustc_lint/src/lints.rs

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
use rustc_errors::{fluent, AddSubdiagnostic, DecorateLint, EmissionGuarantee};
1+
use rustc_errors::{fluent, AddSubdiagnostic, Applicability, DecorateLint, EmissionGuarantee};
2+
use rustc_hir::def_id::DefId;
23
use rustc_macros::{LintDiagnostic, SessionSubdiagnostic};
4+
use rustc_middle::ty::Ty;
35
use rustc_span::{Span, Symbol};
46

7+
use crate::LateContext;
8+
59
#[derive(LintDiagnostic)]
610
#[diag(lint_range_endpoint_out_of_range)]
711
pub struct RangeEndpointOutOfRange<'a> {
@@ -146,3 +150,130 @@ pub struct InvalidAtomicOrderingDiag {
146150
#[label]
147151
pub fail_order_arg_span: Span,
148152
}
153+
154+
#[derive(LintDiagnostic)]
155+
#[diag(lint_unused_op)]
156+
pub struct UnusedOp<'a> {
157+
pub op: &'a str,
158+
#[label]
159+
pub label: Span,
160+
#[suggestion(style = "verbose", code = "let _ = ", applicability = "machine-applicable")]
161+
pub suggestion: Span,
162+
}
163+
164+
#[derive(LintDiagnostic)]
165+
#[diag(lint_unused_result)]
166+
pub struct UnusedResult<'a> {
167+
pub ty: Ty<'a>,
168+
}
169+
170+
// FIXME(davidtwco): this isn't properly translatable becauses of the
171+
// pre/post strings
172+
#[derive(LintDiagnostic)]
173+
#[diag(lint_unused_closure)]
174+
#[note]
175+
pub struct UnusedClosure<'a> {
176+
pub count: usize,
177+
pub pre: &'a str,
178+
pub post: &'a str,
179+
}
180+
181+
// FIXME(davidtwco): this isn't properly translatable becauses of the
182+
// pre/post strings
183+
#[derive(LintDiagnostic)]
184+
#[diag(lint_unused_generator)]
185+
#[note]
186+
pub struct UnusedGenerator<'a> {
187+
pub count: usize,
188+
pub pre: &'a str,
189+
pub post: &'a str,
190+
}
191+
192+
// FIXME(davidtwco): this isn't properly translatable becauses of the pre/post
193+
// strings
194+
pub struct UnusedDef<'a, 'b> {
195+
pub pre: &'a str,
196+
pub post: &'a str,
197+
pub cx: &'a LateContext<'b>,
198+
pub def_id: DefId,
199+
pub note: Option<Symbol>,
200+
}
201+
202+
// FIXME: refactor with `Option<String>` in macro
203+
impl<'a, 'b, G: EmissionGuarantee> DecorateLint<'_, G> for UnusedDef<'a, 'b> {
204+
fn decorate_lint(self, diag: rustc_errors::LintDiagnosticBuilder<'_, G>) {
205+
let mut diag = diag.build(fluent::lint_unused_def);
206+
diag.set_arg("pre", self.pre);
207+
diag.set_arg("post", self.post);
208+
diag.set_arg("def", self.cx.tcx.def_path_str(self.def_id));
209+
// check for #[must_use = "..."]
210+
if let Some(note) = self.note {
211+
diag.note(note.as_str());
212+
}
213+
diag.emit();
214+
}
215+
}
216+
217+
#[derive(LintDiagnostic)]
218+
#[diag(lint_path_statement_drop)]
219+
pub struct PathStatementDrop {
220+
#[subdiagnostic]
221+
pub sub: PathStatementDropSub,
222+
}
223+
224+
#[derive(SessionSubdiagnostic)]
225+
pub enum PathStatementDropSub {
226+
#[suggestion(
227+
suggestion,
228+
code = "drop({snippet});",
229+
applicability = "machine-applicable"
230+
)]
231+
Suggestion {
232+
#[primary_span]
233+
span: Span,
234+
snippet: String,
235+
},
236+
#[help(help)]
237+
Help {
238+
#[primary_span]
239+
span: Span,
240+
},
241+
}
242+
243+
#[derive(LintDiagnostic)]
244+
#[diag(lint_path_statement_no_effect)]
245+
pub struct PathStatementNoEffect;
246+
247+
#[derive(LintDiagnostic)]
248+
#[diag(lint_unused_delim)]
249+
pub struct UnusedDelim<'a> {
250+
pub delim: &'static str,
251+
pub item: &'a str,
252+
#[subdiagnostic]
253+
pub suggestion: Option<UnusedDelimSuggestion>,
254+
}
255+
256+
#[derive(Subdiagnostic)]
257+
#[multipart_suggestion(suggestion, applicability = "machine-applicable")]
258+
pub struct UnusedDelimSuggestion {
259+
#[suggestion_part(code = "{start_replace}")]
260+
pub start_span: Span,
261+
pub start_replace: &'static str,
262+
#[suggestion_part(code = "{end_replace}")]
263+
pub end_span: Span,
264+
pub end_replace: &'static str,
265+
}
266+
267+
#[derive(LintDiagnostic)]
268+
#[diag(lint_unused_import_braces)]
269+
pub struct UnusedImportBracesDiag {
270+
pub node: Symbol,
271+
}
272+
273+
#[derive(LintDiagnostic)]
274+
#[diag(lint_unused_allocation)]
275+
pub struct UnusedAllocationDiag;
276+
277+
#[derive(LintDiagnostic)]
278+
#[diag(lint_unused_allocation_mut)]
279+
pub struct UnusedAllocationMutDiag;

compiler/rustc_lint/src/unused.rs

Lines changed: 69 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
3+
use crate::lints::{
4+
PathStatementDrop, PathStatementDropSub, PathStatementNoEffect, UnusedAllocationDiag,
5+
UnusedAllocationMutDiag, UnusedClosure, UnusedDef, UnusedDelim, UnusedDelimSuggestion,
6+
UnusedGenerator, UnusedImportBracesDiag, UnusedOp, UnusedResult,
7+
};
18
use crate::Lint;
29
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
310
use rustc_ast as ast;
411
use rustc_ast::util::{classify, parser};
512
use rustc_ast::{ExprKind, StmtKind};
6-
use rustc_errors::{fluent, pluralize, Applicability, MultiSpan};
13+
use rustc_errors::{pluralize, MultiSpan};
714
use rustc_hir as hir;
815
use rustc_hir::def::{DefKind, Res};
916
use rustc_hir::def_id::DefId;
@@ -163,23 +170,20 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
163170
let mut op_warned = false;
164171

165172
if let Some(must_use_op) = must_use_op {
166-
cx.struct_span_lint(UNUSED_MUST_USE, expr.span, fluent::lint_unused_op, |lint| {
167-
lint.set_arg("op", must_use_op)
168-
.span_label(expr.span, fluent::label)
169-
.span_suggestion_verbose(
170-
expr.span.shrink_to_lo(),
171-
fluent::suggestion,
172-
"let _ = ",
173-
Applicability::MachineApplicable,
174-
)
175-
});
173+
cx.emit_spanned_lint(
174+
UNUSED_MUST_USE,
175+
expr.span,
176+
UnusedOp {
177+
op: must_use_op,
178+
label: expr.span,
179+
suggestion: expr.span.shrink_to_lo(),
180+
},
181+
);
176182
op_warned = true;
177183
}
178184

179185
if !(type_lint_emitted_or_suppressed || fn_warned || op_warned) {
180-
cx.struct_span_lint(UNUSED_RESULTS, s.span, fluent::lint_unused_result, |lint| {
181-
lint.set_arg("ty", ty)
182-
});
186+
cx.emit_spanned_lint(UNUSED_RESULTS, s.span, UnusedResult { ty });
183187
}
184188

185189
fn check_fn_must_use(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
@@ -402,47 +406,31 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
402406
);
403407
}
404408
MustUsePath::Closure(span) => {
405-
cx.struct_span_lint(
409+
cx.emit_spanned_lint(
406410
UNUSED_MUST_USE,
407411
*span,
408-
fluent::lint_unused_closure,
409-
|lint| {
410-
// FIXME(davidtwco): this isn't properly translatable because of the
411-
// pre/post strings
412-
lint.set_arg("count", plural_len)
413-
.set_arg("pre", descr_pre)
414-
.set_arg("post", descr_post)
415-
.note(fluent::note)
416-
},
412+
UnusedClosure { count: plural_len, pre: descr_pre, post: descr_post },
417413
);
418414
}
419415
MustUsePath::Generator(span) => {
420-
cx.struct_span_lint(
416+
cx.emit_spanned_lint(
421417
UNUSED_MUST_USE,
422418
*span,
423-
fluent::lint_unused_generator,
424-
|lint| {
425-
// FIXME(davidtwco): this isn't properly translatable because of the
426-
// pre/post strings
427-
lint.set_arg("count", plural_len)
428-
.set_arg("pre", descr_pre)
429-
.set_arg("post", descr_post)
430-
.note(fluent::note)
431-
},
419+
UnusedGenerator { count: plural_len, pre: descr_pre, post: descr_post },
432420
);
433421
}
434422
MustUsePath::Def(span, def_id, reason) => {
435-
cx.struct_span_lint(UNUSED_MUST_USE, *span, fluent::lint_unused_def, |lint| {
436-
// FIXME(davidtwco): this isn't properly translatable because of the pre/post
437-
// strings
438-
lint.set_arg("pre", descr_pre);
439-
lint.set_arg("post", descr_post);
440-
lint.set_arg("def", cx.tcx.def_path_str(*def_id));
441-
if let Some(note) = reason {
442-
lint.note(note.as_str());
443-
}
444-
lint
445-
});
423+
cx.emit_spanned_lint(
424+
UNUSED_MUST_USE,
425+
*span,
426+
UnusedDef {
427+
pre: descr_pre,
428+
post: descr_post,
429+
cx,
430+
def_id: *def_id,
431+
note: *reason,
432+
},
433+
);
446434
}
447435
}
448436
}
@@ -478,31 +466,15 @@ impl<'tcx> LateLintPass<'tcx> for PathStatements {
478466
if let hir::ExprKind::Path(_) = expr.kind {
479467
let ty = cx.typeck_results().expr_ty(expr);
480468
if ty.needs_drop(cx.tcx, cx.param_env) {
481-
cx.struct_span_lint(
482-
PATH_STATEMENTS,
483-
s.span,
484-
fluent::lint_path_statement_drop,
485-
|lint| {
486-
if let Ok(snippet) = cx.sess().source_map().span_to_snippet(expr.span) {
487-
lint.span_suggestion(
488-
s.span,
489-
fluent::suggestion,
490-
format!("drop({});", snippet),
491-
Applicability::MachineApplicable,
492-
);
493-
} else {
494-
lint.span_help(s.span, fluent::suggestion);
495-
}
496-
lint
497-
},
498-
);
469+
let sub = if let Ok(snippet) = cx.sess().source_map().span_to_snippet(expr.span)
470+
{
471+
PathStatementDropSub::Suggestion { span: s.span, snippet }
472+
} else {
473+
PathStatementDropSub::Help { span: s.span }
474+
};
475+
cx.emit_spanned_lint(PATH_STATEMENTS, s.span, PathStatementDrop { sub })
499476
} else {
500-
cx.struct_span_lint(
501-
PATH_STATEMENTS,
502-
s.span,
503-
fluent::lint_path_statement_no_effect,
504-
|lint| lint,
505-
);
477+
cx.emit_spanned_lint(PATH_STATEMENTS, s.span, PathStatementNoEffect);
506478
}
507479
}
508480
}
@@ -695,36 +667,35 @@ trait UnusedDelimLint {
695667
} else {
696668
MultiSpan::from(value_span)
697669
};
698-
cx.struct_span_lint(self.lint(), primary_span, fluent::lint_unused_delim, |lint| {
699-
lint.set_arg("delim", Self::DELIM_STR);
700-
lint.set_arg("item", msg);
701-
if let Some((lo, hi)) = spans {
702-
let sm = cx.sess().source_map();
703-
let lo_replace =
670+
let suggestion = spans.map(|(lo, hi)| {
671+
let sm = cx.sess().source_map();
672+
let lo_replace =
704673
if keep_space.0 &&
705674
let Ok(snip) = sm.span_to_prev_source(lo) && !snip.ends_with(' ') {
706-
" ".to_string()
675+
" "
707676
} else {
708-
"".to_string()
677+
""
709678
};
710679

711-
let hi_replace =
680+
let hi_replace =
712681
if keep_space.1 &&
713682
let Ok(snip) = sm.span_to_next_source(hi) && !snip.starts_with(' ') {
714-
" ".to_string()
683+
" "
715684
} else {
716-
"".to_string()
685+
""
717686
};
718-
719-
let replacement = vec![(lo, lo_replace), (hi, hi_replace)];
720-
lint.multipart_suggestion(
721-
fluent::suggestion,
722-
replacement,
723-
Applicability::MachineApplicable,
724-
);
687+
UnusedDelimSuggestion {
688+
start_span: lo,
689+
start_replace: lo_replace,
690+
end_span: hi,
691+
end_replace: hi_replace,
725692
}
726-
lint
727693
});
694+
cx.emit_spanned_lint(
695+
self.lint(),
696+
primary_span,
697+
UnusedDelim { delim: Self::DELIM_STR, item: msg, suggestion },
698+
);
728699
}
729700

730701
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
@@ -1297,11 +1268,10 @@ impl UnusedImportBraces {
12971268
ast::UseTreeKind::Nested(_) => return,
12981269
};
12991270

1300-
cx.struct_span_lint(
1271+
cx.emit_spanned_lint(
13011272
UNUSED_IMPORT_BRACES,
13021273
item.span,
1303-
fluent::lint_unused_import_braces,
1304-
|lint| lint.set_arg("node", node_name),
1274+
UnusedImportBracesDiag { node: node_name },
13051275
);
13061276
}
13071277
}
@@ -1351,17 +1321,14 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAllocation {
13511321

13521322
for adj in cx.typeck_results().expr_adjustments(e) {
13531323
if let adjustment::Adjust::Borrow(adjustment::AutoBorrow::Ref(_, m)) = adj.kind {
1354-
cx.struct_span_lint(
1355-
UNUSED_ALLOCATION,
1356-
e.span,
1357-
match m {
1358-
adjustment::AutoBorrowMutability::Not => fluent::lint_unused_allocation,
1359-
adjustment::AutoBorrowMutability::Mut { .. } => {
1360-
fluent::lint_unused_allocation_mut
1361-
}
1362-
},
1363-
|lint| lint,
1364-
);
1324+
match m {
1325+
adjustment::AutoBorrowMutability::Not => {
1326+
cx.emit_spanned_lint(UNUSED_ALLOCATION, e.span, UnusedAllocationDiag);
1327+
}
1328+
adjustment::AutoBorrowMutability::Mut { .. } => {
1329+
cx.emit_spanned_lint(UNUSED_ALLOCATION, e.span, UnusedAllocationMutDiag);
1330+
}
1331+
};
13651332
}
13661333
}
13671334
}

0 commit comments

Comments
 (0)