Skip to content

Commit a6a8f6c

Browse files
committed
Auto merge of #61864 - lzutao:ptr-null, r=sfackler
Make use of `ptr::null(_mut)` instead of casting zero There are few places that I don't replace the zero casting pointer with `ptr::null` or `ptr::null_mut`: ```bash % git grep -E '[ ([{]0 as \*' src/libcore/ptr/mod.rs:216:pub const fn null<T>() -> *const T { 0 as *const T } src/libcore/ptr/mod.rs:231:pub const fn null_mut<T>() -> *mut T { 0 as *mut T } src/test/run-pass/consts/const-cast-ptr-int.rs:12:static a: TestStruct = TestStruct{x: 0 as *const u8}; src/test/ui/issues/issue-45730.rs:5: let x: *const _ = 0 as *const _; //~ ERROR cannot cast src/test/ui/issues/issue-45730.rs:8: let x = 0 as *const i32 as *const _ as *mut _; //~ ERROR cannot cast src/test/ui/issues/issue-45730.stderr:14:LL | let x: *const _ = 0 as *const _; src/test/ui/issues/issue-45730.stderr:24:LL | let x = 0 as *const i32 as *const _ as *mut _; src/test/ui/lint/lint-forbid-internal-unsafe.rs:15: println!("{}", evil!(*(0 as *const u8))); src/test/ui/order-dependent-cast-inference.rs:5: let mut y = 0 as *const _; src/test/ui/order-dependent-cast-inference.stderr:4:LL | let mut y = 0 as *const _; ``` r? @sfackler
2 parents 704ab2b + 7d69d4c commit a6a8f6c

File tree

33 files changed

+55
-52
lines changed

33 files changed

+55
-52
lines changed

src/bootstrap/job.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use std::env;
3333
use std::io;
3434
use std::mem;
35+
use std::ptr;
3536
use crate::Build;
3637

3738
type HANDLE = *mut u8;
@@ -118,8 +119,8 @@ pub unsafe fn setup(build: &mut Build) {
118119
SetErrorMode(mode & !SEM_NOGPFAULTERRORBOX);
119120

120121
// Create a new job object for us to use
121-
let job = CreateJobObjectW(0 as *mut _, 0 as *const _);
122-
assert!(job != 0 as *mut _, "{}", io::Error::last_os_error());
122+
let job = CreateJobObjectW(ptr::null_mut(), ptr::null());
123+
assert!(!job.is_null(), "{}", io::Error::last_os_error());
123124

124125
// Indicate that when all handles to the job object are gone that all
125126
// process in the object should be killed. Note that this includes our
@@ -166,8 +167,8 @@ pub unsafe fn setup(build: &mut Build) {
166167
};
167168

168169
let parent = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid.parse().unwrap());
169-
assert!(parent != 0 as *mut _, "{}", io::Error::last_os_error());
170-
let mut parent_handle = 0 as *mut _;
170+
assert!(!parent.is_null(), "{}", io::Error::last_os_error());
171+
let mut parent_handle = ptr::null_mut();
171172
let r = DuplicateHandle(GetCurrentProcess(), job,
172173
parent, &mut parent_handle,
173174
0, FALSE, DUPLICATE_SAME_ACCESS);

src/bootstrap/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub fn symlink_dir(config: &Config, src: &Path, dest: &Path) -> io::Result<()> {
209209
let h = CreateFileW(path.as_ptr(),
210210
GENERIC_WRITE,
211211
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
212-
0 as *mut _,
212+
ptr::null_mut(),
213213
OPEN_EXISTING,
214214
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
215215
ptr::null_mut());

src/libarena/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ impl<T> Default for TypedArena<T> {
114114
TypedArena {
115115
// We set both `ptr` and `end` to 0 so that the first call to
116116
// alloc() will trigger a grow().
117-
ptr: Cell::new(0 as *mut T),
118-
end: Cell::new(0 as *mut T),
117+
ptr: Cell::new(ptr::null_mut()),
118+
end: Cell::new(ptr::null_mut()),
119119
chunks: RefCell::new(vec![]),
120120
_own: PhantomData,
121121
}
@@ -370,8 +370,8 @@ impl Default for DroplessArena {
370370
#[inline]
371371
fn default() -> DroplessArena {
372372
DroplessArena {
373-
ptr: Cell::new(0 as *mut u8),
374-
end: Cell::new(0 as *mut u8),
373+
ptr: Cell::new(ptr::null_mut()),
374+
end: Cell::new(ptr::null_mut()),
375375
chunks: Default::default(),
376376
}
377377
}

src/libpanic_unwind/dummy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::any::Any;
77
use core::intrinsics;
88

99
pub fn payload() -> *mut u8 {
10-
0 as *mut u8
10+
core::ptr::null_mut()
1111
}
1212

1313
pub unsafe fn cleanup(_ptr: *mut u8) -> Box<dyn Any + Send> {

src/libpanic_unwind/seh.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ mod imp {
104104
pub const NAME2: [u8; 7] = [b'.', b'P', b'A', b'X', 0, 0, 0];
105105

106106
macro_rules! ptr {
107-
(0) => (0 as *mut u8);
107+
(0) => (core::ptr::null_mut());
108108
($e:expr) => ($e as *mut u8);
109109
}
110110
}
@@ -223,13 +223,13 @@ extern "C" {
223223
#[cfg_attr(not(test), lang = "msvc_try_filter")]
224224
static mut TYPE_DESCRIPTOR1: _TypeDescriptor = _TypeDescriptor {
225225
pVFTable: unsafe { &TYPE_INFO_VTABLE } as *const _ as *const _,
226-
spare: 0 as *mut _,
226+
spare: core::ptr::null_mut(),
227227
name: imp::NAME1,
228228
};
229229

230230
static mut TYPE_DESCRIPTOR2: _TypeDescriptor = _TypeDescriptor {
231231
pVFTable: unsafe { &TYPE_INFO_VTABLE } as *const _ as *const _,
232-
spare: 0 as *mut _,
232+
spare: core::ptr::null_mut(),
233233
name: imp::NAME2,
234234
};
235235

src/librustc_errors/lock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub fn acquire_global_lock(name: &str) -> Box<dyn Any> {
6464
//
6565
// This will silently create one if it doesn't already exist, or it'll
6666
// open up a handle to one if it already exists.
67-
let mutex = CreateMutexA(0 as *mut _, 0, cname.as_ptr() as *const u8);
67+
let mutex = CreateMutexA(std::ptr::null_mut(), 0, cname.as_ptr() as *const u8);
6868
if mutex.is_null() {
6969
panic!("failed to create global mutex named `{}`: {}",
7070
name,

src/librustc_metadata/dynamic_lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ mod dl {
115115
{
116116
use std::sync::{Mutex, Once};
117117
static INIT: Once = Once::new();
118-
static mut LOCK: *mut Mutex<()> = 0 as *mut _;
118+
static mut LOCK: *mut Mutex<()> = ptr::null_mut();
119119
unsafe {
120120
INIT.call_once(|| {
121121
LOCK = Box::into_raw(Box::new(Mutex::new(())));

src/librustc_typeck/error_codes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3904,7 +3904,7 @@ x as Vec<u8>; // error: non-primitive cast: `u8` as `std::vec::Vec<u8>`
39043904
39053905
// Another example
39063906
3907-
let v = 0 as *const u8; // So here, `v` is a `*const u8`.
3907+
let v = core::ptr::null::<u8>(); // So here, `v` is a `*const u8`.
39083908
v as &u8; // error: non-primitive cast: `*const u8` as `&u8`
39093909
```
39103910
@@ -3914,7 +3914,7 @@ Only primitive types can be cast into each other. Examples:
39143914
let x = 0u8;
39153915
x as u32; // ok!
39163916
3917-
let v = 0 as *const u8;
3917+
let v = core::ptr::null::<u8>();
39183918
v as *const i8; // ok!
39193919
```
39203920
@@ -3954,7 +3954,7 @@ A cast between a thin and a fat pointer was attempted.
39543954
Erroneous code example:
39553955
39563956
```compile_fail,E0607
3957-
let v = 0 as *const u8;
3957+
let v = core::ptr::null::<u8>();
39583958
v as *const [u8];
39593959
```
39603960

src/libstd/sys/wasi/args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn maybe_args() -> io::Result<Args> {
3232
let (mut argc, mut argv_buf_size) = (0, 0);
3333
cvt_wasi(libc::__wasi_args_sizes_get(&mut argc, &mut argv_buf_size))?;
3434

35-
let mut argc = vec![0 as *mut libc::c_char; argc];
35+
let mut argc = vec![core::ptr::null_mut::<libc::c_char>(); argc];
3636
let mut argv_buf = vec![0; argv_buf_size];
3737
cvt_wasi(libc::__wasi_args_get(argc.as_mut_ptr(), argv_buf.as_mut_ptr()))?;
3838

src/libstd/sys/wasm/thread_local_atomics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct ThreadControlBlock {
1111
impl ThreadControlBlock {
1212
fn new() -> ThreadControlBlock {
1313
ThreadControlBlock {
14-
keys: [0 as *mut u8; MAX_KEYS],
14+
keys: [core::ptr::null_mut(); MAX_KEYS],
1515
}
1616
}
1717

0 commit comments

Comments
 (0)