@@ -17,6 +17,8 @@ pub(super) fn check<'tcx>(
1717 arg : & ' tcx Expr < ' _ > ,
1818 msrv : Msrv ,
1919) -> bool {
20+ let mut applicability = Applicability :: MachineApplicable ;
21+ let arg_sugg = sugg:: Sugg :: hir_with_context ( cx, arg, e. span . ctxt ( ) , ".." , & mut applicability) ;
2022 match ( from_ty. kind ( ) , to_ty. kind ( ) ) {
2123 ( ty:: RawPtr ( from_pointee_ty, from_mutbl) , ty:: RawPtr ( to_pointee_ty, to_mutbl) ) => {
2224 span_lint_and_then (
@@ -25,40 +27,38 @@ pub(super) fn check<'tcx>(
2527 e. span ,
2628 "transmute from a pointer to a pointer" ,
2729 |diag| {
28- if let Some ( arg) = sugg:: Sugg :: hir_opt ( cx, arg) {
29- if from_mutbl == to_mutbl
30- && to_pointee_ty. is_sized ( cx. tcx , cx. typing_env ( ) )
31- && msrv. meets ( cx, msrvs:: POINTER_CAST )
32- {
33- diag. span_suggestion_verbose (
34- e. span ,
35- "use `pointer::cast` instead" ,
36- format ! ( "{}.cast::<{to_pointee_ty}>()" , arg. maybe_paren( ) ) ,
37- Applicability :: MaybeIncorrect ,
38- ) ;
39- } else if from_pointee_ty == to_pointee_ty
40- && let Some ( method) = match ( from_mutbl, to_mutbl) {
41- ( ty:: Mutability :: Not , ty:: Mutability :: Mut ) => Some ( "cast_mut" ) ,
42- ( ty:: Mutability :: Mut , ty:: Mutability :: Not ) => Some ( "cast_const" ) ,
43- _ => None ,
44- }
45- && !from_pointee_ty. has_erased_regions ( )
46- && msrv. meets ( cx, msrvs:: POINTER_CAST_CONSTNESS )
47- {
48- diag. span_suggestion_verbose (
49- e. span ,
50- format ! ( "use `pointer::{method}` instead" ) ,
51- format ! ( "{}.{method}()" , arg. maybe_paren( ) ) ,
52- Applicability :: MaybeIncorrect ,
53- ) ;
54- } else {
55- diag. span_suggestion_verbose (
56- e. span ,
57- "use an `as` cast instead" ,
58- arg. as_ty ( to_ty) ,
59- Applicability :: MaybeIncorrect ,
60- ) ;
30+ if from_mutbl == to_mutbl
31+ && to_pointee_ty. is_sized ( cx. tcx , cx. typing_env ( ) )
32+ && msrv. meets ( cx, msrvs:: POINTER_CAST )
33+ {
34+ diag. span_suggestion_verbose (
35+ e. span ,
36+ "use `pointer::cast` instead" ,
37+ format ! ( "{}.cast::<{to_pointee_ty}>()" , arg_sugg. maybe_paren( ) ) ,
38+ Applicability :: MaybeIncorrect ,
39+ ) ;
40+ } else if from_pointee_ty == to_pointee_ty
41+ && let Some ( method) = match ( from_mutbl, to_mutbl) {
42+ ( ty:: Mutability :: Not , ty:: Mutability :: Mut ) => Some ( "cast_mut" ) ,
43+ ( ty:: Mutability :: Mut , ty:: Mutability :: Not ) => Some ( "cast_const" ) ,
44+ _ => None ,
6145 }
46+ && !from_pointee_ty. has_erased_regions ( )
47+ && msrv. meets ( cx, msrvs:: POINTER_CAST_CONSTNESS )
48+ {
49+ diag. span_suggestion_verbose (
50+ e. span ,
51+ format ! ( "use `pointer::{method}` instead" ) ,
52+ format ! ( "{}.{method}()" , arg_sugg. maybe_paren( ) ) ,
53+ Applicability :: MaybeIncorrect ,
54+ ) ;
55+ } else {
56+ diag. span_suggestion_verbose (
57+ e. span ,
58+ "use an `as` cast instead" ,
59+ arg_sugg. as_ty ( to_ty) ,
60+ Applicability :: MaybeIncorrect ,
61+ ) ;
6262 }
6363 } ,
6464 ) ;
0 commit comments