@@ -4,9 +4,9 @@ use clippy_utils::msrvs::{self, Msrv};
4
4
use clippy_utils:: source:: snippet_with_applicability;
5
5
use clippy_utils:: sugg:: Sugg ;
6
6
use rustc_errors:: Applicability ;
7
- use rustc_hir:: { Expr , ExprKind , Mutability , QPath , TyKind } ;
7
+ use rustc_hir:: { self as hir , Expr , ExprKind , QPath , TyKind } ;
8
8
use rustc_lint:: LateContext ;
9
- use rustc_middle:: ty;
9
+ use rustc_middle:: ty:: { self , Ty } ;
10
10
use rustc_span:: { Span , sym} ;
11
11
12
12
use super :: PTR_AS_PTR ;
@@ -26,21 +26,26 @@ impl OmitFollowedCastReason<'_> {
26
26
}
27
27
}
28
28
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 ( )
33
39
&& 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
36
41
// The `U` in `pointer::cast` have to be `Sized`
37
42
// as explained here: https://github.com/rust-lang/rust/issues/60602.
38
43
&& to_pointee_ty. is_sized ( cx. tcx , cx. typing_env ( ) )
39
44
&& msrv. meets ( cx, msrvs:: POINTER_CAST )
40
45
&& !is_from_proc_macro ( cx, expr)
41
46
{
42
47
let mut app = Applicability :: MachineApplicable ;
43
- let turbofish = match & cast_to_hir_ty . kind {
48
+ let turbofish = match & cast_to_hir . kind {
44
49
TyKind :: Infer ( ( ) ) => String :: new ( ) ,
45
50
TyKind :: Ptr ( mut_ty) => {
46
51
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)
58
63
// following `cast` does not compile because it fails to infer what type is expected
59
64
// as type argument to `std::ptr::ptr_null` or `std::ptr::ptr_null_mut`, so
60
65
// 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
62
67
&& let ExprKind :: Path ( ref qpath @ QPath :: Resolved ( None , path) ) = func. kind
63
68
&& let Some ( method_defid) = path. res . opt_def_id ( )
64
69
{
@@ -76,7 +81,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv)
76
81
let method = snippet_with_applicability ( cx, qpath_span_without_turbofish ( method) , ".." , & mut app) ;
77
82
( "try call directly" , format ! ( "{method}{turbofish}()" ) )
78
83
} 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) ;
80
85
81
86
(
82
87
"try `pointer::cast`, a safer alternative" ,
0 commit comments