1
Fork 0

Improve u32 to char diagnostic

This commit is contained in:
gimbles 2022-05-19 20:03:40 +05:30
parent e6327bc8b8
commit 9e5c24eaf8
4 changed files with 25 additions and 28 deletions

View file

@ -347,16 +347,22 @@ impl<'a, 'tcx> CastCheck<'tcx> {
);
err.span_label(self.span, "invalid cast");
if self.expr_ty.is_numeric() {
err.span_help(
self.span,
if self.expr_ty == fcx.tcx.types.i8 {
"try casting from `u8` instead"
} else if self.expr_ty == fcx.tcx.types.u32 {
"try `char::from_u32` instead"
} else {
"try `char::from_u32` instead (via a `u32`)"
},
);
if self.expr_ty == fcx.tcx.types.u32 {
match fcx.tcx.sess.source_map().span_to_snippet(self.expr.span) {
Ok(snippet) => err.span_suggestion(
self.span,
"try `char::from_u32` instead",
format!("char::from_u32({snippet})"),
Applicability::MachineApplicable,
),
Err(_) => err.span_help(self.span, "try `char::from_u32` instead"),
};
} else if self.expr_ty == fcx.tcx.types.i8 {
err.span_help(self.span, "try casting from `u8` instead");
} else {
err.span_help(self.span, "try `char::from_u32` instead (via a `u32`)");
};
}
err.emit();
}