Skip to content

Commit 50d1f58

Browse files
Rollup merge of rust-lang#144478 - joshtriplett:doc-code-formatting-prep, r=Amanieu
Improve formatting of doc code blocks We don't currently apply automatic formatting to doc comment code blocks. As a result, it has built up various idiosyncracies, which make such automatic formatting difficult. Some of those idiosyncracies also make things harder for human readers or other tools. This PR makes a few improvements to doc code formatting, in the hopes of making future automatic formatting easier, as well as in many cases providing net readability improvements. I would suggest reading each commit separately, as each commit contains one class of changes.
2 parents d71da41 + c86b3c8 commit 50d1f58

File tree

12 files changed

+39
-34
lines changed

12 files changed

+39
-34
lines changed

alloc/src/string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,12 @@ use crate::vec::{self, Vec};
265265
/// You can look at these with the [`as_ptr`], [`len`], and [`capacity`]
266266
/// methods:
267267
///
268+
// FIXME Update this when vec_into_raw_parts is stabilized
268269
/// ```
269270
/// use std::mem;
270271
///
271272
/// let story = String::from("Once upon a time...");
272273
///
273-
// FIXME Update this when vec_into_raw_parts is stabilized
274274
/// // Prevent automatically dropping the String's data
275275
/// let mut story = mem::ManuallyDrop::new(story);
276276
///
@@ -970,13 +970,13 @@ impl String {
970970
///
971971
/// # Examples
972972
///
973+
// FIXME Update this when vec_into_raw_parts is stabilized
973974
/// ```
974975
/// use std::mem;
975976
///
976977
/// unsafe {
977978
/// let s = String::from("hello");
978979
///
979-
// FIXME Update this when vec_into_raw_parts is stabilized
980980
/// // Prevent automatically dropping the String's data
981981
/// let mut s = mem::ManuallyDrop::new(s);
982982
///

alloc/src/vec/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -566,13 +566,13 @@ impl<T> Vec<T> {
566566
///
567567
/// # Examples
568568
///
569+
// FIXME Update this when vec_into_raw_parts is stabilized
569570
/// ```
570571
/// use std::ptr;
571572
/// use std::mem;
572573
///
573574
/// let v = vec![1, 2, 3];
574575
///
575-
// FIXME Update this when vec_into_raw_parts is stabilized
576576
/// // Prevent running `v`'s destructor so we are in complete control
577577
/// // of the allocation.
578578
/// let mut v = mem::ManuallyDrop::new(v);
@@ -674,6 +674,7 @@ impl<T> Vec<T> {
674674
///
675675
/// # Examples
676676
///
677+
// FIXME Update this when vec_into_raw_parts is stabilized
677678
/// ```
678679
/// #![feature(box_vec_non_null)]
679680
///
@@ -682,7 +683,6 @@ impl<T> Vec<T> {
682683
///
683684
/// let v = vec![1, 2, 3];
684685
///
685-
// FIXME Update this when vec_into_raw_parts is stabilized
686686
/// // Prevent running `v`'s destructor so we are in complete control
687687
/// // of the allocation.
688688
/// let mut v = mem::ManuallyDrop::new(v);
@@ -994,6 +994,7 @@ impl<T, A: Allocator> Vec<T, A> {
994994
///
995995
/// # Examples
996996
///
997+
// FIXME Update this when vec_into_raw_parts is stabilized
997998
/// ```
998999
/// #![feature(allocator_api)]
9991000
///
@@ -1007,7 +1008,6 @@ impl<T, A: Allocator> Vec<T, A> {
10071008
/// v.push(2);
10081009
/// v.push(3);
10091010
///
1010-
// FIXME Update this when vec_into_raw_parts is stabilized
10111011
/// // Prevent running `v`'s destructor so we are in complete control
10121012
/// // of the allocation.
10131013
/// let mut v = mem::ManuallyDrop::new(v);
@@ -1114,6 +1114,7 @@ impl<T, A: Allocator> Vec<T, A> {
11141114
///
11151115
/// # Examples
11161116
///
1117+
// FIXME Update this when vec_into_raw_parts is stabilized
11171118
/// ```
11181119
/// #![feature(allocator_api, box_vec_non_null)]
11191120
///
@@ -1127,7 +1128,6 @@ impl<T, A: Allocator> Vec<T, A> {
11271128
/// v.push(2);
11281129
/// v.push(3);
11291130
///
1130-
// FIXME Update this when vec_into_raw_parts is stabilized
11311131
/// // Prevent running `v`'s destructor so we are in complete control
11321132
/// // of the allocation.
11331133
/// let mut v = mem::ManuallyDrop::new(v);
@@ -3872,8 +3872,8 @@ impl<T, A: Allocator> Vec<T, A> {
38723872
/// while i < vec.len() - end_items {
38733873
/// if some_predicate(&mut vec[i]) {
38743874
/// let val = vec.remove(i);
3875-
/// # extracted.push(val);
38763875
/// // your code here
3876+
/// # extracted.push(val);
38773877
/// } else {
38783878
/// i += 1;
38793879
/// }

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/iter/traits/iterator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ pub trait Iterator {
807807
/// might be preferable to keep a functional style with longer iterators:
808808
///
809809
/// ```
810-
/// (0..5).flat_map(|x| x * 100 .. x * 110)
810+
/// (0..5).flat_map(|x| (x * 100)..(x * 110))
811811
/// .enumerate()
812812
/// .filter(|&(i, x)| (i + x) % 3 == 0)
813813
/// .for_each(|(i, x)| println!("{i}:{x}"));

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
@@ -1547,7 +1550,10 @@ pub(crate) mod builtin {
15471550
/// // expression given.
15481551
/// assert!(true);
15491552
///
1550-
/// fn some_computation() -> bool { true } // a very simple function
1553+
/// fn some_computation() -> bool {
1554+
/// // Some expensive computation here
1555+
/// true
1556+
/// }
15511557
///
15521558
/// assert!(some_computation());
15531559
///

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/primitive_docs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ mod prim_bool {}
271271
/// When the compiler sees a value of type `!` in a [coercion site], it implicitly inserts a
272272
/// coercion to allow the type checker to infer any type:
273273
///
274+
// FIXME: use `core::convert::absurd` here instead, once it's merged
274275
/// ```rust,ignore (illustrative-and-has-placeholders)
275276
/// // this
276277
/// let x: u8 = panic!();
@@ -281,7 +282,6 @@ mod prim_bool {}
281282
/// // where absurd is a function with the following signature
282283
/// // (it's sound, because `!` always marks unreachable code):
283284
/// fn absurd<T>(_: !) -> T { ... }
284-
// FIXME: use `core::convert::absurd` here instead, once it's merged
285285
/// ```
286286
///
287287
/// This can lead to compilation errors if the type cannot be inferred:

0 commit comments

Comments
 (0)