From 8a8b085a8c1a7396173a104c67e0cf2f5b74fc74 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Tue, 26 Oct 2021 22:50:24 +0900 Subject: [PATCH] Fix clippy::uninit_vec warning 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 --- futures-util/src/io/buf_reader.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/futures-util/src/io/buf_reader.rs b/futures-util/src/io/buf_reader.rs index ec6a8e154..9a919f718 100644 --- a/futures-util/src/io/buf_reader.rs +++ b/futures-util/src/io/buf_reader.rs @@ -48,12 +48,9 @@ impl BufReader { /// 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 } } }