|
| 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 | +}; |
1 | 8 | use crate::Lint;
|
2 | 9 | use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
|
3 | 10 | use rustc_ast as ast;
|
4 | 11 | use rustc_ast::util::{classify, parser};
|
5 | 12 | use rustc_ast::{ExprKind, StmtKind};
|
6 |
| -use rustc_errors::{fluent, pluralize, Applicability, MultiSpan}; |
| 13 | +use rustc_errors::{pluralize, MultiSpan}; |
7 | 14 | use rustc_hir as hir;
|
8 | 15 | use rustc_hir::def::{DefKind, Res};
|
9 | 16 | use rustc_hir::def_id::DefId;
|
@@ -163,23 +170,20 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
|
163 | 170 | let mut op_warned = false;
|
164 | 171 |
|
165 | 172 | 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 | + ); |
176 | 182 | op_warned = true;
|
177 | 183 | }
|
178 | 184 |
|
179 | 185 | 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 }); |
183 | 187 | }
|
184 | 188 |
|
185 | 189 | fn check_fn_must_use(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
|
@@ -402,47 +406,31 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
|
402 | 406 | );
|
403 | 407 | }
|
404 | 408 | MustUsePath::Closure(span) => {
|
405 |
| - cx.struct_span_lint( |
| 409 | + cx.emit_spanned_lint( |
406 | 410 | UNUSED_MUST_USE,
|
407 | 411 | *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 }, |
417 | 413 | );
|
418 | 414 | }
|
419 | 415 | MustUsePath::Generator(span) => {
|
420 |
| - cx.struct_span_lint( |
| 416 | + cx.emit_spanned_lint( |
421 | 417 | UNUSED_MUST_USE,
|
422 | 418 | *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 }, |
432 | 420 | );
|
433 | 421 | }
|
434 | 422 | 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 | + ); |
446 | 434 | }
|
447 | 435 | }
|
448 | 436 | }
|
@@ -478,31 +466,15 @@ impl<'tcx> LateLintPass<'tcx> for PathStatements {
|
478 | 466 | if let hir::ExprKind::Path(_) = expr.kind {
|
479 | 467 | let ty = cx.typeck_results().expr_ty(expr);
|
480 | 468 | 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 }) |
499 | 476 | } 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); |
506 | 478 | }
|
507 | 479 | }
|
508 | 480 | }
|
@@ -695,36 +667,35 @@ trait UnusedDelimLint {
|
695 | 667 | } else {
|
696 | 668 | MultiSpan::from(value_span)
|
697 | 669 | };
|
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 = |
704 | 673 | if keep_space.0 &&
|
705 | 674 | let Ok(snip) = sm.span_to_prev_source(lo) && !snip.ends_with(' ') {
|
706 |
| - " ".to_string() |
| 675 | + " " |
707 | 676 | } else {
|
708 |
| - "".to_string() |
| 677 | + "" |
709 | 678 | };
|
710 | 679 |
|
711 |
| - let hi_replace = |
| 680 | + let hi_replace = |
712 | 681 | if keep_space.1 &&
|
713 | 682 | let Ok(snip) = sm.span_to_next_source(hi) && !snip.starts_with(' ') {
|
714 |
| - " ".to_string() |
| 683 | + " " |
715 | 684 | } else {
|
716 |
| - "".to_string() |
| 685 | + "" |
717 | 686 | };
|
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, |
725 | 692 | }
|
726 |
| - lint |
727 | 693 | });
|
| 694 | + cx.emit_spanned_lint( |
| 695 | + self.lint(), |
| 696 | + primary_span, |
| 697 | + UnusedDelim { delim: Self::DELIM_STR, item: msg, suggestion }, |
| 698 | + ); |
728 | 699 | }
|
729 | 700 |
|
730 | 701 | fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
|
@@ -1297,11 +1268,10 @@ impl UnusedImportBraces {
|
1297 | 1268 | ast::UseTreeKind::Nested(_) => return,
|
1298 | 1269 | };
|
1299 | 1270 |
|
1300 |
| - cx.struct_span_lint( |
| 1271 | + cx.emit_spanned_lint( |
1301 | 1272 | UNUSED_IMPORT_BRACES,
|
1302 | 1273 | item.span,
|
1303 |
| - fluent::lint_unused_import_braces, |
1304 |
| - |lint| lint.set_arg("node", node_name), |
| 1274 | + UnusedImportBracesDiag { node: node_name }, |
1305 | 1275 | );
|
1306 | 1276 | }
|
1307 | 1277 | }
|
@@ -1351,17 +1321,14 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAllocation {
|
1351 | 1321 |
|
1352 | 1322 | for adj in cx.typeck_results().expr_adjustments(e) {
|
1353 | 1323 | 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 | + }; |
1365 | 1332 | }
|
1366 | 1333 | }
|
1367 | 1334 | }
|
|
0 commit comments