diff options
| author | AliƩnore Bouttefeux <alienore.bouttefeux@gmail.com> | 2021-04-09 16:13:04 +0200 |
|---|---|---|
| committer | AliƩnore Bouttefeux <alienore.bouttefeux@gmail.com> | 2021-04-09 16:13:04 +0200 |
| commit | 79666c8857870ed8c16de932781ad50624e1accb (patch) | |
| tree | 3433c9857d1da75d806ee664c01062ff9024bc1e | |
| parent | 0531ed0b6220257d7c79405ca7d81d9fe66976be (diff) | |
| download | rust-79666c8857870ed8c16de932781ad50624e1accb.tar.gz rust-79666c8857870ed8c16de932781ad50624e1accb.zip | |
changes based on review
| -rw-r--r-- | compiler/rustc_lint/src/builtin.rs | 10 | ||||
| -rw-r--r-- | src/test/ui/lint/lint-deref-nullptr.rs | 20 | ||||
| -rw-r--r-- | src/test/ui/lint/lint-deref-nullptr.stderr | 70 |
3 files changed, 34 insertions, 66 deletions
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::<i32>(); - /// let x = core::ptr::addr_of!(*std::ptr::null::<i32>()); - /// let x = *core::ptr::null::<i32>(); + /// let x = &*ptr::null::<i32>(); + /// let x = ptr::addr_of!(*ptr::null::<i32>()); /// 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::<i32>(); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = *core::ptr::null_mut::<i32>(); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = *(core::ptr::null::<i16>() as *const i32); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = *(core::ptr::null::<i16>() 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::<i32>(); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer core::ptr::addr_of!(*core::ptr::null::<i32>()); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer std::ptr::addr_of_mut!(*core::ptr::null_mut::<i32>()); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = *std::ptr::null::<i32>(); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = *std::ptr::null_mut::<i32>(); - //~^ 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::<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:13:18 | LL | let ub = *core::ptr::null_mut::<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:15:18 | LL | let ub = *(core::ptr::null::<i16>() 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::<i16>() 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::<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:21:29 | LL | core::ptr::addr_of!(*core::ptr::null::<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:23:32 | LL | std::ptr::addr_of_mut!(*core::ptr::null_mut::<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:25:18 | LL | let ub = *std::ptr::null::<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:27:18 | LL | let ub = *std::ptr::null_mut::<i32>(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | 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 |
