rust/library/core/src/ptr
bors 7537b20626 Auto merge of #83948 - ABouttefeux:lint-nullprt-deref, r=RalfJung
add lint deref_nullptr detecting when a null ptr is dereferenced

fixes #83856
changelog: add lint that detect code like
```rust
unsafe {
      &*core::ptr::null::<i32>()
 };
unsafe {
     addr_of!(std::ptr::null::<i32>())
};
let x: i32 = unsafe {*core::ptr::null()};
let x: i32 = unsafe {*core::ptr::null_mut()};
unsafe {*(0 as *const i32)};
unsafe {*(core::ptr::null() as *const i32)};
```
```
warning: Dereferencing a null pointer causes undefined behavior
 --> src\main.rs:5:26
  |
5 |     let x: i32 = unsafe {*core::ptr::null()};
  |                          ^^^^^^^^^^^^^^^^^^
  |                          |
  |                          a null pointer is dereferenced
  |                          this code causes undefined behavior when executed
  |
  = note: `#[warn(deref_nullptr)]` on by default
```

Limitation:
It does not detect code like
```rust
const ZERO: usize = 0;
unsafe {*(ZERO as *const i32)};
```
or code where `0` is not directly a literal
2021-04-14 18:04:22 +00:00
..
const_ptr.rs fix Miri errors in libcore doctests 2021-04-10 11:58:48 +02:00
metadata.rs Pointer metadata: add tracking issue number 2021-02-15 14:27:51 +01:00
mod.rs add lint deref_nullptr 2021-04-06 22:01:00 +02:00
mut_ptr.rs fix Miri errors in libcore doctests 2021-04-10 11:58:48 +02:00
non_null.rs Bump cfgs 2021-04-04 14:57:05 -04:00
unique.rs Remove a stray ignore-tidy-undocumented-unsafe 2020-09-08 15:00:47 -05:00