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.1 KiB
Rust
Raw Normal View History

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