]> git.lizzy.rs Git - rust.git/blob - tests/ui/lint/lint-deref-nullptr.rs
Rollup merge of #106766 - GuillaumeGomez:rm-stripper-dead-code, r=notriddle
[rust.git] / tests / ui / lint / lint-deref-nullptr.rs
1 // test the deref_nullptr lint
2
3 #![deny(deref_nullptr)]
4
5 use std::ptr;
6
7 struct Struct {
8     field: u8,
9 }
10
11 fn f() {
12     unsafe {
13         let a = 1;
14         let ub = *(a as *const i32);
15         let ub = *(0 as *const i32);
16         //~^ ERROR dereferencing a null pointer
17         let ub = *ptr::null::<i32>();
18         //~^ ERROR dereferencing a null pointer
19         let ub = *ptr::null_mut::<i32>();
20         //~^ ERROR dereferencing a null pointer
21         let ub = *(ptr::null::<i16>() as *const i32);
22         //~^ ERROR dereferencing a null pointer
23         let ub = *(ptr::null::<i16>() as *mut i32 as *mut usize as *const u8);
24         //~^ ERROR dereferencing a null pointer
25         let ub = &*ptr::null::<i32>();
26         //~^ ERROR dereferencing a null pointer
27         let ub = &*ptr::null_mut::<i32>();
28         //~^ ERROR dereferencing a null pointer
29         ptr::addr_of!(*ptr::null::<i32>());
30         //~^ ERROR dereferencing a null pointer
31         ptr::addr_of_mut!(*ptr::null_mut::<i32>());
32         //~^ ERROR dereferencing a null pointer
33         let offset = ptr::addr_of!((*ptr::null::<Struct>()).field);
34         //~^ ERROR dereferencing a null pointer
35     }
36 }
37
38 fn main() {}