2022-06-16 10:39:06 -05:00
|
|
|
#![warn(clippy::read_zero_byte_vec)]
|
2023-07-31 16:53:53 -05:00
|
|
|
#![allow(
|
|
|
|
clippy::unused_io_amount,
|
|
|
|
clippy::needless_pass_by_ref_mut,
|
|
|
|
clippy::slow_vector_initialization
|
|
|
|
)]
|
2022-06-16 10:39:06 -05:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
|
|
|
use std::io::prelude::*;
|
2023-08-24 14:32:12 -05:00
|
|
|
//@no-rustfix
|
2022-06-16 10:39:06 -05:00
|
|
|
extern crate futures;
|
|
|
|
use futures::io::{AsyncRead, AsyncReadExt};
|
|
|
|
use tokio::io::{AsyncRead as TokioAsyncRead, AsyncReadExt as _, AsyncWrite as TokioAsyncWrite, AsyncWriteExt as _};
|
|
|
|
|
|
|
|
fn test() -> io::Result<()> {
|
|
|
|
let cap = 1000;
|
|
|
|
let mut f = File::open("foo.txt").unwrap();
|
|
|
|
|
|
|
|
// should lint
|
|
|
|
let mut data = Vec::with_capacity(20);
|
|
|
|
f.read_exact(&mut data).unwrap();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: reading zero byte data to `Vec`
|
|
|
|
//~| NOTE: `-D clippy::read-zero-byte-vec` implied by `-D warnings`
|
2022-06-16 10:39:06 -05:00
|
|
|
|
|
|
|
// should lint
|
|
|
|
let mut data2 = Vec::with_capacity(cap);
|
|
|
|
f.read_exact(&mut data2)?;
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: reading zero byte data to `Vec`
|
2022-06-16 10:39:06 -05:00
|
|
|
|
|
|
|
// should lint
|
|
|
|
let mut data3 = Vec::new();
|
|
|
|
f.read_exact(&mut data3)?;
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: reading zero byte data to `Vec`
|
2022-06-16 10:39:06 -05:00
|
|
|
|
|
|
|
// should lint
|
|
|
|
let mut data4 = vec![];
|
|
|
|
let _ = f.read(&mut data4)?;
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: reading zero byte data to `Vec`
|
2022-06-16 10:39:06 -05:00
|
|
|
|
|
|
|
// should lint
|
|
|
|
let _ = {
|
|
|
|
let mut data5 = Vec::new();
|
|
|
|
f.read(&mut data5)
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: reading zero byte data to `Vec`
|
2022-06-16 10:39:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// should lint
|
|
|
|
let _ = {
|
|
|
|
let mut data6: Vec<u8> = Default::default();
|
|
|
|
f.read(&mut data6)
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: reading zero byte data to `Vec`
|
2022-06-16 10:39:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// should not lint
|
|
|
|
let mut buf = [0u8; 100];
|
|
|
|
f.read(&mut buf)?;
|
|
|
|
|
|
|
|
// should not lint
|
|
|
|
let mut data8 = Vec::new();
|
|
|
|
data8.resize(100, 0);
|
|
|
|
f.read_exact(&mut data8)?;
|
|
|
|
|
|
|
|
// should not lint
|
|
|
|
let mut data9 = vec![1, 2, 3];
|
|
|
|
f.read_exact(&mut data9)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-01-25 12:17:36 -06:00
|
|
|
fn test_nested() -> io::Result<()> {
|
|
|
|
let cap = 1000;
|
|
|
|
let mut f = File::open("foo.txt").unwrap();
|
|
|
|
|
|
|
|
// Issue #9274
|
|
|
|
// Should not lint
|
|
|
|
let mut v = Vec::new();
|
|
|
|
{
|
|
|
|
v.resize(10, 0);
|
|
|
|
f.read(&mut v)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut v = Vec::new();
|
|
|
|
{
|
|
|
|
f.read(&mut v)?;
|
|
|
|
//~^ ERROR: reading zero byte data to `Vec`
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-06-16 10:39:06 -05:00
|
|
|
async fn test_futures<R: AsyncRead + Unpin>(r: &mut R) {
|
|
|
|
// should lint
|
|
|
|
let mut data = Vec::new();
|
|
|
|
r.read(&mut data).await.unwrap();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: reading zero byte data to `Vec`
|
2022-06-16 10:39:06 -05:00
|
|
|
|
|
|
|
// should lint
|
|
|
|
let mut data2 = Vec::new();
|
|
|
|
r.read_exact(&mut data2).await.unwrap();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: reading zero byte data to `Vec`
|
2022-06-16 10:39:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn test_tokio<R: TokioAsyncRead + Unpin>(r: &mut R) {
|
|
|
|
// should lint
|
|
|
|
let mut data = Vec::new();
|
|
|
|
r.read(&mut data).await.unwrap();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: reading zero byte data to `Vec`
|
2022-06-16 10:39:06 -05:00
|
|
|
|
|
|
|
// should lint
|
|
|
|
let mut data2 = Vec::new();
|
|
|
|
r.read_exact(&mut data2).await.unwrap();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: reading zero byte data to `Vec`
|
2022-06-16 10:39:06 -05:00
|
|
|
}
|
|
|
|
|
2024-03-21 16:20:40 -05:00
|
|
|
fn allow_works<F: std::io::Read>(mut f: F) {
|
|
|
|
let mut data = Vec::with_capacity(100);
|
|
|
|
#[allow(clippy::read_zero_byte_vec)]
|
|
|
|
f.read(&mut data).unwrap();
|
|
|
|
}
|
|
|
|
|
2022-06-16 10:39:06 -05:00
|
|
|
fn main() {}
|