diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index ab82070ce73..cb81c330b71 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1,3 +1,5 @@ +// ignore-tidy-filelength + //! Lints in the Rust compiler. //! //! This contains lints which can feasibly be implemented as their own diff --git a/src/test/ui/cleanup-shortcircuit.rs b/src/test/ui/cleanup-shortcircuit.rs index 4f5197a5ba9..fe867ce1fbd 100644 --- a/src/test/ui/cleanup-shortcircuit.rs +++ b/src/test/ui/cleanup-shortcircuit.rs @@ -3,6 +3,9 @@ // pretty-expanded FIXME #23616 +#![allow(deref_nullptr)] + + use std::env; pub fn main() { diff --git a/src/test/ui/lint/lint-deref-nullptr.rs b/src/test/ui/lint/lint-deref-nullptr.rs new file mode 100644 index 00000000000..7b10e711c27 --- /dev/null +++ b/src/test/ui/lint/lint-deref-nullptr.rs @@ -0,0 +1,32 @@ +// test the deref_nullptr lint + +#![deny(deref_nullptr)] + +fn f() { + unsafe { + let a = 1; + let ub = *(a as *const i32); + let ub = *(0 as *const i32); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + let ub = *core::ptr::null::(); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + let ub = *core::ptr::null_mut::(); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + let ub = *(core::ptr::null::() as *const i32); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + let ub = *(core::ptr::null::() as *mut i32 as *mut usize as *const u8); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + let ub = &*core::ptr::null::(); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + core::ptr::addr_of!(*core::ptr::null::()); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + std::ptr::addr_of_mut!(*core::ptr::null_mut::()); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + let ub = *std::ptr::null::(); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + let ub = *std::ptr::null_mut::(); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + } +} + +fn main() {} diff --git a/src/test/ui/lint/lint-deref-nullptr.stderr b/src/test/ui/lint/lint-deref-nullptr.stderr new file mode 100644 index 00000000000..4fc6c54e197 --- /dev/null +++ b/src/test/ui/lint/lint-deref-nullptr.stderr @@ -0,0 +1,98 @@ +error: Dereferencing a null pointer causes undefined behavior + --> $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 + | +note: the lint level is defined here + --> $DIR/lint-deref-nullptr.rs:3:9 + | +LL | #![deny(deref_nullptr)] + | ^^^^^^^^^^^^^ + +error: Dereferencing a null pointer causes undefined behavior + --> $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 + +error: Dereferencing a null pointer causes undefined behavior + --> $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 + +error: Dereferencing a null pointer causes undefined behavior + --> $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 + +error: Dereferencing a null pointer causes undefined behavior + --> $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 + +error: Dereferencing a null pointer causes undefined behavior + --> $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 + +error: Dereferencing a null pointer causes undefined behavior + --> $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 + +error: Dereferencing a null pointer causes undefined behavior + --> $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 + +error: Dereferencing a null pointer causes undefined behavior + --> $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 + +error: Dereferencing a null pointer causes undefined behavior + --> $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 + +error: aborting due to 10 previous errors +