Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 19 additions & 6 deletions compiler/rustc_hir_typeck/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,25 @@ impl<'a, 'tcx> CastCheck<'tcx> {
.must_apply_modulo_regions()
{
label = false;
err.span_suggestion(
self.span,
"consider using the `From` trait instead",
format!("{}::from({})", self.cast_ty, snippet),
Applicability::MaybeIncorrect,
);
if let ty::Adt(def, args) = self.cast_ty.kind() {
err.span_suggestion_verbose(
self.span,
"consider using the `From` trait instead",
format!(
"{}::from({})",
fcx.tcx.value_path_str_with_args(def.did(), args),
snippet
),
Applicability::MaybeIncorrect,
);
} else {
err.span_suggestion(
self.span,
"consider using the `From` trait instead",
format!("{}::from({})", self.cast_ty, snippet),
Applicability::MaybeIncorrect,
);
};
}
}

Expand Down
7 changes: 6 additions & 1 deletion tests/ui/coercion/issue-73886.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ error[E0605]: non-primitive cast: `u32` as `Option<_>`
--> $DIR/issue-73886.rs:4:13
|
LL | let _ = 7u32 as Option<_>;
| ^^^^^^^^^^^^^^^^^ help: consider using the `From` trait instead: `Option<_>::from(7u32)`
| ^^^^^^^^^^^^^^^^^
|
= note: an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
help: consider using the `From` trait instead
|
LL - let _ = 7u32 as Option<_>;
LL + let _ = Option::<_>::from(7u32);
|

error: aborting due to 2 previous errors

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/coercion/non-primitive-cast-135412.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//@ run-rustfix

use std::sync::Arc;

fn main() {
let _ = Option::<_>::from(7u32);
//~^ ERROR non-primitive cast: `u32` as `Option<_>`
let _ = Arc::<str>::from("String");
//~^ ERROR non-primitive cast: `&'static str` as `Arc<str>`
}
10 changes: 10 additions & 0 deletions tests/ui/coercion/non-primitive-cast-135412.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//@ run-rustfix

use std::sync::Arc;

fn main() {
let _ = 7u32 as Option<_>;
//~^ ERROR non-primitive cast: `u32` as `Option<_>`
let _ = "String" as Arc<str>;
//~^ ERROR non-primitive cast: `&'static str` as `Arc<str>`
}
29 changes: 29 additions & 0 deletions tests/ui/coercion/non-primitive-cast-135412.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
error[E0605]: non-primitive cast: `u32` as `Option<_>`
--> $DIR/non-primitive-cast-135412.rs:6:13
|
LL | let _ = 7u32 as Option<_>;
| ^^^^^^^^^^^^^^^^^
|
= note: an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
help: consider using the `From` trait instead
|
LL - let _ = 7u32 as Option<_>;
LL + let _ = Option::<_>::from(7u32);
|

error[E0605]: non-primitive cast: `&'static str` as `Arc<str>`
--> $DIR/non-primitive-cast-135412.rs:8:13
|
LL | let _ = "String" as Arc<str>;
| ^^^^^^^^^^^^^^^^^^^^
|
= note: an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
help: consider using the `From` trait instead
|
LL - let _ = "String" as Arc<str>;
LL + let _ = Arc::<str>::from("String");
|

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0605`.
Loading