Skip to content

read-zero-byte-vec if we assign to a var, post suggestion turns the assignment into () bc of the .resize call #15575

@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::read-zero-byte-vec

this code:

use std::net::TcpListener;

use std::io::Read;

fn main() {
    let listener = TcpListener::bind("127.0.0.1:9010").unwrap();
    let mut stream_and_addr = listener.accept().unwrap();
    let mut buf = Vec::with_capacity(32);
    let num_bytes_received = stream_and_addr.0.read(&mut buf).unwrap();

    println!("Got {} bytes", num_bytes_received);
}

caused the following diagnostics:

    Checking _snippet_0 v0.1.0 (/tmp/icemaker_global_tempdir.W9mV5T7suTWy/icemaker_clippyfix_tempdir.Krho1Ov5Kvsw/_snippet_0)
warning: reading zero byte data to `Vec`
 --> src/main.rs:9:30
  |
9 |     let num_bytes_received = stream_and_addr.0.read(&mut buf).unwrap();
  |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `buf.resize(32, 0); stream_and_addr.0.read(&mut buf)`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#read_zero_byte_vec
  = note: requested on the command line with `--force-warn clippy::read-zero-byte-vec`

warning: `_snippet_0` (bin "_snippet_0") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.22s

However after applying these diagnostics, the resulting code:

use std::net::TcpListener;

use std::io::Read;

fn main() {
    let listener = TcpListener::bind("127.0.0.1:9010").unwrap();
    let mut stream_and_addr = listener.accept().unwrap();
    let mut buf = Vec::with_capacity(32);
    let num_bytes_received = buf.resize(32, 0); stream_and_addr.0.read(&mut buf).unwrap();

    println!("Got {} bytes", num_bytes_received);
}

no longer compiled:

    Checking _snippet_0 v0.1.0 (/tmp/icemaker_global_tempdir.W9mV5T7suTWy/icemaker_clippyfix_tempdir.Krho1Ov5Kvsw/_snippet_0)
error[E0277]: `()` doesn't implement `std::fmt::Display`
  --> src/main.rs:11:30
   |
11 |     println!("Got {} bytes", num_bytes_received);
   |                   --         ^^^^^^^^^^^^^^^^^^ `()` cannot be formatted with the default formatter
   |                   |
   |                   required by this formatting parameter
   |
   = help: the trait `std::fmt::Display` is not implemented for `()`
   = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
   = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `_snippet_0` (bin "_snippet_0") due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_snippet_0` (bin "_snippet_0" test) due to 1 previous error

Version:

rustc 1.91.0-nightly (91ee6a405 2025-08-26)
binary: rustc
commit-hash: 91ee6a4057ce4bf1ab6d2f932cae497488d67c81
commit-date: 2025-08-26
host: x86_64-unknown-linux-gnu
release: 1.91.0-nightly
LLVM version: 21.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions