Skip to content

Commit

Permalink
Fix clippy::uninit_vec warning
Browse files Browse the repository at this point in the history
    error: calling `set_len()` immediately after reserving a buffer creates uninitialized values
      --> futures-util/src/io/buf_reader.rs:52:13
       |
    52 |             let mut buffer = Vec::with_capacity(capacity);
       |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    53 |             buffer.set_len(capacity);
       |             ^^^^^^^^^^^^^^^^^^^^^^^^
       |
       = note: `#[deny(clippy::uninit_vec)]` on by default
       = help: initialize the buffer or wrap the content in `MaybeUninit`
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninit_vec
  • Loading branch information
taiki-e committed Oct 5, 2024
1 parent f3fb74d commit 8a8b085
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions futures-util/src/io/buf_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,9 @@ impl<R: AsyncRead> BufReader<R> {

/// Creates a new `BufReader` with the specified buffer capacity.
pub fn with_capacity(capacity: usize, inner: R) -> Self {
unsafe {
let mut buffer = Vec::with_capacity(capacity);
buffer.set_len(capacity);
super::initialize(&inner, &mut buffer);
Self { inner, buffer: buffer.into_boxed_slice(), pos: 0, cap: 0 }
}
// TODO: consider using Box<[u8]>::new_uninit_slice once it stabilized
let buffer = vec![0; capacity];
Self { inner, buffer: buffer.into_boxed_slice(), pos: 0, cap: 0 }
}
}

Expand Down

0 comments on commit 8a8b085

Please sign in to comment.