Skip to content

Commit 80a9699

Browse files
committed
Migrate 'trivial cast' lint
1 parent 82471e9 commit 80a9699

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

compiler/rustc_hir_typeck/messages.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ hir_typeck_suggest_boxing_when_appropriate = store this in the heap by calling `
145145
146146
hir_typeck_suggest_ptr_null_mut = consider using `core::ptr::null_mut` instead
147147
148+
hir_typeck_trivial_cast = trivial {$numeric ->
149+
[true] numeric cast
150+
*[false] cast
151+
}: `{$expr_ty}` as `{$cast_ty}`
152+
.help = cast can be replaced by coercion; this might require a temporary variable
153+
148154
hir_typeck_union_pat_dotdot = `..` cannot be used in union patterns
149155
150156
hir_typeck_union_pat_multiple_fields = union patterns should have exactly one field

compiler/rustc_hir_typeck/src/cast.rs

+7-20
Original file line numberDiff line numberDiff line change
@@ -631,31 +631,18 @@ impl<'a, 'tcx> CastCheck<'tcx> {
631631
}
632632

633633
fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
634-
let t_cast = self.cast_ty;
635-
let t_expr = self.expr_ty;
636-
let (adjective, lint) = if t_cast.is_numeric() && t_expr.is_numeric() {
637-
("numeric ", lint::builtin::TRIVIAL_NUMERIC_CASTS)
634+
let (numeric, lint) = if self.cast_ty.is_numeric() && self.expr_ty.is_numeric() {
635+
(true, lint::builtin::TRIVIAL_NUMERIC_CASTS)
638636
} else {
639-
("", lint::builtin::TRIVIAL_CASTS)
637+
(false, lint::builtin::TRIVIAL_CASTS)
640638
};
641-
fcx.tcx.struct_span_lint_hir(
639+
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
640+
let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
641+
fcx.tcx.emit_spanned_lint(
642642
lint,
643643
self.expr.hir_id,
644644
self.span,
645-
DelayDm(|| {
646-
format!(
647-
"trivial {}cast: `{}` as `{}`",
648-
adjective,
649-
fcx.ty_to_string(t_expr),
650-
fcx.ty_to_string(t_cast)
651-
)
652-
}),
653-
|lint| {
654-
lint.help(
655-
"cast can be replaced by coercion; this might \
656-
require a temporary variable",
657-
)
658-
},
645+
errors::TrivialCast { numeric, expr_ty, cast_ty },
659646
);
660647
}
661648

compiler/rustc_hir_typeck/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,15 @@ pub struct SuggestPtrNullMut {
533533
pub span: Span,
534534
}
535535

536+
#[derive(LintDiagnostic)]
537+
#[diag(hir_typeck_trivial_cast)]
538+
#[help]
539+
pub struct TrivialCast<'tcx> {
540+
pub numeric: bool,
541+
pub expr_ty: Ty<'tcx>,
542+
pub cast_ty: Ty<'tcx>,
543+
}
544+
536545
#[derive(Diagnostic)]
537546
#[diag(hir_typeck_no_associated_item, code = "E0599")]
538547
pub struct NoAssociatedItem {

0 commit comments

Comments
 (0)