Skip to content

Commit a7aa185

Browse files
committed
migrate dead.rs to translateable diagnostics
1 parent f0afb88 commit a7aa185

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,9 @@ passes_incorrect_target =
558558
[one] argument
559559
*[other] arguments
560560
}
561+
562+
passes_useless_assignment =
563+
useless assignment of {$is_field_assign ->
564+
[true] field
565+
*[false] variable
566+
} of type `{$ty}` to itself

compiler/rustc_errors/src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -988,11 +988,7 @@ impl Handler {
988988
}
989989

990990
pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
991-
if self.inner.borrow().has_errors() {
992-
Some(ErrorGuaranteed(()))
993-
} else {
994-
None
995-
}
991+
if self.inner.borrow().has_errors() { Some(ErrorGuaranteed(())) } else { None }
996992
}
997993
pub fn has_errors_or_lint_errors(&self) -> Option<ErrorGuaranteed> {
998994
if self.inner.borrow().has_errors_or_lint_errors() {

compiler/rustc_passes/src/dead.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use itertools::Itertools;
66
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
7-
use rustc_errors::{pluralize, Applicability, DelayDm, MultiSpan};
7+
use rustc_errors::{pluralize, Applicability, MultiSpan};
88
use rustc_hir as hir;
99
use rustc_hir::def::{CtorOf, DefKind, Res};
1010
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -18,6 +18,8 @@ use rustc_session::lint;
1818
use rustc_span::symbol::{sym, Symbol};
1919
use std::mem;
2020

21+
use crate::errors::UselessAssignment;
22+
2123
// Any local node that may call something in its body block should be
2224
// explored. For example, if it's a live Node::Item that is a
2325
// function, then we should explore its block to check for codes that
@@ -180,19 +182,11 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
180182
&& !assign.span.from_expansion()
181183
{
182184
let is_field_assign = matches!(lhs.kind, hir::ExprKind::Field(..));
183-
self.tcx.struct_span_lint_hir(
185+
self.tcx.emit_spanned_lint(
184186
lint::builtin::DEAD_CODE,
185187
assign.hir_id,
186188
assign.span,
187-
DelayDm(|| format!(
188-
"useless assignment of {} of type `{}` to itself",
189-
if is_field_assign { "field" } else { "variable" },
190-
self.typeck_results().expr_ty(lhs),
191-
)),
192-
|lint| {
193-
lint
194-
195-
},
189+
UselessAssignment { is_field_assign, ty: self.typeck_results().expr_ty(lhs) }
196190
)
197191
}
198192
}

compiler/rustc_passes/src/errors.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_ast::Label;
77
use rustc_errors::{error_code, Applicability, ErrorGuaranteed, IntoDiagnostic, MultiSpan};
88
use rustc_hir::{self as hir, ExprKind, Target};
99
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
10-
use rustc_middle::ty::MainDefinition;
10+
use rustc_middle::ty::{MainDefinition, Ty};
1111
use rustc_span::{Span, Symbol, DUMMY_SP};
1212

1313
#[derive(LintDiagnostic)]
@@ -1265,3 +1265,10 @@ pub struct IncorrectTarget<'a> {
12651265
pub actual_num: usize,
12661266
pub at_least: bool,
12671267
}
1268+
1269+
#[derive(LintDiagnostic)]
1270+
#[diag(passes::useless_assignment)]
1271+
pub struct UselessAssignment<'a> {
1272+
pub is_field_assign: bool,
1273+
pub ty: Ty<'a>,
1274+
}

0 commit comments

Comments
 (0)