@@ -2,9 +2,10 @@ use clippy_config::msrvs::{self, Msrv};
2
2
use clippy_utils:: diagnostics:: span_lint_and_sugg;
3
3
use clippy_utils:: sugg:: Sugg ;
4
4
use rustc_errors:: Applicability ;
5
- use rustc_hir:: { Expr , Mutability } ;
5
+ use rustc_hir:: { Expr , ExprKind , Mutability , QPath } ;
6
6
use rustc_lint:: LateContext ;
7
7
use rustc_middle:: ty:: { self , Ty , TypeVisitableExt } ;
8
+ use rustc_span:: sym;
8
9
9
10
use super :: PTR_CAST_CONSTNESS ;
10
11
@@ -16,8 +17,7 @@ pub(super) fn check<'tcx>(
16
17
cast_to : Ty < ' tcx > ,
17
18
msrv : & Msrv ,
18
19
) {
19
- if msrv. meets ( msrvs:: POINTER_CAST_CONSTNESS )
20
- && let ty:: RawPtr ( from_ty, from_mutbl) = cast_from. kind ( )
20
+ if let ty:: RawPtr ( from_ty, from_mutbl) = cast_from. kind ( )
21
21
&& let ty:: RawPtr ( to_ty, to_mutbl) = cast_to. kind ( )
22
22
&& matches ! (
23
23
( from_mutbl, to_mutbl) ,
@@ -26,20 +26,54 @@ pub(super) fn check<'tcx>(
26
26
&& from_ty == to_ty
27
27
&& !from_ty. has_erased_regions ( )
28
28
{
29
- let sugg = Sugg :: hir ( cx, cast_expr, "_" ) ;
30
- let constness = match * to_mutbl {
31
- Mutability :: Not => "const" ,
32
- Mutability :: Mut => "mut" ,
33
- } ;
29
+ if let ExprKind :: Call ( func, [ ] ) = cast_expr. kind
30
+ && let ExprKind :: Path ( QPath :: Resolved ( None , path) ) = func. kind
31
+ && let Some ( method_defid) = path. res . opt_def_id ( )
32
+ {
33
+ let mut app = Applicability :: MachineApplicable ;
34
+ let sugg = Sugg :: hir_with_applicability ( cx, cast_expr, "_" , & mut app) . to_string ( ) ;
35
+ if cx. tcx . is_diagnostic_item ( sym:: ptr_null, method_defid) {
36
+ span_lint_and_sugg (
37
+ cx,
38
+ PTR_CAST_CONSTNESS ,
39
+ expr. span ,
40
+ "`as` casting to make a const null pointer into a mutable null pointer" ,
41
+ "use `null_mut()` directly instead" ,
42
+ sugg. replace ( "null" , "null_mut" ) ,
43
+ app,
44
+ ) ;
45
+ return ;
46
+ }
47
+ if cx. tcx . is_diagnostic_item ( sym:: ptr_null_mut, method_defid) {
48
+ span_lint_and_sugg (
49
+ cx,
50
+ PTR_CAST_CONSTNESS ,
51
+ expr. span ,
52
+ "`as` casting to make a mutable null pointer into an immutable null pointer" ,
53
+ "use `null()` directly instead" ,
54
+ sugg. replace ( "null_mut" , "null" ) ,
55
+ app,
56
+ ) ;
57
+ return ;
58
+ }
59
+ }
34
60
35
- span_lint_and_sugg (
36
- cx,
37
- PTR_CAST_CONSTNESS ,
38
- expr. span ,
39
- "`as` casting between raw pointers while changing only its constness" ,
40
- format ! ( "try `pointer::cast_{constness}`, a safer alternative" ) ,
41
- format ! ( "{}.cast_{constness}()" , sugg. maybe_par( ) ) ,
42
- Applicability :: MachineApplicable ,
43
- ) ;
61
+ if msrv. meets ( msrvs:: POINTER_CAST_CONSTNESS ) {
62
+ let sugg = Sugg :: hir ( cx, cast_expr, "_" ) ;
63
+ let constness = match * to_mutbl {
64
+ Mutability :: Not => "const" ,
65
+ Mutability :: Mut => "mut" ,
66
+ } ;
67
+
68
+ span_lint_and_sugg (
69
+ cx,
70
+ PTR_CAST_CONSTNESS ,
71
+ expr. span ,
72
+ "`as` casting between raw pointers while changing only its constness" ,
73
+ format ! ( "try `pointer::cast_{constness}`, a safer alternative" ) ,
74
+ format ! ( "{}.cast_{constness}()" , sugg. maybe_par( ) ) ,
75
+ Applicability :: MachineApplicable ,
76
+ ) ;
77
+ }
44
78
}
45
79
}
0 commit comments