add test
This commit is contained in:
parent
389100921a
commit
c7bc41f97a
4 changed files with 135 additions and 0 deletions
|
@ -1,3 +1,5 @@
|
|||
// ignore-tidy-filelength
|
||||
|
||||
//! Lints in the Rust compiler.
|
||||
//!
|
||||
//! This contains lints which can feasibly be implemented as their own
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
|
||||
// pretty-expanded FIXME #23616
|
||||
|
||||
#![allow(deref_nullptr)]
|
||||
|
||||
|
||||
use std::env;
|
||||
|
||||
pub fn main() {
|
||||
|
|
32
src/test/ui/lint/lint-deref-nullptr.rs
Normal file
32
src/test/ui/lint/lint-deref-nullptr.rs
Normal file
|
@ -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::<i32>();
|
||||
//~^ ERROR Dereferencing a null pointer causes undefined behavior
|
||||
let ub = *core::ptr::null_mut::<i32>();
|
||||
//~^ ERROR Dereferencing a null pointer causes undefined behavior
|
||||
let ub = *(core::ptr::null::<i16>() as *const i32);
|
||||
//~^ ERROR Dereferencing a null pointer causes undefined behavior
|
||||
let ub = *(core::ptr::null::<i16>() as *mut i32 as *mut usize as *const u8);
|
||||
//~^ ERROR Dereferencing a null pointer causes undefined behavior
|
||||
let ub = &*core::ptr::null::<i32>();
|
||||
//~^ ERROR Dereferencing a null pointer causes undefined behavior
|
||||
core::ptr::addr_of!(*core::ptr::null::<i32>());
|
||||
//~^ ERROR Dereferencing a null pointer causes undefined behavior
|
||||
std::ptr::addr_of_mut!(*core::ptr::null_mut::<i32>());
|
||||
//~^ ERROR Dereferencing a null pointer causes undefined behavior
|
||||
let ub = *std::ptr::null::<i32>();
|
||||
//~^ ERROR Dereferencing a null pointer causes undefined behavior
|
||||
let ub = *std::ptr::null_mut::<i32>();
|
||||
//~^ ERROR Dereferencing a null pointer causes undefined behavior
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
98
src/test/ui/lint/lint-deref-nullptr.stderr
Normal file
98
src/test/ui/lint/lint-deref-nullptr.stderr
Normal file
|
@ -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::<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:13:18
|
||||
|
|
||||
LL | let ub = *core::ptr::null_mut::<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:15:18
|
||||
|
|
||||
LL | let ub = *(core::ptr::null::<i16>() 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::<i16>() 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::<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:21:29
|
||||
|
|
||||
LL | core::ptr::addr_of!(*core::ptr::null::<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: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
|
||||
|
||||
error: Dereferencing a null pointer causes undefined behavior
|
||||
--> $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
|
||||
|
||||
error: Dereferencing a null pointer causes undefined behavior
|
||||
--> $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
|
||||
|
||||
error: aborting due to 10 previous errors
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue