1
1
use clippy_utils:: diagnostics:: span_lint_and_sugg;
2
2
use clippy_utils:: msrvs:: { self , Msrv } ;
3
+ use clippy_utils:: source:: snippet_with_applicability;
3
4
use clippy_utils:: sugg:: Sugg ;
4
5
use clippy_utils:: { std_or_core, sym} ;
5
6
use rustc_errors:: Applicability ;
6
- use rustc_hir:: { Expr , ExprKind , Mutability , QPath } ;
7
+ use rustc_hir:: { Expr , ExprKind , QPath } ;
7
8
use rustc_lint:: LateContext ;
8
9
use rustc_middle:: ty:: { self , Ty , TypeVisitableExt } ;
9
10
@@ -12,26 +13,23 @@ use super::PTR_CAST_CONSTNESS;
12
13
pub ( super ) fn check < ' tcx > (
13
14
cx : & LateContext < ' _ > ,
14
15
expr : & Expr < ' _ > ,
15
- cast_expr : & Expr < ' _ > ,
16
+ cast_from_expr : & Expr < ' _ > ,
16
17
cast_from : Ty < ' tcx > ,
17
18
cast_to : Ty < ' tcx > ,
18
19
msrv : Msrv ,
19
20
) {
20
21
if let ty:: RawPtr ( from_ty, from_mutbl) = cast_from. kind ( )
21
22
&& let ty:: RawPtr ( to_ty, to_mutbl) = cast_to. kind ( )
22
- && matches ! (
23
- ( from_mutbl, to_mutbl) ,
24
- ( Mutability :: Not , Mutability :: Mut ) | ( Mutability :: Mut , Mutability :: Not )
25
- )
23
+ && from_mutbl != to_mutbl
26
24
&& from_ty == to_ty
27
25
&& !from_ty. has_erased_regions ( )
28
26
{
29
- if let ExprKind :: Call ( func, [ ] ) = cast_expr . kind
27
+ if let ExprKind :: Call ( func, [ ] ) = cast_from_expr . kind
30
28
&& let ExprKind :: Path ( QPath :: Resolved ( None , path) ) = func. kind
31
29
&& let Some ( defid) = path. res . opt_def_id ( )
32
30
&& let Some ( prefix) = std_or_core ( cx)
33
31
&& let mut app = Applicability :: MachineApplicable
34
- && let sugg = format ! ( "{}" , Sugg :: hir_with_applicability ( cx, cast_expr , "_" , & mut app) )
32
+ && let sugg = snippet_with_applicability ( cx, cast_from_expr . span , "_" , & mut app)
35
33
&& let Some ( ( _, after_lt) ) = sugg. split_once ( "::<" )
36
34
&& let Some ( ( source, target, target_func) ) = match cx. tcx . get_diagnostic_name ( defid) {
37
35
Some ( sym:: ptr_null) => Some ( ( "const" , "mutable" , "null_mut" ) ) ,
@@ -53,11 +51,8 @@ pub(super) fn check<'tcx>(
53
51
54
52
if msrv. meets ( cx, msrvs:: POINTER_CAST_CONSTNESS ) {
55
53
let mut app = Applicability :: MachineApplicable ;
56
- let sugg = Sugg :: hir_with_context ( cx, cast_expr, expr. span . ctxt ( ) , "_" , & mut app) ;
57
- let constness = match * to_mutbl {
58
- Mutability :: Not => "const" ,
59
- Mutability :: Mut => "mut" ,
60
- } ;
54
+ let sugg = Sugg :: hir_with_context ( cx, cast_from_expr, expr. span . ctxt ( ) , "_" , & mut app) ;
55
+ let constness = to_mutbl. ptr_str ( ) ;
61
56
62
57
span_lint_and_sugg (
63
58
cx,
@@ -73,8 +68,8 @@ pub(super) fn check<'tcx>(
73
68
}
74
69
75
70
pub ( super ) fn check_null_ptr_cast_method ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) {
76
- if let ExprKind :: MethodCall ( method, cast_expr , [ ] , _) = expr. kind
77
- && let ExprKind :: Call ( func, [ ] ) = cast_expr . kind
71
+ if let ExprKind :: MethodCall ( method, cast_from_expr , [ ] , _) = expr. kind
72
+ && let ExprKind :: Call ( func, [ ] ) = cast_from_expr . kind
78
73
&& let ExprKind :: Path ( QPath :: Resolved ( None , path) ) = func. kind
79
74
&& let Some ( defid) = path. res . opt_def_id ( )
80
75
&& let method = match ( cx. tcx . get_diagnostic_name ( defid) , method. ident . name ) {
@@ -84,7 +79,7 @@ pub(super) fn check_null_ptr_cast_method(cx: &LateContext<'_>, expr: &Expr<'_>)
84
79
}
85
80
&& let Some ( prefix) = std_or_core ( cx)
86
81
&& let mut app = Applicability :: MachineApplicable
87
- && let sugg = format ! ( "{}" , Sugg :: hir_with_applicability ( cx, cast_expr , "_" , & mut app) )
82
+ && let sugg = snippet_with_applicability ( cx, cast_from_expr . span , "_" , & mut app)
88
83
&& let Some ( ( _, after_lt) ) = sugg. split_once ( "::<" )
89
84
{
90
85
span_lint_and_sugg (
0 commit comments