2023-07-17 03:19:29 -05:00
|
|
|
#![allow(dead_code, clippy::needless_pass_by_ref_mut)]
|
2024-01-25 12:17:36 -06:00
|
|
|
#![allow(clippy::redundant_pattern_matching)]
|
2018-07-28 10:34:52 -05:00
|
|
|
#![warn(clippy::unused_io_amount)]
|
2017-01-07 05:35:45 -06:00
|
|
|
|
2022-01-13 06:18:19 -06:00
|
|
|
extern crate futures;
|
|
|
|
use futures::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
2021-04-27 09:55:11 -05:00
|
|
|
use std::io::{self, Read};
|
2017-01-07 05:35:45 -06:00
|
|
|
|
|
|
|
fn question_mark<T: io::Read + io::Write>(s: &mut T) -> io::Result<()> {
|
|
|
|
s.write(b"test")?;
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: written amount is not handled
|
2017-01-07 05:35:45 -06:00
|
|
|
let mut buf = [0u8; 4];
|
|
|
|
s.read(&mut buf)?;
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: read amount is not handled
|
2017-01-07 05:35:45 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unwrap<T: io::Read + io::Write>(s: &mut T) {
|
|
|
|
s.write(b"test").unwrap();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: written amount is not handled
|
2017-01-07 05:35:45 -06:00
|
|
|
let mut buf = [0u8; 4];
|
|
|
|
s.read(&mut buf).unwrap();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: read amount is not handled
|
2017-01-07 05:35:45 -06:00
|
|
|
}
|
|
|
|
|
2020-01-09 08:46:55 -06:00
|
|
|
fn vectored<T: io::Read + io::Write>(s: &mut T) -> io::Result<()> {
|
|
|
|
s.read_vectored(&mut [io::IoSliceMut::new(&mut [])])?;
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: read amount is not handled
|
2020-01-09 08:46:55 -06:00
|
|
|
s.write_vectored(&[io::IoSlice::new(&[])])?;
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: written amount is not handled
|
2020-01-09 08:46:55 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-04-27 09:55:11 -05:00
|
|
|
fn ok(file: &str) -> Option<()> {
|
|
|
|
let mut reader = std::fs::File::open(file).ok()?;
|
|
|
|
let mut result = [0u8; 0];
|
|
|
|
reader.read(&mut result).ok()?;
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: read amount is not handled
|
2021-04-27 09:55:11 -05:00
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::redundant_closure)]
|
|
|
|
#[allow(clippy::bind_instead_of_map)]
|
|
|
|
fn or_else(file: &str) -> io::Result<()> {
|
|
|
|
let mut reader = std::fs::File::open(file)?;
|
|
|
|
let mut result = [0u8; 0];
|
|
|
|
reader.read(&mut result).or_else(|err| Err(err))?;
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: read amount is not handled
|
2021-04-27 09:55:11 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum Error {
|
|
|
|
Kind,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn or(file: &str) -> Result<(), Error> {
|
|
|
|
let mut reader = std::fs::File::open(file).unwrap();
|
|
|
|
let mut result = [0u8; 0];
|
|
|
|
reader.read(&mut result).or(Err(Error::Kind))?;
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: read amount is not handled
|
2021-04-27 09:55:11 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn combine_or(file: &str) -> Result<(), Error> {
|
|
|
|
let mut reader = std::fs::File::open(file).unwrap();
|
|
|
|
let mut result = [0u8; 0];
|
|
|
|
reader
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: read amount is not handled
|
2021-04-27 09:55:11 -05:00
|
|
|
.read(&mut result)
|
|
|
|
.or(Err(Error::Kind))
|
|
|
|
.or(Err(Error::Kind))
|
|
|
|
.expect("error");
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-10 07:01:19 -06:00
|
|
|
fn is_ok_err<T: io::Read + io::Write>(s: &mut T) {
|
|
|
|
s.write(b"ok").is_ok();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: written amount is not handled
|
2023-02-10 07:01:19 -06:00
|
|
|
s.write(b"err").is_err();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: written amount is not handled
|
2023-02-10 07:01:19 -06:00
|
|
|
let mut buf = [0u8; 0];
|
|
|
|
s.read(&mut buf).is_ok();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: read amount is not handled
|
2023-02-10 07:01:19 -06:00
|
|
|
s.read(&mut buf).is_err();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: read amount is not handled
|
2023-02-10 07:01:19 -06:00
|
|
|
}
|
|
|
|
|
2022-01-13 06:18:19 -06:00
|
|
|
async fn bad_async_write<W: AsyncWrite + Unpin>(w: &mut W) {
|
|
|
|
w.write(b"hello world").await.unwrap();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: written amount is not handled
|
2022-01-13 06:18:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn bad_async_read<R: AsyncRead + Unpin>(r: &mut R) {
|
|
|
|
let mut buf = [0u8; 0];
|
|
|
|
r.read(&mut buf[..]).await.unwrap();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: read amount is not handled
|
2022-01-13 06:18:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn io_not_ignored_async_write<W: AsyncWrite + Unpin>(mut w: W) {
|
|
|
|
// Here we're forgetting to await the future, so we should get a
|
|
|
|
// warning about _that_ (or we would, if it were enabled), but we
|
|
|
|
// won't get one about ignoring the return value.
|
|
|
|
w.write(b"hello world");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bad_async_write_closure<W: AsyncWrite + Unpin + 'static>(w: W) -> impl futures::Future<Output = io::Result<()>> {
|
|
|
|
let mut w = w;
|
|
|
|
async move {
|
|
|
|
w.write(b"hello world").await?;
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: written amount is not handled
|
2022-01-13 06:18:19 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn async_read_nested_or<R: AsyncRead + Unpin>(r: &mut R, do_it: bool) -> Result<[u8; 1], Error> {
|
|
|
|
let mut buf = [0u8; 1];
|
|
|
|
if do_it {
|
|
|
|
r.read(&mut buf[..]).await.or(Err(Error::Kind))?;
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: read amount is not handled
|
2022-01-13 06:18:19 -06:00
|
|
|
}
|
|
|
|
Ok(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
use tokio::io::{AsyncRead as TokioAsyncRead, AsyncReadExt as _, AsyncWrite as TokioAsyncWrite, AsyncWriteExt as _};
|
|
|
|
|
|
|
|
async fn bad_async_write_tokio<W: TokioAsyncWrite + Unpin>(w: &mut W) {
|
|
|
|
w.write(b"hello world").await.unwrap();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: written amount is not handled
|
2022-01-13 06:18:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn bad_async_read_tokio<R: TokioAsyncRead + Unpin>(r: &mut R) {
|
|
|
|
let mut buf = [0u8; 0];
|
|
|
|
r.read(&mut buf[..]).await.unwrap();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: read amount is not handled
|
2022-01-13 06:18:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn undetected_bad_async_write<W: AsyncWrite + Unpin>(w: &mut W) {
|
|
|
|
// It would be good to detect this case some day, but the current lint
|
|
|
|
// doesn't handle it. (The documentation says that this lint "detects
|
|
|
|
// only common patterns".)
|
|
|
|
let future = w.write(b"Hello world");
|
|
|
|
future.await.unwrap();
|
|
|
|
}
|
|
|
|
|
2024-01-25 12:17:36 -06:00
|
|
|
fn match_okay_underscore<T: io::Read + io::Write>(s: &mut T) {
|
|
|
|
match s.write(b"test") {
|
|
|
|
//~^ ERROR: written amount is not handled
|
|
|
|
Ok(_) => todo!(),
|
|
|
|
//~^ NOTE: the result is consumed here, but the amount of I/O bytes remains unhandled
|
|
|
|
Err(_) => todo!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut buf = [0u8; 4];
|
|
|
|
match s.read(&mut buf) {
|
|
|
|
//~^ ERROR: read amount is not handled
|
|
|
|
Ok(_) => todo!(),
|
|
|
|
//~^ NOTE: the result is consumed here, but the amount of I/O bytes remains unhandled
|
|
|
|
Err(_) => todo!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn match_okay_underscore_read_expr<T: io::Read + io::Write>(s: &mut T) {
|
|
|
|
match s.read(&mut [0u8; 4]) {
|
|
|
|
//~^ ERROR: read amount is not handled
|
|
|
|
Ok(_) => todo!(),
|
|
|
|
//~^ NOTE: the result is consumed here, but the amount of I/O bytes remains unhandled
|
|
|
|
Err(_) => todo!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn match_okay_underscore_write_expr<T: io::Read + io::Write>(s: &mut T) {
|
|
|
|
match s.write(b"test") {
|
|
|
|
//~^ ERROR: written amount is not handled
|
|
|
|
Ok(_) => todo!(),
|
|
|
|
//~^ NOTE: the result is consumed here, but the amount of I/O bytes remains unhandled
|
|
|
|
Err(_) => todo!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn returned_value_should_not_lint<T: io::Read + io::Write>(s: &mut T) -> Result<usize, std::io::Error> {
|
|
|
|
s.write(b"test")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn if_okay_underscore_read_expr<T: io::Read + io::Write>(s: &mut T) {
|
|
|
|
if let Ok(_) = s.read(&mut [0u8; 4]) {
|
|
|
|
//~^ ERROR: read amount is not handled
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn if_okay_underscore_write_expr<T: io::Read + io::Write>(s: &mut T) {
|
|
|
|
if let Ok(_) = s.write(b"test") {
|
|
|
|
//~^ ERROR: written amount is not handled
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn if_okay_dots_write_expr<T: io::Read + io::Write>(s: &mut T) {
|
|
|
|
if let Ok(..) = s.write(b"test") {
|
|
|
|
//~^ ERROR: written amount is not handled
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn if_okay_underscore_write_expr_true_negative<T: io::Read + io::Write>(s: &mut T) {
|
|
|
|
if let Ok(bound) = s.write(b"test") {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn match_okay_underscore_true_neg<T: io::Read + io::Write>(s: &mut T) {
|
|
|
|
match s.write(b"test") {
|
|
|
|
Ok(bound) => todo!(),
|
|
|
|
Err(_) => todo!(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn true_negative<T: io::Read + io::Write>(s: &mut T) {
|
|
|
|
let mut buf = [0u8; 4];
|
|
|
|
let read_amount = s.read(&mut buf).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_return_should_not_raise<T: io::Read + io::Write>(s: &mut T) -> io::Result<usize> {
|
|
|
|
/// this is bad code because it goes around the problem of handling the read amount
|
|
|
|
/// by returning it, which makes it impossible to know this is a resonpose from the
|
|
|
|
/// correct account.
|
|
|
|
let mut buf = [0u8; 4];
|
|
|
|
s.read(&mut buf)
|
|
|
|
}
|
|
|
|
|
2024-02-08 13:24:42 -06:00
|
|
|
pub fn unwrap_in_block(rdr: &mut dyn std::io::Read) -> std::io::Result<usize> {
|
|
|
|
let read = { rdr.read(&mut [0])? };
|
|
|
|
Ok(read)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn consumed_example(rdr: &mut dyn std::io::Read) {
|
|
|
|
match rdr.read(&mut [0]) {
|
|
|
|
Ok(0) => println!("EOF"),
|
|
|
|
Ok(_) => println!("fully read"),
|
|
|
|
Err(_) => println!("fail"),
|
|
|
|
};
|
|
|
|
match rdr.read(&mut [0]) {
|
|
|
|
Ok(0) => println!("EOF"),
|
|
|
|
Ok(_) => println!("fully read"),
|
|
|
|
Err(_) => println!("fail"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unreachable_or_panic(rdr: &mut dyn std::io::Read) {
|
|
|
|
{
|
|
|
|
match rdr.read(&mut [0]) {
|
|
|
|
Ok(_) => unreachable!(),
|
|
|
|
Err(_) => println!("expected"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
match rdr.read(&mut [0]) {
|
|
|
|
Ok(_) => panic!(),
|
|
|
|
Err(_) => println!("expected"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn wildcards(rdr: &mut dyn std::io::Read) {
|
|
|
|
{
|
|
|
|
match rdr.read(&mut [0]) {
|
|
|
|
Ok(1) => todo!(),
|
|
|
|
_ => todo!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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::unused_io_amount)]
|
|
|
|
f.read(&mut data).unwrap();
|
|
|
|
}
|
2024-02-08 13:24:42 -06:00
|
|
|
|
2018-12-09 16:26:16 -06:00
|
|
|
fn main() {}
|