Skip to content

Commit 78cfc8d

Browse files
committed
Migrate 'trivial cast' lint
1 parent cb8f360 commit 78cfc8d

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
@@ -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

+7-20
Original file line numberDiff line numberDiff line change
@@ -635,31 +635,18 @@ impl<'a, 'tcx> CastCheck<'tcx> {
635635
}
636636

637637
fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
638-
let t_cast = self.cast_ty;
639-
let t_expr = self.expr_ty;
640-
let (adjective, lint) = if t_cast.is_numeric() && t_expr.is_numeric() {
641-
("numeric ", lint::builtin::TRIVIAL_NUMERIC_CASTS)
638+
let (numeric, lint) = if self.cast_ty.is_numeric() && self.expr_ty.is_numeric() {
639+
(true, lint::builtin::TRIVIAL_NUMERIC_CASTS)
642640
} else {
643-
("", lint::builtin::TRIVIAL_CASTS)
641+
(false, lint::builtin::TRIVIAL_CASTS)
644642
};
645-
fcx.tcx.struct_span_lint_hir(
643+
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
644+
let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
645+
fcx.tcx.emit_spanned_lint(
646646
lint,
647647
self.expr.hir_id,
648648
self.span,
649-
DelayDm(|| {
650-
format!(
651-
"trivial {}cast: `{}` as `{}`",
652-
adjective,
653-
fcx.ty_to_string(t_expr),
654-
fcx.ty_to_string(t_cast)
655-
)
656-
}),
657-
|lint| {
658-
lint.help(
659-
"cast can be replaced by coercion; this might \
660-
require a temporary variable",
661-
)
662-
},
649+
errors::TrivialCast { numeric, expr_ty, cast_ty },
663650
);
664651
}
665652

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)