rust/tests/ui/lint/lint-deref-nullptr.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

39 lines
1.2 KiB
Rust
Raw Normal View History

2021-04-07 17:23:17 +00:00
// test the deref_nullptr lint
#![deny(deref_nullptr)]
2021-04-10 13:40:07 +00:00
use std::ptr;
struct Struct {
field: u8,
}
2021-04-07 17:23:17 +00:00
fn f() {
unsafe {
let a = 1;
let ub = *(a as *const i32);
let ub = *(0 as *const i32);
2021-04-09 14:13:04 +00:00
//~^ ERROR dereferencing a null pointer
2021-04-10 13:40:07 +00:00
let ub = *ptr::null::<i32>();
//~^ ERROR dereferencing a null pointer
let ub = *ptr::null_mut::<i32>();
2021-04-09 14:13:04 +00:00
//~^ ERROR dereferencing a null pointer
2021-04-10 13:40:07 +00:00
let ub = *(ptr::null::<i16>() as *const i32);
2021-04-09 14:13:04 +00:00
//~^ ERROR dereferencing a null pointer
2021-04-10 13:40:07 +00:00
let ub = *(ptr::null::<i16>() as *mut i32 as *mut usize as *const u8);
2021-04-09 14:13:04 +00:00
//~^ ERROR dereferencing a null pointer
2021-04-10 13:40:07 +00:00
let ub = &*ptr::null::<i32>();
2021-04-09 14:13:04 +00:00
//~^ ERROR dereferencing a null pointer
2021-04-10 14:17:09 +00:00
let ub = &*ptr::null_mut::<i32>();
//~^ ERROR dereferencing a null pointer
2021-04-10 13:40:07 +00:00
ptr::addr_of!(*ptr::null::<i32>());
2021-04-09 14:13:04 +00:00
//~^ ERROR dereferencing a null pointer
2021-04-10 13:40:07 +00:00
ptr::addr_of_mut!(*ptr::null_mut::<i32>());
2021-04-09 14:13:04 +00:00
//~^ ERROR dereferencing a null pointer
2021-04-10 13:40:07 +00:00
let offset = ptr::addr_of!((*ptr::null::<Struct>()).field);
2021-04-09 14:13:04 +00:00
//~^ ERROR dereferencing a null pointer
2021-04-07 17:23:17 +00:00
}
}
fn main() {}