]> git.lizzy.rs Git - rust.git/blob - src/docs/read_zero_byte_vec.txt
Auto merge of #9421 - xphoniex:fix-#9420, r=giraffate
[rust.git] / src / docs / read_zero_byte_vec.txt
1 ### What it does
2 This lint catches reads into a zero-length `Vec`.
3 Especially in the case of a call to `with_capacity`, this lint warns that read
4 gets the number of bytes from the `Vec`'s length, not its capacity.
5
6 ### Why is this bad?
7 Reading zero bytes is almost certainly not the intended behavior.
8
9 ### Known problems
10 In theory, a very unusual read implementation could assign some semantic meaning
11 to zero-byte reads. But it seems exceptionally unlikely that code intending to do
12 a zero-byte read would allocate a `Vec` for it.
13
14 ### Example
15 ```
16 use std::io;
17 fn foo<F: io::Read>(mut f: F) {
18     let mut data = Vec::with_capacity(100);
19     f.read(&mut data).unwrap();
20 }
21 ```
22 Use instead:
23 ```
24 use std::io;
25 fn foo<F: io::Read>(mut f: F) {
26     let mut data = Vec::with_capacity(100);
27     data.resize(100, 0);
28     f.read(&mut data).unwrap();
29 }
30 ```