diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 905a808f51f..f19572550eb 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -2972,10 +2972,10 @@ declare_lint! { /// /// ```rust,no_run /// # #![allow(unused)] + /// use std::ptr; /// unsafe { - /// let x = &*core::ptr::null::(); - /// let x = core::ptr::addr_of!(*std::ptr::null::()); - /// let x = *core::ptr::null::(); + /// let x = &*ptr::null::(); + /// let x = ptr::addr_of!(*ptr::null::()); /// let x = *(0 as *const i32); /// } /// ``` @@ -3036,9 +3036,7 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr { if let rustc_hir::UnOp::Deref = un_op { if is_null_ptr(cx, expr_deref) { cx.struct_span_lint(DEREF_NULLPTR, expr.span, |lint| { - let mut err = - lint.build("Dereferencing a null pointer causes undefined behavior"); - err.span_label(expr.span, "a null pointer is dereferenced"); + let mut err = lint.build("dereferencing a null pointer"); err.span_label( expr.span, "this code causes undefined behavior when executed", diff --git a/src/test/ui/lint/lint-deref-nullptr.rs b/src/test/ui/lint/lint-deref-nullptr.rs index 7b10e711c27..a5aee735140 100644 --- a/src/test/ui/lint/lint-deref-nullptr.rs +++ b/src/test/ui/lint/lint-deref-nullptr.rs @@ -7,25 +7,25 @@ fn f() { let a = 1; let ub = *(a as *const i32); let ub = *(0 as *const i32); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = *core::ptr::null::(); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = *core::ptr::null_mut::(); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = *(core::ptr::null::() as *const i32); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = *(core::ptr::null::() as *mut i32 as *mut usize as *const u8); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = &*core::ptr::null::(); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer core::ptr::addr_of!(*core::ptr::null::()); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer std::ptr::addr_of_mut!(*core::ptr::null_mut::()); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = *std::ptr::null::(); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = *std::ptr::null_mut::(); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer } } diff --git a/src/test/ui/lint/lint-deref-nullptr.stderr b/src/test/ui/lint/lint-deref-nullptr.stderr index 4fc6c54e197..ba27d2c45fc 100644 --- a/src/test/ui/lint/lint-deref-nullptr.stderr +++ b/src/test/ui/lint/lint-deref-nullptr.stderr @@ -1,11 +1,8 @@ -error: Dereferencing a null pointer causes undefined behavior +error: dereferencing a null pointer --> $DIR/lint-deref-nullptr.rs:9:18 | LL | let ub = *(0 as *const i32); - | ^^^^^^^^^^^^^^^^^^ - | | - | a null pointer is dereferenced - | this code causes undefined behavior when executed + | ^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed | note: the lint level is defined here --> $DIR/lint-deref-nullptr.rs:3:9 @@ -13,86 +10,59 @@ note: the lint level is defined here LL | #![deny(deref_nullptr)] | ^^^^^^^^^^^^^ -error: Dereferencing a null pointer causes undefined behavior +error: dereferencing a null pointer --> $DIR/lint-deref-nullptr.rs:11:18 | LL | let ub = *core::ptr::null::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | a null pointer is dereferenced - | this code causes undefined behavior when executed + | ^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed -error: Dereferencing a null pointer causes undefined behavior +error: dereferencing a null pointer --> $DIR/lint-deref-nullptr.rs:13:18 | LL | let ub = *core::ptr::null_mut::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | a null pointer is dereferenced - | this code causes undefined behavior when executed + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed -error: Dereferencing a null pointer causes undefined behavior +error: dereferencing a null pointer --> $DIR/lint-deref-nullptr.rs:15:18 | LL | let ub = *(core::ptr::null::() as *const i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | a null pointer is dereferenced - | this code causes undefined behavior when executed + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed -error: Dereferencing a null pointer causes undefined behavior +error: dereferencing a null pointer --> $DIR/lint-deref-nullptr.rs:17:18 | LL | let ub = *(core::ptr::null::() as *mut i32 as *mut usize as *const u8); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | a null pointer is dereferenced - | this code causes undefined behavior when executed + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed -error: Dereferencing a null pointer causes undefined behavior +error: dereferencing a null pointer --> $DIR/lint-deref-nullptr.rs:19:19 | LL | let ub = &*core::ptr::null::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | a null pointer is dereferenced - | this code causes undefined behavior when executed + | ^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed -error: Dereferencing a null pointer causes undefined behavior +error: dereferencing a null pointer --> $DIR/lint-deref-nullptr.rs:21:29 | LL | core::ptr::addr_of!(*core::ptr::null::()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | a null pointer is dereferenced - | this code causes undefined behavior when executed + | ^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed -error: Dereferencing a null pointer causes undefined behavior +error: dereferencing a null pointer --> $DIR/lint-deref-nullptr.rs:23:32 | LL | std::ptr::addr_of_mut!(*core::ptr::null_mut::()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | a null pointer is dereferenced - | this code causes undefined behavior when executed + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed -error: Dereferencing a null pointer causes undefined behavior +error: dereferencing a null pointer --> $DIR/lint-deref-nullptr.rs:25:18 | LL | let ub = *std::ptr::null::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | a null pointer is dereferenced - | this code causes undefined behavior when executed + | ^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed -error: Dereferencing a null pointer causes undefined behavior +error: dereferencing a null pointer --> $DIR/lint-deref-nullptr.rs:27:18 | LL | let ub = *std::ptr::null_mut::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | a null pointer is dereferenced - | this code causes undefined behavior when executed + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed error: aborting due to 10 previous errors