Skip to content

Commit eefec8a

Browse files
committed
library: Normalize safety-for-unsafe-block comments
Almost all safety comments are of the form `// SAFETY:`, so normalize the rest and fix a few of them that should have been a `/// # Safety` section instead. Furthermore, make `tidy` only allow the uppercase form. While currently `tidy` only checks `core`, it is a good idea to prevent `core` from drifting to non-uppercase comments, so that later we can start checking `alloc` etc. too. Signed-off-by: Miguel Ojeda <[email protected]>
1 parent fe1bf8e commit eefec8a

File tree

7 files changed

+26
-19
lines changed

7 files changed

+26
-19
lines changed

library/alloc/src/collections/btree/map/entry.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,14 @@ impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
278278
pub fn insert(self, value: V) -> &'a mut V {
279279
let out_ptr = match self.handle.insert_recursing(self.key, value) {
280280
(Fit(_), val_ptr) => {
281-
// Safety: We have consumed self.handle and the handle returned.
281+
// SAFETY: We have consumed self.handle and the handle returned.
282282
let map = unsafe { self.dormant_map.awaken() };
283283
map.length += 1;
284284
val_ptr
285285
}
286286
(Split(ins), val_ptr) => {
287287
drop(ins.left);
288-
// Safety: We have consumed self.handle and the reference returned.
288+
// SAFETY: We have consumed self.handle and the reference returned.
289289
let map = unsafe { self.dormant_map.awaken() };
290290
let root = map.root.as_mut().unwrap();
291291
root.push_internal_level().push(ins.kv.0, ins.kv.1, ins.right);

library/alloc/src/vec/mod.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -1938,13 +1938,13 @@ impl<T, A: Allocator> Vec<T, A> {
19381938
pub fn split_at_spare_mut(&mut self) -> (&mut [T], &mut [MaybeUninit<T>]) {
19391939
let ptr = self.as_mut_ptr();
19401940

1941-
// Safety:
1941+
// SAFETY:
19421942
// - `ptr` is guaranteed to be in bounds for `capacity` elements
19431943
// - `len` is guaranteed to less or equal to `capacity`
19441944
// - `MaybeUninit<T>` has the same layout as `T`
19451945
let spare_ptr = unsafe { ptr.cast::<MaybeUninit<T>>().add(self.len) };
19461946

1947-
// Safety:
1947+
// SAFETY:
19481948
// - `ptr` is guaranteed to be valid for `len` elements
19491949
// - `spare_ptr` is offseted from `ptr` by `len`, so it doesn't overlap `initialized` slice
19501950
unsafe {
@@ -2154,7 +2154,8 @@ pub fn from_elem_in<T: Clone, A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<
21542154
}
21552155

21562156
trait ExtendFromWithinSpec {
2157-
/// Safety:
2157+
/// # Safety
2158+
///
21582159
/// - `src` needs to be valid index
21592160
/// - `self.capacity() - self.len()` must be `>= src.len()`
21602161
unsafe fn spec_extend_from_within(&mut self, src: Range<usize>);
@@ -2165,14 +2166,14 @@ impl<T: Clone, A: Allocator> ExtendFromWithinSpec for Vec<T, A> {
21652166
let initialized = {
21662167
let (this, spare) = self.split_at_spare_mut();
21672168

2168-
// Safety:
2169+
// SAFETY:
21692170
// - caller guaratees that src is a valid index
21702171
let to_clone = unsafe { this.get_unchecked(src) };
21712172

21722173
to_clone.iter().cloned().zip(spare.iter_mut()).map(|(e, s)| s.write(e)).count()
21732174
};
21742175

2175-
// Safety:
2176+
// SAFETY:
21762177
// - elements were just initialized
21772178
unsafe {
21782179
let new_len = self.len() + initialized;
@@ -2187,11 +2188,11 @@ impl<T: Copy, A: Allocator> ExtendFromWithinSpec for Vec<T, A> {
21872188
{
21882189
let (init, spare) = self.split_at_spare_mut();
21892190

2190-
// Safety:
2191+
// SAFETY:
21912192
// - caller guaratees that `src` is a valid index
21922193
let source = unsafe { init.get_unchecked(src) };
21932194

2194-
// Safety:
2195+
// SAFETY:
21952196
// - Both pointers are created from unique slice references (`&mut [_]`)
21962197
// so they are valid and do not overlap.
21972198
// - Elements are :Copy so it's OK to to copy them, without doing
@@ -2203,7 +2204,7 @@ impl<T: Copy, A: Allocator> ExtendFromWithinSpec for Vec<T, A> {
22032204
unsafe { ptr::copy_nonoverlapping(source.as_ptr(), spare.as_mut_ptr() as _, count) };
22042205
}
22052206

2206-
// Safety:
2207+
// SAFETY:
22072208
// - The elements were just initialized by `copy_nonoverlapping`
22082209
self.len += count;
22092210
}

library/std/src/io/copy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<I: Write> BufferedCopySpec for BufWriter<I> {
106106
Ok(0) => return Ok(len), // EOF reached
107107
Ok(bytes_read) => {
108108
assert!(bytes_read <= spare_cap.len());
109-
// Safety: The initializer contract guarantees that either it or `read`
109+
// SAFETY: The initializer contract guarantees that either it or `read`
110110
// will have initialized these bytes. And we just checked that the number
111111
// of bytes is within the buffer capacity.
112112
unsafe { buf.set_len(buf.len() + bytes_read) };

library/std/src/lazy.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,17 @@ impl<T> SyncOnceCell<T> {
440440
res
441441
}
442442

443-
/// Safety: The value must be initialized
443+
/// # Safety
444+
///
445+
/// The value must be initialized
444446
unsafe fn get_unchecked(&self) -> &T {
445447
debug_assert!(self.is_initialized());
446448
(&*self.value.get()).assume_init_ref()
447449
}
448450

449-
/// Safety: The value must be initialized
451+
/// # Safety
452+
///
453+
/// The value must be initialized
450454
unsafe fn get_unchecked_mut(&mut self) -> &mut T {
451455
debug_assert!(self.is_initialized());
452456
(&mut *self.value.get()).assume_init_mut()
@@ -456,7 +460,7 @@ impl<T> SyncOnceCell<T> {
456460
unsafe impl<#[may_dangle] T> Drop for SyncOnceCell<T> {
457461
fn drop(&mut self) {
458462
if self.is_initialized() {
459-
// Safety: The cell is initialized and being dropped, so it can't
463+
// SAFETY: The cell is initialized and being dropped, so it can't
460464
// be accessed again. We also don't touch the `T` other than
461465
// dropping it, which validates our usage of #[may_dangle].
462466
unsafe { (&mut *self.value.get()).assume_init_drop() };

library/std/src/sys/windows/path.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ mod tests;
88
pub const MAIN_SEP_STR: &str = "\\";
99
pub const MAIN_SEP: char = '\\';
1010

11-
// Safety: `bytes` must be a valid wtf8 encoded slice
11+
/// # Safety
12+
///
13+
/// `bytes` must be a valid wtf8 encoded slice
1214
#[inline]
1315
unsafe fn bytes_as_os_str(bytes: &[u8]) -> &OsStr {
1416
// &OsStr is layout compatible with &Slice, which is compatible with &Wtf8,
@@ -130,7 +132,7 @@ fn parse_next_component(path: &OsStr, verbatim: bool) -> (&OsStr, &OsStr) {
130132
// The max `separator_end` is `bytes.len()` and `bytes[bytes.len()..]` is a valid index.
131133
let path = &path.bytes()[separator_end..];
132134

133-
// Safety: `path` is a valid wtf8 encoded slice and each of the separators ('/', '\')
135+
// SAFETY: `path` is a valid wtf8 encoded slice and each of the separators ('/', '\')
134136
// is encoded in a single byte, therefore `bytes[separator_start]` and
135137
// `bytes[separator_end]` must be code point boundaries and thus
136138
// `bytes[..separator_start]` and `bytes[separator_end..]` are valid wtf8 slices.

library/std/src/sys_common/rwlock.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl StaticRWLock {
103103
/// The lock is automatically unlocked when the returned guard is dropped.
104104
#[inline]
105105
pub fn read_with_guard(&'static self) -> RWLockReadGuard {
106-
// Safety: All methods require static references, therefore self
106+
// SAFETY: All methods require static references, therefore self
107107
// cannot be moved between invocations.
108108
unsafe {
109109
self.0.read();
@@ -117,7 +117,7 @@ impl StaticRWLock {
117117
/// The lock is automatically unlocked when the returned guard is dropped.
118118
#[inline]
119119
pub fn write_with_guard(&'static self) -> RWLockWriteGuard {
120-
// Safety: All methods require static references, therefore self
120+
// SAFETY: All methods require static references, therefore self
121121
// cannot be moved between invocations.
122122
unsafe {
123123
self.0.write();

src/tools/tidy/src/style.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ pub fn check(path: &Path, bad: &mut bool) {
289289
suppressible_tidy_err!(err, skip_undocumented_unsafe, "undocumented unsafe");
290290
}
291291
}
292-
if line.contains("// SAFETY:") || line.contains("// Safety:") {
292+
if line.contains("// SAFETY:") {
293293
last_safety_comment = true;
294294
} else if line.trim().starts_with("//") || line.trim().is_empty() {
295295
// keep previous value

0 commit comments

Comments
 (0)