rust-lang / rust-lang/rust-clippy

New Lint Request: BufWriter must be flushed before being dropped

Open
#2,467 2 comments 7 reactions 1 assignee View on GitHub

@lukaslueg is already working on this.

Since Apr 22, 2023.

A-lint E-medium L-correctness
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

fn run() -> io::Result<()> {
  let mut buf = BufWriter::new(File::open("src/main.rs")?);
  writeln!(buf, "foo")?;
  Ok(())
}

should be

fn run() -> io::Result<()> {
  let mut buf = BufWriter::new(File::open("src/main.rs")?);
  writeln!(buf, "foo")?;
  buf.flush()?;
  Ok(())
}

File::open returns read-only File instance, so these fns should return Err.
But the former fn silently returns Ok because BufWriter::drop ignores flush errors.
https://play.rust-lang.org/?gist=1efc55d8a54d5b9eea25132d085c478c&version=stable

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.