-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c36c6d8
commit 23e1ff5
Showing
3 changed files
with
249 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,87 @@ | ||
#![warn(clippy::read_zero_byte_vec)] | ||
#![allow(clippy::unused_io_amount)] | ||
use std::fs::File; | ||
use std::io; | ||
use std::io::prelude::*; | ||
|
||
fn main() { | ||
// test code goes here | ||
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(); | ||
|
||
// should lint | ||
let mut data2 = Vec::with_capacity(cap); | ||
f.read_exact(&mut data2)?; | ||
|
||
// should lint | ||
let mut data3 = Vec::new(); | ||
f.read_exact(&mut data3)?; | ||
|
||
// should lint | ||
let mut data4 = vec![]; | ||
let _ = f.read(&mut data4)?; | ||
|
||
// should lint | ||
let _ = { | ||
let mut data5 = Vec::new(); | ||
f.read(&mut data5) | ||
}; | ||
|
||
// should lint | ||
let _ = { | ||
let mut data6: Vec<u8> = Default::default(); | ||
f.read(&mut data6) | ||
}; | ||
|
||
// should not lint | ||
let mut buf = [0u8; 100]; | ||
f.read(&mut buf)?; | ||
|
||
// should not lint | ||
let mut empty = vec![]; | ||
let mut data7 = vec![]; | ||
f.read(&mut empty); | ||
|
||
// should not lint | ||
f.read(&mut data7); | ||
|
||
// 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(()) | ||
} | ||
|
||
async fn test_futures<R: AsyncRead + Unpin>(r: &mut R) { | ||
// should lint | ||
let mut data = Vec::new(); | ||
r.read(&mut data).await.unwrap(); | ||
|
||
// should lint | ||
let mut data2 = Vec::new(); | ||
r.read_exact(&mut data2).await.unwrap(); | ||
} | ||
|
||
async fn test_tokio<R: TokioAsyncRead + Unpin>(r: &mut R) { | ||
// should lint | ||
let mut data = Vec::new(); | ||
r.read(&mut data).await.unwrap(); | ||
|
||
// should lint | ||
let mut data2 = Vec::new(); | ||
r.read_exact(&mut data2).await.unwrap(); | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
error: reading zero byte data to `Vec` | ||
--> $DIR/read_zero_byte_vec.rs:17:5 | ||
| | ||
LL | f.read_exact(&mut data).unwrap(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `data.resize(20, 0); f.read_exact(&mut data).unwrap();` | ||
| | ||
= note: `-D clippy::read-zero-byte-vec` implied by `-D warnings` | ||
|
||
error: reading zero byte data to `Vec` | ||
--> $DIR/read_zero_byte_vec.rs:21:5 | ||
| | ||
LL | f.read_exact(&mut data2)?; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `data2.resize(cap, 0); f.read_exact(&mut data2)?;` | ||
|
||
error: reading zero byte data to `Vec` | ||
--> $DIR/read_zero_byte_vec.rs:25:5 | ||
| | ||
LL | f.read_exact(&mut data3)?; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: reading zero byte data to `Vec` | ||
--> $DIR/read_zero_byte_vec.rs:29:5 | ||
| | ||
LL | let _ = f.read(&mut data4)?; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: reading zero byte data to `Vec` | ||
--> $DIR/read_zero_byte_vec.rs:34:9 | ||
| | ||
LL | f.read(&mut data5) | ||
| ^^^^^^^^^^^^^^^^^^ | ||
|
||
error: reading zero byte data to `Vec` | ||
--> $DIR/read_zero_byte_vec.rs:40:9 | ||
| | ||
LL | f.read(&mut data6) | ||
| ^^^^^^^^^^^^^^^^^^ | ||
|
||
error: reading zero byte data to `Vec` | ||
--> $DIR/read_zero_byte_vec.rs:70:5 | ||
| | ||
LL | r.read(&mut data).await.unwrap(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: reading zero byte data to `Vec` | ||
--> $DIR/read_zero_byte_vec.rs:74:5 | ||
| | ||
LL | r.read_exact(&mut data2).await.unwrap(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: reading zero byte data to `Vec` | ||
--> $DIR/read_zero_byte_vec.rs:80:5 | ||
| | ||
LL | r.read(&mut data).await.unwrap(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: reading zero byte data to `Vec` | ||
--> $DIR/read_zero_byte_vec.rs:84:5 | ||
| | ||
LL | r.read_exact(&mut data2).await.unwrap(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 10 previous errors | ||
|