diff options
| author | bors <bors@rust-lang.org> | 2023-09-16 03:40:20 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-09-16 03:40:20 +0000 |
| commit | 635c4a5e612b0ee8af6615635599702d3dce9906 (patch) | |
| tree | e6e5f854126129a3d5ea0b07379053aed15a83b4 /library/core/src/ptr | |
| parent | e81f85fe9eebf8a5ba21438fb188a59d54095697 (diff) | |
| parent | 4b1bc2701067d7b340d102d40227091c00447f48 (diff) | |
| download | rust-635c4a5e612b0ee8af6615635599702d3dce9906.tar.gz rust-635c4a5e612b0ee8af6615635599702d3dce9906.zip | |
Auto merge of #114494 - est31:extend_useless_ptr_null_checks, r=jackh726
Make useless_ptr_null_checks smarter about some std functions This teaches the `useless_ptr_null_checks` lint that some std functions can't ever return null pointers, because they need to point to valid data, get references as input, etc. This is achieved by introducing an `#[rustc_never_returns_null_ptr]` attribute and adding it to these std functions (gated behind bootstrap `cfg_attr`). Later on, the attribute could maybe be used to tell LLVM that the returned pointer is never null. I don't expect much impact of that though, as the functions are pretty shallow and usually the input data is already never null. Follow-up of PR #113657 Fixes #114442
Diffstat (limited to 'library/core/src/ptr')
| -rw-r--r-- | library/core/src/ptr/mod.rs | 3 | ||||
| -rw-r--r-- | library/core/src/ptr/non_null.rs | 2 |
2 files changed, 4 insertions, 1 deletions
diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index 41e67fd8435..d1286a1dea7 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -698,6 +698,7 @@ where #[inline(always)] #[must_use] #[unstable(feature = "ptr_from_ref", issue = "106116")] +#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)] #[rustc_diagnostic_item = "ptr_from_ref"] pub const fn from_ref<T: ?Sized>(r: &T) -> *const T { r @@ -710,7 +711,7 @@ pub const fn from_ref<T: ?Sized>(r: &T) -> *const T { #[inline(always)] #[must_use] #[unstable(feature = "ptr_from_ref", issue = "106116")] -#[rustc_diagnostic_item = "ptr_from_mut"] +#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)] pub const fn from_mut<T: ?Sized>(r: &mut T) -> *mut T { r } diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index 6b3c4343ed3..d5bd54fd59a 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -338,6 +338,7 @@ impl<T: ?Sized> NonNull<T> { /// ``` #[stable(feature = "nonnull", since = "1.25.0")] #[rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0")] + #[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)] #[must_use] #[inline(always)] pub const fn as_ptr(self) -> *mut T { @@ -597,6 +598,7 @@ impl<T> NonNull<[T]> { #[must_use] #[unstable(feature = "slice_ptr_get", issue = "74265")] #[rustc_const_unstable(feature = "slice_ptr_get", issue = "74265")] + #[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)] pub const fn as_mut_ptr(self) -> *mut T { self.as_non_null_ptr().as_ptr() } |
