Skip to content

Commit 2a4a572

Browse files
committed
Ensure each code sample compiles by itself
1 parent 0efdff5 commit 2a4a572

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

clippy_lints/src/same_length_and_capacity.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,47 @@ declare_clippy_lint! {
2424
/// ### Example
2525
///
2626
/// ```no_run
27+
/// #![feature(vec_into_raw_parts)]
2728
/// let mut original: Vec::<i32> = Vec::with_capacity(20);
2829
/// original.extend([1, 2, 3, 4, 5]);
2930
///
3031
/// let (ptr, mut len, cap) = original.into_raw_parts();
3132
///
32-
/// // Pretend we added three more integers:
33-
/// len = 8;
33+
/// // I will add three more integers:
34+
/// unsafe {
35+
/// let ptr = ptr as *mut i32;
36+
///
37+
/// for i in 6..9 {
38+
/// *ptr.add(i - 1) = i as i32;
39+
/// len += 1;
40+
/// }
41+
///}
3442
///
3543
/// // But I forgot the capacity was separate from the length:
3644
/// let reconstructed = unsafe { Vec::from_raw_parts(ptr, len, len) };
3745
/// ```
3846
///
3947
/// Use instead:
4048
/// ```no_run
41-
/// // Correction to the last line of the given example code:
49+
/// #![feature(vec_into_raw_parts)]
50+
/// let mut original: Vec::<i32> = Vec::with_capacity(20);
51+
/// original.extend([1, 2, 3, 4, 5]);
52+
///
53+
/// let (ptr, mut len, cap) = original.into_raw_parts();
54+
///
55+
/// // I will add three more integers:
56+
/// unsafe {
57+
/// let ptr = ptr as *mut i32;
58+
///
59+
/// for i in 6..9 {
60+
/// *ptr.add(i - 1) = i as i32;
61+
/// len += 1;
62+
/// }
63+
///}
64+
///
65+
/// // This time, leverage the previously saved capacity:
4266
/// let reconstructed = unsafe { Vec::from_raw_parts(ptr, len, cap) };
67+
4368
/// ```
4469
#[clippy::version = "1.91.0"]
4570
pub SAME_LENGTH_AND_CAPACITY,

0 commit comments

Comments
 (0)