Skip to content

Commit a4612e7

Browse files
committed
Improve and regularize comment placement in doc code
Because doc code does not get automatically formatted, some doc code has creative placements of comments that automatic formatting can't handle. Reformat those comments to make the resulting code support standard Rust formatting without breaking; this is generally an improvement to readability as well. Some comments are not indented to the prevailing indent, and are instead aligned under some bit of code. Indent them to the prevailing indent, and put spaces *inside* the comments to align them with code. Some comments span several lines of code (which aren't the line the comment is about) and expect alignment. Reformat them into one comment not broken up by unrelated intervening code. Some comments are placed on the same line as an opening brace, placing them effectively inside the subsequent block, such that formatting would typically format them like a line of that block. Move those comments to attach them to what they apply to. Some comments are placed on the same line as a one-line braced block, effectively attaching them to the closing brace, even though they're about the code inside the block. Reformat to make sure the comment will stay on the same line as the code it's commenting.
1 parent 24c0319 commit a4612e7

File tree

8 files changed

+28
-24
lines changed

8 files changed

+28
-24
lines changed

alloc/src/vec/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3778,8 +3778,8 @@ impl<T, A: Allocator> Vec<T, A> {
37783778
/// while i < vec.len() - end_items {
37793779
/// if some_predicate(&mut vec[i]) {
37803780
/// let val = vec.remove(i);
3781-
/// # extracted.push(val);
37823781
/// // your code here
3782+
/// # extracted.push(val);
37833783
/// } else {
37843784
/// i += 1;
37853785
/// }

core/src/cell.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,9 +2068,9 @@ impl<T: ?Sized + fmt::Display> fmt::Display for RefMut<'_, T> {
20682068
/// implies exclusive access to its `T`:
20692069
///
20702070
/// ```rust
2071-
/// #![forbid(unsafe_code)] // with exclusive accesses,
2072-
/// // `UnsafeCell` is a transparent no-op wrapper,
2073-
/// // so no need for `unsafe` here.
2071+
/// #![forbid(unsafe_code)]
2072+
/// // with exclusive accesses, `UnsafeCell` is a transparent no-op wrapper, so no need for
2073+
/// // `unsafe` here.
20742074
/// use std::cell::UnsafeCell;
20752075
///
20762076
/// let mut x: UnsafeCell<i32> = 42.into();

core/src/hint.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,6 @@ pub const fn must_use<T>(value: T) -> T {
649649
/// }
650650
/// }
651651
/// ```
652-
///
653-
///
654652
#[unstable(feature = "likely_unlikely", issue = "136873")]
655653
#[inline(always)]
656654
pub const fn likely(b: bool) -> bool {

core/src/iter/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,12 @@
233233
//!
234234
//! ```
235235
//! let mut values = vec![41];
236-
//! for x in &mut values { // same as `values.iter_mut()`
236+
//! for x in &mut values {
237+
//! // ^ same as `values.iter_mut()`
237238
//! *x += 1;
238239
//! }
239-
//! for x in &values { // same as `values.iter()`
240+
//! for x in &values {
241+
//! // ^ same as `values.iter()`
240242
//! assert_eq!(*x, 42);
241243
//! }
242244
//! assert_eq!(values.len(), 1);

core/src/macros/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,10 @@ pub macro cfg_select($($tt:tt)*) {
271271
/// // expression given.
272272
/// debug_assert!(true);
273273
///
274-
/// fn some_expensive_computation() -> bool { true } // a very simple function
274+
/// fn some_expensive_computation() -> bool {
275+
/// // Some expensive computation here
276+
/// true
277+
/// }
275278
/// debug_assert!(some_expensive_computation());
276279
///
277280
/// // assert with a custom message
@@ -1545,7 +1548,10 @@ pub(crate) mod builtin {
15451548
/// // expression given.
15461549
/// assert!(true);
15471550
///
1548-
/// fn some_computation() -> bool { true } // a very simple function
1551+
/// fn some_computation() -> bool {
1552+
/// // Some expensive computation here
1553+
/// true
1554+
/// }
15491555
///
15501556
/// assert!(some_computation());
15511557
///

core/src/marker.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ unsafe impl<T: Sync + PointeeSized> Send for &T {}
138138
/// impl Bar for Impl { }
139139
///
140140
/// let x: &dyn Foo = &Impl; // OK
141-
/// // let y: &dyn Bar = &Impl; // error: the trait `Bar` cannot
142-
/// // be made into an object
141+
/// // let y: &dyn Bar = &Impl; // error: the trait `Bar` cannot be made into an object
143142
/// ```
144143
///
145144
/// [trait object]: ../../book/ch17-02-trait-objects.html

core/src/mem/maybe_uninit.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -773,8 +773,7 @@ impl<T> MaybeUninit<T> {
773773
/// // Initialize the `MaybeUninit` using `Cell::set`:
774774
/// unsafe {
775775
/// b.assume_init_ref().set(true);
776-
/// // ^^^^^^^^^^^^^^^
777-
/// // Reference to an uninitialized `Cell<bool>`: UB!
776+
/// //^^^^^^^^^^^^^^^ Reference to an uninitialized `Cell<bool>`: UB!
778777
/// }
779778
/// ```
780779
#[stable(feature = "maybe_uninit_ref", since = "1.55.0")]
@@ -864,9 +863,9 @@ impl<T> MaybeUninit<T> {
864863
/// {
865864
/// let mut buffer = MaybeUninit::<[u8; 64]>::uninit();
866865
/// reader.read_exact(unsafe { buffer.assume_init_mut() })?;
867-
/// // ^^^^^^^^^^^^^^^^^^^^^^^^
868-
/// // (mutable) reference to uninitialized memory!
869-
/// // This is undefined behavior.
866+
/// // ^^^^^^^^^^^^^^^^^^^^^^^^
867+
/// // (mutable) reference to uninitialized memory!
868+
/// // This is undefined behavior.
870869
/// Ok(unsafe { buffer.assume_init() })
871870
/// }
872871
/// ```
@@ -884,13 +883,13 @@ impl<T> MaybeUninit<T> {
884883
/// let foo: Foo = unsafe {
885884
/// let mut foo = MaybeUninit::<Foo>::uninit();
886885
/// ptr::write(&mut foo.assume_init_mut().a as *mut u32, 1337);
887-
/// // ^^^^^^^^^^^^^^^^^^^^^
888-
/// // (mutable) reference to uninitialized memory!
889-
/// // This is undefined behavior.
886+
/// // ^^^^^^^^^^^^^^^^^^^^^
887+
/// // (mutable) reference to uninitialized memory!
888+
/// // This is undefined behavior.
890889
/// ptr::write(&mut foo.assume_init_mut().b as *mut u8, 42);
891-
/// // ^^^^^^^^^^^^^^^^^^^^^
892-
/// // (mutable) reference to uninitialized memory!
893-
/// // This is undefined behavior.
890+
/// // ^^^^^^^^^^^^^^^^^^^^^
891+
/// // (mutable) reference to uninitialized memory!
892+
/// // This is undefined behavior.
894893
/// foo.assume_init()
895894
/// };
896895
/// ```

core/src/result.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,7 @@ impl<T, E> Result<T, E> {
15441544
///
15451545
/// ```no_run
15461546
/// let x: Result<u32, &str> = Err("emergency failure");
1547-
/// unsafe { x.unwrap_unchecked(); } // Undefined behavior!
1547+
/// unsafe { x.unwrap_unchecked() }; // Undefined behavior!
15481548
/// ```
15491549
#[inline]
15501550
#[track_caller]

0 commit comments

Comments
 (0)