@@ -2657,8 +2657,8 @@ declare_lint! {
2657
2657
///
2658
2658
/// ### Explanation
2659
2659
///
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) .
2662
2662
///
2663
2663
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
2664
2664
pub DEREF_NULLPTR ,
@@ -2673,14 +2673,14 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr {
2673
2673
/// test if expression is a null ptr
2674
2674
fn is_null_ptr ( cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > ) -> bool {
2675
2675
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 {
2678
2678
return is_zero ( expr) || is_null_ptr ( cx, expr) ;
2679
2679
}
2680
2680
}
2681
2681
// 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 {
2684
2684
if let Some ( def_id) = cx. qpath_res ( qpath, path. hir_id ) . opt_def_id ( ) {
2685
2685
return matches ! (
2686
2686
cx. tcx. get_diagnostic_name( def_id) ,
@@ -2697,7 +2697,7 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr {
2697
2697
/// test if expression is the literal `0`
2698
2698
fn is_zero ( expr : & hir:: Expr < ' _ > ) -> bool {
2699
2699
match & expr. kind {
2700
- rustc_hir :: ExprKind :: Lit ( lit) => {
2700
+ hir :: ExprKind :: Lit ( lit) => {
2701
2701
if let LitKind :: Int ( a, _) = lit. node {
2702
2702
return a == 0 ;
2703
2703
}
@@ -2707,8 +2707,16 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr {
2707
2707
false
2708
2708
}
2709
2709
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 {
2712
2720
cx. emit_span_lint ( DEREF_NULLPTR , expr. span , BuiltinDerefNullptr {
2713
2721
label : expr. span ,
2714
2722
} ) ;
0 commit comments