File tree 2 files changed +8
-10
lines changed
compiler/rustc_hir_typeck/src
2 files changed +8
-10
lines changed Original file line number Diff line number Diff line change @@ -501,12 +501,15 @@ impl<'a, 'tcx> CastCheck<'tcx> {
501
501
. must_apply_modulo_regions ( )
502
502
{
503
503
label = false ;
504
- if !self . cast_ty . is_primitive ( ) {
505
- let type_to_cast = self . cast_ty . to_string ( ) . replacen ( '<' , "::<" , 1 ) ;
504
+ if let ty:: Adt ( def, args) = self . cast_ty . kind ( ) {
506
505
err. span_suggestion_verbose (
507
506
self . span ,
508
507
"consider using the `From` trait instead" ,
509
- format ! ( "{}::from({})" , type_to_cast, snippet) ,
508
+ format ! (
509
+ "{}::from({})" ,
510
+ fcx. tcx. value_path_str_with_args( def. did( ) , args) ,
511
+ snippet
512
+ ) ,
510
513
Applicability :: MaybeIncorrect ,
511
514
) ;
512
515
} else {
@@ -516,7 +519,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
516
519
format ! ( "{}::from({})" , self . cast_ty, snippet) ,
517
520
Applicability :: MaybeIncorrect ,
518
521
) ;
519
- }
522
+ } ;
520
523
}
521
524
}
522
525
Original file line number Diff line number Diff line change @@ -11,14 +11,9 @@ error[E0605]: non-primitive cast: `Foo<'a>` as `T`
11
11
--> $DIR/issue-16048.rs:24:16
12
12
|
13
13
LL | return *self as T;
14
- | ^^^^^^^^^^
14
+ | ^^^^^^^^^^ help: consider using the `From` trait instead: `T::from(*self)`
15
15
|
16
16
= note: an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
17
- help: consider using the `From` trait instead
18
- |
19
- LL - return *self as T;
20
- LL + return T::from(*self);
21
- |
22
17
23
18
error: aborting due to 2 previous errors
24
19
You can’t perform that action at this time.
0 commit comments