@@ -2657,8 +2657,8 @@ declare_lint! {
26572657 ///
26582658 /// ### Explanation
26592659 ///
2660- /// Dereferencing a null pointer causes [undefined behavior] even as a place expression,
2661- /// like `&*(0 as *const i32)` or `addr_of!(*(0 as *const i32))` .
2660+ /// Dereferencing a null pointer causes [undefined behavior] if it is accessed
2661+ /// (loaded from or stored to) .
26622662 ///
26632663 /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
26642664 pub DEREF_NULLPTR ,
@@ -2673,14 +2673,14 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr {
26732673 /// test if expression is a null ptr
26742674 fn is_null_ptr ( cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > ) -> bool {
26752675 match & expr. kind {
2676- rustc_hir :: ExprKind :: Cast ( expr, ty) => {
2677- if let rustc_hir :: TyKind :: Ptr ( _) = ty. kind {
2676+ hir :: ExprKind :: Cast ( expr, ty) => {
2677+ if let hir :: TyKind :: Ptr ( _) = ty. kind {
26782678 return is_zero ( expr) || is_null_ptr ( cx, expr) ;
26792679 }
26802680 }
26812681 // check for call to `core::ptr::null` or `core::ptr::null_mut`
2682- rustc_hir :: ExprKind :: Call ( path, _) => {
2683- if let rustc_hir :: ExprKind :: Path ( ref qpath) = path. kind {
2682+ hir :: ExprKind :: Call ( path, _) => {
2683+ if let hir :: ExprKind :: Path ( ref qpath) = path. kind {
26842684 if let Some ( def_id) = cx. qpath_res ( qpath, path. hir_id ) . opt_def_id ( ) {
26852685 return matches ! (
26862686 cx. tcx. get_diagnostic_name( def_id) ,
@@ -2697,7 +2697,7 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr {
26972697 /// test if expression is the literal `0`
26982698 fn is_zero ( expr : & hir:: Expr < ' _ > ) -> bool {
26992699 match & expr. kind {
2700- rustc_hir :: ExprKind :: Lit ( lit) => {
2700+ hir :: ExprKind :: Lit ( lit) => {
27012701 if let LitKind :: Int ( a, _) = lit. node {
27022702 return a == 0 ;
27032703 }
@@ -2707,8 +2707,16 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr {
27072707 false
27082708 }
27092709
2710- if let rustc_hir:: ExprKind :: Unary ( rustc_hir:: UnOp :: Deref , expr_deref) = expr. kind {
2711- if is_null_ptr ( cx, expr_deref) {
2710+ if let hir:: ExprKind :: Unary ( hir:: UnOp :: Deref , expr_deref) = expr. kind
2711+ && is_null_ptr ( cx, expr_deref)
2712+ {
2713+ if let hir:: Node :: Expr ( hir:: Expr {
2714+ kind : hir:: ExprKind :: AddrOf ( hir:: BorrowKind :: Raw , ..) ,
2715+ ..
2716+ } ) = cx. tcx . parent_hir_node ( expr. hir_id )
2717+ {
2718+ // `&raw *NULL` is ok.
2719+ } else {
27122720 cx. emit_span_lint ( DEREF_NULLPTR , expr. span , BuiltinDerefNullptr {
27132721 label : expr. span ,
27142722 } ) ;
0 commit comments