@@ -4,9 +4,9 @@ use clippy_utils::msrvs::{self, Msrv};
44use clippy_utils:: source:: snippet_with_applicability;
55use clippy_utils:: sugg:: Sugg ;
66use rustc_errors:: Applicability ;
7- use rustc_hir:: { Expr , ExprKind , Mutability , QPath , TyKind } ;
7+ use rustc_hir:: { self as hir , Expr , ExprKind , QPath , TyKind } ;
88use rustc_lint:: LateContext ;
9- use rustc_middle:: ty;
9+ use rustc_middle:: ty:: { self , Ty } ;
1010use rustc_span:: { Span , sym} ;
1111
1212use super :: PTR_AS_PTR ;
@@ -26,21 +26,26 @@ impl OmitFollowedCastReason<'_> {
2626 }
2727}
2828
29- pub ( super ) fn check < ' tcx > ( cx : & LateContext < ' tcx > , expr : & Expr < ' tcx > , msrv : Msrv ) {
30- if let ExprKind :: Cast ( cast_expr, cast_to_hir_ty) = expr. kind
31- && let ( cast_from, cast_to) = ( cx. typeck_results ( ) . expr_ty ( cast_expr) , cx. typeck_results ( ) . expr_ty ( expr) )
32- && let ty:: RawPtr ( _, from_mutbl) = cast_from. kind ( )
29+ pub ( super ) fn check < ' tcx > (
30+ cx : & LateContext < ' tcx > ,
31+ expr : & Expr < ' tcx > ,
32+ cast_from_expr : & Expr < ' _ > ,
33+ cast_from : Ty < ' _ > ,
34+ cast_to_hir : & hir:: Ty < ' _ > ,
35+ cast_to : Ty < ' tcx > ,
36+ msrv : Msrv ,
37+ ) {
38+ if let ty:: RawPtr ( _, from_mutbl) = cast_from. kind ( )
3339 && let ty:: RawPtr ( to_pointee_ty, to_mutbl) = cast_to. kind ( )
34- && matches ! ( ( from_mutbl, to_mutbl) ,
35- ( Mutability :: Not , Mutability :: Not ) | ( Mutability :: Mut , Mutability :: Mut ) )
40+ && from_mutbl == to_mutbl
3641 // The `U` in `pointer::cast` have to be `Sized`
3742 // as explained here: https://github.com/rust-lang/rust/issues/60602.
3843 && to_pointee_ty. is_sized ( cx. tcx , cx. typing_env ( ) )
3944 && msrv. meets ( cx, msrvs:: POINTER_CAST )
4045 && !is_from_proc_macro ( cx, expr)
4146 {
4247 let mut app = Applicability :: MachineApplicable ;
43- let turbofish = match & cast_to_hir_ty . kind {
48+ let turbofish = match & cast_to_hir . kind {
4449 TyKind :: Infer ( ( ) ) => String :: new ( ) ,
4550 TyKind :: Ptr ( mut_ty) => {
4651 if matches ! ( mut_ty. ty. kind, TyKind :: Infer ( ( ) ) ) {
@@ -58,7 +63,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv)
5863 // following `cast` does not compile because it fails to infer what type is expected
5964 // as type argument to `std::ptr::ptr_null` or `std::ptr::ptr_null_mut`, so
6065 // we omit following `cast`:
61- let omit_cast = if let ExprKind :: Call ( func, [ ] ) = cast_expr . kind
66+ let omit_cast = if let ExprKind :: Call ( func, [ ] ) = cast_from_expr . kind
6267 && let ExprKind :: Path ( ref qpath @ QPath :: Resolved ( None , path) ) = func. kind
6368 && let Some ( method_defid) = path. res . opt_def_id ( )
6469 {
@@ -76,7 +81,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv)
7681 let method = snippet_with_applicability ( cx, qpath_span_without_turbofish ( method) , ".." , & mut app) ;
7782 ( "try call directly" , format ! ( "{method}{turbofish}()" ) )
7883 } else {
79- let cast_expr_sugg = Sugg :: hir_with_context ( cx, cast_expr , expr. span . ctxt ( ) , "_" , & mut app) ;
84+ let cast_expr_sugg = Sugg :: hir_with_context ( cx, cast_from_expr , expr. span . ctxt ( ) , "_" , & mut app) ;
8085
8186 (
8287 "try `pointer::cast`, a safer alternative" ,
0 commit comments