rust-lang / rust-lang/rust-clippy

Lint idea: Warn about unflushed writer

Open
#3,415 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Let's say that I have a function that creates a tarball and sends it somewhere.

/// Send a directory as an archive.
fn send_archive<P, Q>(&mut self, source_path: P, target_dir: &str, tar_path: Q) -> MyResult<()>
    where P: AsRef<Path>, Q: AsRef<Path>
{
    let tar_gz = File::create(tar_path.as_ref())?;
    let encoder = GzEncoder::new(tar_gz, Compression::default());
    let mut tar = TarBuilder::new(encoder);
    tar.append_dir_all(target_dir, source_path)?;
    // sends file to storage ...
    self.send_file(Content::from_file(tar_path), target_dir)?;
    Ok(())
}

Here, TarBuilder is a writer. Idiomatically, we don't explicitly flush writers because they auto-flush on drop, but forgetting this could lead us to an abysmal debugging of why we keep getting incomplete stuff. (I was stupid, I suspected SSH, SFTP and even the object storage API (in send_file call), but I never suspected this).

The obvious solution is to wrap the writer into another block or doing an explicit flush, although I'm wondering if there's a nice solution to this. I guess it's possible to identify writers in lints, but how do we determine the span of the block for the writer, or where clippy should suggest adding a flush call? Could it be the last called method to the writer? At the same time, we could skip the suggestion if the last method call ends with the function.

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.

Research direction

Start with the TarBuilder example in the issue and determine how a lint could identify a writer that is used before it is flushed or dropped. Define the supported cases, diagnostic location, and suggestion behavior, including the function-ending and explicit-flush cases; completion requires a decided scope and corresponding lint tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.