rust-clippy/tests/ui/read_zero_byte_vec.rs

122 lines
2.9 KiB
Rust
Raw Normal View History

2022-06-06 13:48:05 +00:00
#![warn(clippy::read_zero_byte_vec)]
#![allow(
clippy::unused_io_amount,
clippy::needless_pass_by_ref_mut,
clippy::slow_vector_initialization
)]
2022-06-06 13:48:05 +00:00
use std::fs::File;
use std::io;
use std::io::prelude::*;
2023-07-27 11:40:22 +00:00
//@no-rustfix
2022-06-06 13:48:05 +00: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();
//~^ ERROR: reading zero byte data to `Vec`
//~| NOTE: `-D clippy::read-zero-byte-vec` implied by `-D warnings`
2022-06-06 13:48:05 +00:00
// should lint
let mut data2 = Vec::with_capacity(cap);
f.read_exact(&mut data2)?;
//~^ ERROR: reading zero byte data to `Vec`
2022-06-06 13:48:05 +00:00
// should lint
let mut data3 = Vec::new();
f.read_exact(&mut data3)?;
//~^ ERROR: reading zero byte data to `Vec`
2022-06-06 13:48:05 +00:00
// should lint
let mut data4 = vec![];
let _ = f.read(&mut data4)?;
//~^ ERROR: reading zero byte data to `Vec`
2022-06-06 13:48:05 +00:00
// should lint
let _ = {
let mut data5 = Vec::new();
f.read(&mut data5)
//~^ ERROR: reading zero byte data to `Vec`
2022-06-06 13:48:05 +00:00
};
// should lint
let _ = {
let mut data6: Vec<u8> = Default::default();
f.read(&mut data6)
//~^ ERROR: reading zero byte data to `Vec`
2022-06-06 13:48:05 +00: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(())
}
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-06 13:48:05 +00:00
async fn test_futures<R: AsyncRead + Unpin>(r: &mut R) {
// should lint
let mut data = Vec::new();
r.read(&mut data).await.unwrap();
//~^ ERROR: reading zero byte data to `Vec`
2022-06-06 13:48:05 +00:00
// should lint
let mut data2 = Vec::new();
r.read_exact(&mut data2).await.unwrap();
//~^ ERROR: reading zero byte data to `Vec`
2022-06-06 13:48:05 +00:00
}
async fn test_tokio<R: TokioAsyncRead + Unpin>(r: &mut R) {
// should lint
let mut data = Vec::new();
r.read(&mut data).await.unwrap();
//~^ ERROR: reading zero byte data to `Vec`
2022-06-06 13:48:05 +00:00
// should lint
let mut data2 = Vec::new();
r.read_exact(&mut data2).await.unwrap();
//~^ ERROR: reading zero byte data to `Vec`
2022-06-06 13:48:05 +00: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-06 13:48:05 +00:00
fn main() {}