Skip to content

Commit d55485b

Browse files
committed
Migrate 'trivial cast' lint
1 parent 8dcdba1 commit d55485b

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

compiler/rustc_hir_typeck/messages.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ hir_typeck_suggest_boxing_when_appropriate = store this in the heap by calling `
134134
135135
hir_typeck_suggest_ptr_null_mut = consider using `core::ptr::null_mut` instead
136136
137+
hir_typeck_trivial_cast = trivial {$numeric ->
138+
[true] numeric cast
139+
*[false] cast
140+
}: `{$expr_ty}` as `{$cast_ty}`
141+
.help = cast can be replaced by coercion; this might require a temporary variable
142+
137143
hir_typeck_union_pat_dotdot = `..` cannot be used in union patterns
138144
139145
hir_typeck_union_pat_multiple_fields = union patterns should have exactly one field

compiler/rustc_hir_typeck/src/cast.rs

+5-20
Original file line numberDiff line numberDiff line change
@@ -634,31 +634,16 @@ impl<'a, 'tcx> CastCheck<'tcx> {
634634
}
635635

636636
fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
637-
let t_cast = self.cast_ty;
638-
let t_expr = self.expr_ty;
639-
let (adjective, lint) = if t_cast.is_numeric() && t_expr.is_numeric() {
640-
("numeric ", lint::builtin::TRIVIAL_NUMERIC_CASTS)
637+
let (numeric, lint) = if self.cast_ty.is_numeric() && self.expr_ty.is_numeric() {
638+
(true, lint::builtin::TRIVIAL_NUMERIC_CASTS)
641639
} else {
642-
("", lint::builtin::TRIVIAL_CASTS)
640+
(false, lint::builtin::TRIVIAL_CASTS)
643641
};
644-
fcx.tcx.struct_span_lint_hir(
642+
fcx.tcx.emit_spanned_lint(
645643
lint,
646644
self.expr.hir_id,
647645
self.span,
648-
DelayDm(|| {
649-
format!(
650-
"trivial {}cast: `{}` as `{}`",
651-
adjective,
652-
fcx.ty_to_string(t_expr),
653-
fcx.ty_to_string(t_cast)
654-
)
655-
}),
656-
|lint| {
657-
lint.help(
658-
"cast can be replaced by coercion; this might \
659-
require a temporary variable",
660-
)
661-
},
646+
errors::TrivialCast { numeric, expr_ty: self.expr_ty, cast_ty: self.cast_ty },
662647
);
663648
}
664649

compiler/rustc_hir_typeck/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,15 @@ pub struct SuggestPtrNullMut {
487487
pub span: Span,
488488
}
489489

490+
#[derive(LintDiagnostic)]
491+
#[diag(hir_typeck_trivial_cast)]
492+
#[help]
493+
pub struct TrivialCast<'tcx> {
494+
pub numeric: bool,
495+
pub expr_ty: Ty<'tcx>,
496+
pub cast_ty: Ty<'tcx>,
497+
}
498+
490499
#[derive(Diagnostic)]
491500
#[diag(hir_typeck_no_associated_item, code = "E0599")]
492501
pub struct NoAssociatedItem {

0 commit comments

Comments
 (0)