rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Lint unused written length returned by `io::Write::write`

Open sinkuu opened this issue 8 years ago • 5 comments

It's possible that this will cause partial write (depending on what W is), which may not be intended:

use std::io;

fn write_foo<W: io::Write>(w: &mut W) -> io::Result<()> {
    w.write(b"foo")?;
    Ok(())
}

This should be changed to either

fn write_foo<W: io::Write>(w: &mut W) -> io::Result<()> {
    let written = w.write(b"foo")?;
    // ... handle written
    Ok(())
}

or

fn write_foo<W: io::Write>(w: &mut W) -> io::Result<()> {
    w.write_all(b"foo")?;
    Ok(())
}

sinkuu avatar Nov 30 '16 01:11 sinkuu

I'm all for a general lint on expr; that has a type other than (). But your case is special, I think it even should be deny by default, since it's obviously a bug.

oli-obk avatar Nov 30 '16 07:11 oli-obk

Unused result of Read::read should also be linted as well and suggest to use `read_exact".

sinkuu avatar Dec 04 '16 03:12 sinkuu

To fully close this we probably need to use the dataflow analysis of MIR to prove that the write/read count is either used for a decision (to error or to retry, or is "used" good enough?) or escapes the function (by writing to a reference arg or by returning the value).

In case of escaping, the function needs to be marked so it is treated just like read/write. Since that is not feasible, we can require the function to have a #[io_amount_ret] or #[io_amount_ref(arg_name)] attribute that we collect when the lint is in check_crate and thus tread calls to that function just like read/write calls.

oli-obk avatar Jan 09 '17 08:01 oli-obk

@oli-obk I think first approximation considering escaped value as used would be already very useful. I've witnessed multiple bugs that would be caught by that and don't remember ever seeing a bug that wouldn't. The only common escaping of the value is within Read/Write impl when delegating (self.0.read()) and since that is already considered in lint it's non-issue.

Kixunil avatar Jul 22 '22 07:07 Kixunil

Oh, it's already implemented, I thought it wasn't because I hit a false negative: #9226. :sweat_smile:

Kixunil avatar Jul 22 '22 08:07 Kixunil