Skip to content

Commit 52b5150

Browse files
committed
Avoid ptrtoint when checking if a pointer is null
Casting the pointer to an integer requires a ptrtoint, while casting 0 to a pointer is directly folded to a `null` value.
1 parent dfc5c0f commit 52b5150

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/libcore/ptr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl<T> PtrExt for *const T {
303303

304304
#[inline]
305305
#[stable(feature = "rust1", since = "1.0.0")]
306-
fn is_null(self) -> bool { self as usize == 0 }
306+
fn is_null(self) -> bool { self == 0 as *const T }
307307

308308
#[inline]
309309
#[stable(feature = "rust1", since = "1.0.0")]
@@ -330,7 +330,7 @@ impl<T> PtrExt for *mut T {
330330

331331
#[inline]
332332
#[stable(feature = "rust1", since = "1.0.0")]
333-
fn is_null(self) -> bool { self as usize == 0 }
333+
fn is_null(self) -> bool { self == 0 as *mut T }
334334

335335
#[inline]
336336
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)