about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/unused_io_amount.rs
blob: 32a50375806a3b736c8c229a67e28c8e7eab4934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#![allow(dead_code, clippy::needless_pass_by_ref_mut)]
#![allow(clippy::redundant_pattern_matching)]
#![warn(clippy::unused_io_amount)]

extern crate futures;
use futures::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use std::io::{self, Read};

fn question_mark<T: io::Read + io::Write>(s: &mut T) -> io::Result<()> {
    s.write(b"test")?;
    //~^ unused_io_amount
    let mut buf = [0u8; 4];
    s.read(&mut buf)?;
    //~^ unused_io_amount
    Ok(())
}

fn unwrap<T: io::Read + io::Write>(s: &mut T) {
    s.write(b"test").unwrap();
    //~^ unused_io_amount
    let mut buf = [0u8; 4];
    s.read(&mut buf).unwrap();
    //~^ unused_io_amount
}

fn vectored<T: io::Read + io::Write>(s: &mut T) -> io::Result<()> {
    s.read_vectored(&mut [io::IoSliceMut::new(&mut [])])?;
    //~^ unused_io_amount
    s.write_vectored(&[io::IoSlice::new(&[])])?;
    //~^ unused_io_amount
    Ok(())
}

fn ok(file: &str) -> Option<()> {
    let mut reader = std::fs::File::open(file).ok()?;
    let mut result = [0u8; 0];
    reader.read(&mut result).ok()?;
    //~^ unused_io_amount
    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))?;
    //~^ unused_io_amount
    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))?;
    //~^ unused_io_amount
    Ok(())
}

fn combine_or(file: &str) -> Result<(), Error> {
    let mut reader = std::fs::File::open(file).unwrap();
    let mut result = [0u8; 0];
    reader
        //~^ unused_io_amount
        .read(&mut result)
        .or(Err(Error::Kind))
        .or(Err(Error::Kind))
        .expect("error");
    Ok(())
}

fn is_ok_err<T: io::Read + io::Write>(s: &mut T) {
    s.write(b"ok").is_ok();
    //~^ unused_io_amount
    s.write(b"err").is_err();
    //~^ unused_io_amount
    let mut buf = [0u8; 0];
    s.read(&mut buf).is_ok();
    //~^ unused_io_amount
    s.read(&mut buf).is_err();
    //~^ unused_io_amount
}

async fn bad_async_write<W: AsyncWrite + Unpin>(w: &mut W) {
    w.write(b"hello world").await.unwrap();
    //~^ unused_io_amount
}

async fn bad_async_read<R: AsyncRead + Unpin>(r: &mut R) {
    let mut buf = [0u8; 0];
    r.read(&mut buf[..]).await.unwrap();
    //~^ unused_io_amount
}

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");
    //~^ unused_io_amount
}

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?;
        //~^ unused_io_amount
        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))?;
        //~^ unused_io_amount
    }
    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();
    //~^ unused_io_amount
}

async fn bad_async_read_tokio<R: TokioAsyncRead + Unpin>(r: &mut R) {
    let mut buf = [0u8; 0];
    r.read(&mut buf[..]).await.unwrap();
    //~^ unused_io_amount
}

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();
}

fn match_okay_underscore<T: io::Read + io::Write>(s: &mut T) {
    match s.write(b"test") {
        //~^ unused_io_amount
        Ok(_) => todo!(),
        Err(_) => todo!(),
    };

    let mut buf = [0u8; 4];
    match s.read(&mut buf) {
        //~^ unused_io_amount
        Ok(_) => todo!(),
        Err(_) => todo!(),
    }
}

fn match_okay_underscore_read_expr<T: io::Read + io::Write>(s: &mut T) {
    match s.read(&mut [0u8; 4]) {
        //~^ unused_io_amount
        Ok(_) => todo!(),
        Err(_) => todo!(),
    }
}

fn match_okay_underscore_write_expr<T: io::Read + io::Write>(s: &mut T) {
    match s.write(b"test") {
        //~^ unused_io_amount
        Ok(_) => todo!(),
        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]) {
        //~^ unused_io_amount
        todo!()
    }
}

fn if_okay_underscore_write_expr<T: io::Read + io::Write>(s: &mut T) {
    if let Ok(_) = s.write(b"test") {
        //~^ unused_io_amount
        todo!()
    }
}

fn if_okay_dots_write_expr<T: io::Read + io::Write>(s: &mut T) {
    if let Ok(..) = s.write(b"test") {
        //~^ unused_io_amount
        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)
}

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!(),
        }
    }
}
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();
}

struct Reader {}

impl Read for Reader {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        todo!()
    }

    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
        // We shouldn't recommend using Read::read_exact inside Read::read_exact!
        self.read(buf).unwrap();
        Ok(())
    }
}

fn main() {}