Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
reword suggestion message
  • Loading branch information
estebank committed Jun 6, 2025
commit 3c049e21ca75134b7cab84507c93c18ce38ff275
11 changes: 7 additions & 4 deletions compiler/rustc_hir_typeck/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,17 +381,20 @@ impl<'a, 'tcx> CastCheck<'tcx> {
if self.expr_ty.is_numeric() {
if self.expr_ty == fcx.tcx.types.u32 {
err.multipart_suggestion(
"try `char::from_u32` instead",
"consider using `char::from_u32` instead",
vec![
(self.expr_span.shrink_to_lo(), "char::from_u32(".to_string()),
(self.expr_span.shrink_to_hi().to(self.cast_span), ")".to_string()),
],
Applicability::MachineApplicable,
);
} else if self.expr_ty == fcx.tcx.types.i8 {
err.span_help(self.span, "try casting from `u8` instead");
err.span_help(self.span, "consider casting from `u8` instead");
} else {
err.span_help(self.span, "try `char::from_u32` instead (via a `u32`)");
err.span_help(
self.span,
"consider using `char::from_u32` instead (via a `u32`)",
);
};
}
err.emit();
Expand Down Expand Up @@ -643,7 +646,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
let mtstr = mt.prefix_str();
err.span_suggestion_verbose(
self.cast_span.shrink_to_lo(),
"try casting to a reference instead",
"consider casting to a reference instead",
format!("&{mtstr}"),
Applicability::MachineApplicable,
);
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/cast/cast-to-slice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0620]: cast to unsized type: `&[u8]` as `[char]`
LL | "example".as_bytes() as [char];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: try casting to a reference instead
help: consider casting to a reference instead
|
LL | "example".as_bytes() as &[char];
| +
Expand All @@ -15,7 +15,7 @@ error[E0620]: cast to unsized type: `&[u8]` as `[char]`
LL | arr as [char];
| ^^^^^^^^^^^^^
|
help: try casting to a reference instead
help: consider casting to a reference instead
|
LL | arr as &[char];
| +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0620]: cast to unsized type: `&{integer}` as `dyn Send`
LL | &1 as dyn Send;
| ^^^^^^^^^^^^^^
|
help: try casting to a reference instead
help: consider casting to a reference instead
|
LL | &1 as &dyn Send;
| +
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/const-eval/const-eval-overflow-4b.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ error[E0604]: only `u8` can be cast as `char`, not `i8`
LL | : [u32; 5i8 as char as usize]
| ^^^^^^^^^^^ invalid cast
|
help: try casting from `u8` instead
help: consider casting from `u8` instead
--> $DIR/const-eval-overflow-4b.rs:24:13
|
LL | : [u32; 5i8 as char as usize]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0604.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0604]: only `u8` can be cast as `char`, not `u32`
LL | 1u32 as char;
| ^^^^^^^^^^^^ invalid cast
|
help: try `char::from_u32` instead
help: consider using `char::from_u32` instead
|
LL - 1u32 as char;
LL + char::from_u32(1u32);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0620.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0620]: cast to unsized type: `&[usize; 2]` as `[usize]`
LL | let _foo = &[1_usize, 2] as [usize];
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
help: try casting to a reference instead
help: consider casting to a reference instead
|
LL | let _foo = &[1_usize, 2] as &[usize];
| +
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error-emitter/error-festival.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ error[E0604]: only `u8` can be cast as `char`, not `u32`
LL | 0u32 as char;
| ^^^^^^^^^^^^ invalid cast
|
help: try `char::from_u32` instead
help: consider using `char::from_u32` instead
|
LL - 0u32 as char;
LL + char::from_u32(0u32);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-17441.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0620]: cast to unsized type: `&[usize; 2]` as `[usize]`
LL | let _foo = &[1_usize, 2] as [usize];
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
help: try casting to a reference instead
help: consider casting to a reference instead
|
LL | let _foo = &[1_usize, 2] as &[usize];
| +
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/mismatched_types/cast-rfc0401.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ error[E0604]: only `u8` can be cast as `char`, not `u32`
LL | let _ = 0x61u32 as char;
| ^^^^^^^^^^^^^^^ invalid cast
|
help: try `char::from_u32` instead
help: consider using `char::from_u32` instead
|
LL - let _ = 0x61u32 as char;
LL + let _ = char::from_u32(0x61u32);
Expand Down
Loading