@@ -2679,8 +2679,8 @@ declare_lint! {
2679
2679
///
2680
2680
/// ### Explanation
2681
2681
///
2682
- /// Dereferencing a null pointer causes [undefined behavior] even as a place expression,
2683
- /// like `&*(0 as *const i32)` or `addr_of!(*(0 as *const i32))` .
2682
+ /// Dereferencing a null pointer causes [undefined behavior] if it is accessed
2683
+ /// (loaded from or stored to) .
2684
2684
///
2685
2685
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
2686
2686
pub DEREF_NULLPTR ,
@@ -2695,14 +2695,14 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr {
2695
2695
/// test if expression is a null ptr
2696
2696
fn is_null_ptr ( cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > ) -> bool {
2697
2697
match & expr. kind {
2698
- rustc_hir :: ExprKind :: Cast ( expr, ty) => {
2699
- if let rustc_hir :: TyKind :: Ptr ( _) = ty. kind {
2698
+ hir :: ExprKind :: Cast ( expr, ty) => {
2699
+ if let hir :: TyKind :: Ptr ( _) = ty. kind {
2700
2700
return is_zero ( expr) || is_null_ptr ( cx, expr) ;
2701
2701
}
2702
2702
}
2703
2703
// check for call to `core::ptr::null` or `core::ptr::null_mut`
2704
- rustc_hir :: ExprKind :: Call ( path, _) => {
2705
- if let rustc_hir :: ExprKind :: Path ( ref qpath) = path. kind {
2704
+ hir :: ExprKind :: Call ( path, _) => {
2705
+ if let hir :: ExprKind :: Path ( ref qpath) = path. kind {
2706
2706
if let Some ( def_id) = cx. qpath_res ( qpath, path. hir_id ) . opt_def_id ( ) {
2707
2707
return matches ! (
2708
2708
cx. tcx. get_diagnostic_name( def_id) ,
@@ -2719,7 +2719,7 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr {
2719
2719
/// test if expression is the literal `0`
2720
2720
fn is_zero ( expr : & hir:: Expr < ' _ > ) -> bool {
2721
2721
match & expr. kind {
2722
- rustc_hir :: ExprKind :: Lit ( lit) => {
2722
+ hir :: ExprKind :: Lit ( lit) => {
2723
2723
if let LitKind :: Int ( a, _) = lit. node {
2724
2724
return a == 0 ;
2725
2725
}
@@ -2729,8 +2729,16 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr {
2729
2729
false
2730
2730
}
2731
2731
2732
- if let rustc_hir:: ExprKind :: Unary ( rustc_hir:: UnOp :: Deref , expr_deref) = expr. kind {
2733
- if is_null_ptr ( cx, expr_deref) {
2732
+ if let hir:: ExprKind :: Unary ( hir:: UnOp :: Deref , expr_deref) = expr. kind
2733
+ && is_null_ptr ( cx, expr_deref)
2734
+ {
2735
+ if let hir:: Node :: Expr ( hir:: Expr {
2736
+ kind : hir:: ExprKind :: AddrOf ( hir:: BorrowKind :: Raw , ..) ,
2737
+ ..
2738
+ } ) = cx. tcx . parent_hir_node ( expr. hir_id )
2739
+ {
2740
+ // `&raw *NULL` is ok.
2741
+ } else {
2734
2742
cx. emit_span_lint (
2735
2743
DEREF_NULLPTR ,
2736
2744
expr. span ,
0 commit comments