A file is opened and never closed
What it finds
f, err := os.Open(path) in a function that never calls f.Close(): the descriptor stays open until the garbage collector happens to run the file's finalizer, so a program that opens files in a loop can run out of descriptors, and data written to a file may not reach the disk before the program ends.
The call must be Open, Create, OpenFile or CreateTemp of os, resolved through the imports, and its first result must go to a local variable. The function must not call Close on the variable, and the variable must not leave the function: a file that is returned, stored or passed to another function may be closed elsewhere.
From the rule's source, file_not_closed.rs.
package p import ( "io" "os") func f(path string) ([]byte, error) { file, err := os.Open(path)tx0 reports: the file file from os.Open is never closed if err != nil { return nil, err } return io.ReadAll(file)} func g(path string) error { out, err := os.Create(path) if err != nil { return err } defer out.Close() _, err = out.WriteString("x") return err} func h(path string) (*os.File, error) { f, err := os.Open(path) if err != nil { return nil, err } return f, nil} 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:file-not-closed" = "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:file-not-closed] reason="..."tx0 explain tx0/go:file-not-closed shows the same in the terminal. Found a false positive? Report it.