2 #![warn(clippy::unused_io_amount)]
5 use futures::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
6 use std::io::{self, Read};
8 fn question_mark<T: io::Read + io::Write>(s: &mut T) -> io::Result<()> {
10 let mut buf = [0u8; 4];
15 fn unwrap<T: io::Read + io::Write>(s: &mut T) {
16 s.write(b"test").unwrap();
17 let mut buf = [0u8; 4];
18 s.read(&mut buf).unwrap();
21 fn vectored<T: io::Read + io::Write>(s: &mut T) -> io::Result<()> {
22 s.read_vectored(&mut [io::IoSliceMut::new(&mut [])])?;
23 s.write_vectored(&[io::IoSlice::new(&[])])?;
27 fn ok(file: &str) -> Option<()> {
28 let mut reader = std::fs::File::open(file).ok()?;
29 let mut result = [0u8; 0];
30 reader.read(&mut result).ok()?;
34 #[allow(clippy::redundant_closure)]
35 #[allow(clippy::bind_instead_of_map)]
36 fn or_else(file: &str) -> io::Result<()> {
37 let mut reader = std::fs::File::open(file)?;
38 let mut result = [0u8; 0];
39 reader.read(&mut result).or_else(|err| Err(err))?;
48 fn or(file: &str) -> Result<(), Error> {
49 let mut reader = std::fs::File::open(file).unwrap();
50 let mut result = [0u8; 0];
51 reader.read(&mut result).or(Err(Error::Kind))?;
55 fn combine_or(file: &str) -> Result<(), Error> {
56 let mut reader = std::fs::File::open(file).unwrap();
57 let mut result = [0u8; 0];
66 async fn bad_async_write<W: AsyncWrite + Unpin>(w: &mut W) {
67 w.write(b"hello world").await.unwrap();
70 async fn bad_async_read<R: AsyncRead + Unpin>(r: &mut R) {
71 let mut buf = [0u8; 0];
72 r.read(&mut buf[..]).await.unwrap();
75 async fn io_not_ignored_async_write<W: AsyncWrite + Unpin>(mut w: W) {
76 // Here we're forgetting to await the future, so we should get a
77 // warning about _that_ (or we would, if it were enabled), but we
78 // won't get one about ignoring the return value.
79 w.write(b"hello world");
82 fn bad_async_write_closure<W: AsyncWrite + Unpin + 'static>(w: W) -> impl futures::Future<Output = io::Result<()>> {
85 w.write(b"hello world").await?;
90 async fn async_read_nested_or<R: AsyncRead + Unpin>(r: &mut R, do_it: bool) -> Result<[u8; 1], Error> {
91 let mut buf = [0u8; 1];
93 r.read(&mut buf[..]).await.or(Err(Error::Kind))?;
98 use tokio::io::{AsyncRead as TokioAsyncRead, AsyncReadExt as _, AsyncWrite as TokioAsyncWrite, AsyncWriteExt as _};
100 async fn bad_async_write_tokio<W: TokioAsyncWrite + Unpin>(w: &mut W) {
101 w.write(b"hello world").await.unwrap();
104 async fn bad_async_read_tokio<R: TokioAsyncRead + Unpin>(r: &mut R) {
105 let mut buf = [0u8; 0];
106 r.read(&mut buf[..]).await.unwrap();
109 async fn undetected_bad_async_write<W: AsyncWrite + Unpin>(w: &mut W) {
110 // It would be good to detect this case some day, but the current lint
111 // doesn't handle it. (The documentation says that this lint "detects
112 // only common patterns".)
113 let future = w.write(b"Hello world");
114 future.await.unwrap();