]> git.lizzy.rs Git - rust.git/blob - tests/ui/unused_io_amount.rs
Auto merge of #8374 - Alexendoo:bless-revisions, r=camsteffen
[rust.git] / tests / ui / unused_io_amount.rs
1 #![allow(dead_code)]
2 #![warn(clippy::unused_io_amount)]
3
4 extern crate futures;
5 use futures::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
6 use std::io::{self, Read};
7
8 fn question_mark<T: io::Read + io::Write>(s: &mut T) -> io::Result<()> {
9     s.write(b"test")?;
10     let mut buf = [0u8; 4];
11     s.read(&mut buf)?;
12     Ok(())
13 }
14
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();
19 }
20
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(&[])])?;
24     Ok(())
25 }
26
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()?;
31     Some(())
32 }
33
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))?;
40     Ok(())
41 }
42
43 #[derive(Debug)]
44 enum Error {
45     Kind,
46 }
47
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))?;
52     Ok(())
53 }
54
55 fn combine_or(file: &str) -> Result<(), Error> {
56     let mut reader = std::fs::File::open(file).unwrap();
57     let mut result = [0u8; 0];
58     reader
59         .read(&mut result)
60         .or(Err(Error::Kind))
61         .or(Err(Error::Kind))
62         .expect("error");
63     Ok(())
64 }
65
66 async fn bad_async_write<W: AsyncWrite + Unpin>(w: &mut W) {
67     w.write(b"hello world").await.unwrap();
68 }
69
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();
73 }
74
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");
80 }
81
82 fn bad_async_write_closure<W: AsyncWrite + Unpin + 'static>(w: W) -> impl futures::Future<Output = io::Result<()>> {
83     let mut w = w;
84     async move {
85         w.write(b"hello world").await?;
86         Ok(())
87     }
88 }
89
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];
92     if do_it {
93         r.read(&mut buf[..]).await.or(Err(Error::Kind))?;
94     }
95     Ok(buf)
96 }
97
98 use tokio::io::{AsyncRead as TokioAsyncRead, AsyncReadExt as _, AsyncWrite as TokioAsyncWrite, AsyncWriteExt as _};
99
100 async fn bad_async_write_tokio<W: TokioAsyncWrite + Unpin>(w: &mut W) {
101     w.write(b"hello world").await.unwrap();
102 }
103
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();
107 }
108
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();
115 }
116
117 fn main() {}