@@ -2,9 +2,10 @@ use clippy_config::msrvs::{self, Msrv};
22use clippy_utils:: diagnostics:: span_lint_and_sugg;
33use clippy_utils:: sugg:: Sugg ;
44use rustc_errors:: Applicability ;
5- use rustc_hir:: { Expr , Mutability } ;
5+ use rustc_hir:: { Expr , ExprKind , Mutability , QPath } ;
66use rustc_lint:: LateContext ;
77use rustc_middle:: ty:: { self , Ty , TypeVisitableExt } ;
8+ use rustc_span:: sym;
89
910use super :: PTR_CAST_CONSTNESS ;
1011
@@ -16,8 +17,7 @@ pub(super) fn check<'tcx>(
1617 cast_to : Ty < ' tcx > ,
1718 msrv : & Msrv ,
1819) {
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 ( )
2121 && let ty:: RawPtr ( to_ty, to_mutbl) = cast_to. kind ( )
2222 && matches ! (
2323 ( from_mutbl, to_mutbl) ,
@@ -26,20 +26,54 @@ pub(super) fn check<'tcx>(
2626 && from_ty == to_ty
2727 && !from_ty. has_erased_regions ( )
2828 {
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+ }
3460
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+ }
4478 }
4579}
0 commit comments