1
Fork 0

migrate dead.rs to translateable diagnostics

This commit is contained in:
Nathan Stocks 2022-10-01 18:39:02 -06:00
parent f0afb88302
commit a7aa1850b2
4 changed files with 20 additions and 17 deletions

View file

@ -558,3 +558,9 @@ passes_incorrect_target =
[one] argument
*[other] arguments
}
passes_useless_assignment =
useless assignment of {$is_field_assign ->
[true] field
*[false] variable
} of type `{$ty}` to itself

View file

@ -988,11 +988,7 @@ impl Handler {
}
pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
if self.inner.borrow().has_errors() {
Some(ErrorGuaranteed(()))
} else {
None
}
if self.inner.borrow().has_errors() { Some(ErrorGuaranteed(())) } else { None }
}
pub fn has_errors_or_lint_errors(&self) -> Option<ErrorGuaranteed> {
if self.inner.borrow().has_errors_or_lint_errors() {

View file

@ -4,7 +4,7 @@
use itertools::Itertools;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::{pluralize, Applicability, DelayDm, MultiSpan};
use rustc_errors::{pluralize, Applicability, MultiSpan};
use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId};
@ -18,6 +18,8 @@ use rustc_session::lint;
use rustc_span::symbol::{sym, Symbol};
use std::mem;
use crate::errors::UselessAssignment;
// Any local node that may call something in its body block should be
// explored. For example, if it's a live Node::Item that is a
// function, then we should explore its block to check for codes that
@ -180,19 +182,11 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
&& !assign.span.from_expansion()
{
let is_field_assign = matches!(lhs.kind, hir::ExprKind::Field(..));
self.tcx.struct_span_lint_hir(
self.tcx.emit_spanned_lint(
lint::builtin::DEAD_CODE,
assign.hir_id,
assign.span,
DelayDm(|| format!(
"useless assignment of {} of type `{}` to itself",
if is_field_assign { "field" } else { "variable" },
self.typeck_results().expr_ty(lhs),
)),
|lint| {
lint
},
UselessAssignment { is_field_assign, ty: self.typeck_results().expr_ty(lhs) }
)
}
}

View file

@ -7,7 +7,7 @@ use rustc_ast::Label;
use rustc_errors::{error_code, Applicability, ErrorGuaranteed, IntoDiagnostic, MultiSpan};
use rustc_hir::{self as hir, ExprKind, Target};
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
use rustc_middle::ty::MainDefinition;
use rustc_middle::ty::{MainDefinition, Ty};
use rustc_span::{Span, Symbol, DUMMY_SP};
#[derive(LintDiagnostic)]
@ -1265,3 +1265,10 @@ pub struct IncorrectTarget<'a> {
pub actual_num: usize,
pub at_least: bool,
}
#[derive(LintDiagnostic)]
#[diag(passes::useless_assignment)]
pub struct UselessAssignment<'a> {
pub is_field_assign: bool,
pub ty: Ty<'a>,
}