A buffering or compressing writer is never flushed or closed, so its last bytes are lost
What it finds
w := bufio.NewWriter(f) in a function that never calls w.Flush(): the writer keeps what it is given in a buffer and writes it out only when the buffer is full, so whatever is in the buffer at the end never arrives. A compressing writer of compress/gzip, compress/zlib, compress/flate or archive/zip writes its last block and trailer only in Close.
The writer must be made by NewWriter or NewWriterSize of bufio, NewWriter of encoding/csv or text/tabwriter, which need Flush, or by NewWriter or NewWriterLevel of the compressing packages, which need Close, and go to a local variable. The function must not call the method the writer needs, and the variable must not leave the function.
From the rule's source, writer_not_flushed.rs.
package p import ( "bufio" "compress/gzip" "io") func f(out io.Writer, lines []string) { w := bufio.NewWriter(out)tx0 reports: w is never given Flush, so what it still holds is lost for _, l := range lines { w.WriteString(l) }} func g(out io.Writer, data []byte) error { zw := gzip.NewWriter(out)tx0 reports: zw is never given Close, so what it still holds is lost _, err := zw.Write(data) return err} func h(out io.Writer, data []byte) error { w := bufio.NewWriter(out) if _, err := w.Write(data); err != nil { return err } return w.Flush()} func wrap(out io.Writer) *bufio.Writer { w := bufio.NewWriter(out) return w} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/go:writer-not-flushed" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/go:writer-not-flushed] reason="..."tx0 explain tx0/go:writer-not-flushed shows the same in the terminal. Found a false positive? Report it.