The program exits from a function after a defer, so the deferred call never runs
What it finds
defer f.Close() followed later by log.Fatal(err) or os.Exit(1): exiting ends the program on the spot, without running deferred calls, so files are not flushed, locks and temporary files are left behind. Returning an error to the caller, or exiting from main after a function that did the work has returned, runs them.
The exit must be a call of os.Exit or of Fatal, Fatalf or Fatalln of log, resolved through the imports, in the body of a function after a defer statement of the same function, neither of them inside a function literal.
From the rule's source, exit_skips_defer.rs.
package p import ( "log" "os") func run(path string) { f, err := os.Create(path) if err != nil { log.Fatal(err) } defer f.Close() if _, err := f.WriteString("x"); err != nil { log.Fatalf("write: %v", err)tx0 reports: log.Fatalf ends the program without running the deferred calls } go func() { os.Exit(2) }()} func main() { if len(os.Args) < 2 { os.Exit(2) } run(os.Args[1])} 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:exit-skips-defer" = "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:exit-skips-defer] reason="..."tx0 explain tx0/go:exit-skips-defer shows the same in the terminal. Found a false positive? Report it.