A call on a result is deferred before the error of the call is checked

tx0/go:defer-before-error-checkdefaultwarningbug

What it finds

resp, err := http.Get(url) directly followed by defer resp.Body.Close(), with err checked only afterwards. When the call fails, its other results are usually nil, so the deferred call panics or acts on a value that is not valid.

The assignment must have a call as its only value and at least two names on the left, the last of which contains err and is compared with nil by a later if of the same block, before any other use. The defer must be the very next statement and call a method reached from the first name through fields only, as in resp.Body.Close; both names are resolved, so they are the variables the assignment sets. A deferred call that merely takes the first result as an argument, such as os.RemoveAll(dir), is not reported, since such functions usually accept a zero value.

From the rule's source, defer_before_error_check.rs.

Example from the rule's tests
package p import (	"database/sql"	"net/http"	"os") func get(url string) error {	resp, err := http.Get(url)	defer resp.Body.Close()tx0 reports: resp.Body.Close is deferred before err is checked	if err != nil {		return err	}	return nil} func query(db *sql.DB) (n int, err error) {	rows, err := db.Query("select 1")	defer rows.Close()tx0 reports: rows.Close is deferred before err is checked	if n = 1; err != nil || n == 0 {		return 0, err	}	var queryErr error	rows, queryErr = db.Query("select 2")	defer rows.Close()tx0 reports: rows.Close is deferred before queryErr is checked	if nil != queryErr {		return 0, queryErr	}	return n, nil} func checkedFirst(url string) error {	resp, err := http.Get(url)	if err != nil {		return err	}	defer resp.Body.Close()	return nil} func zeroValueIsFine() error {	dir, err := os.MkdirTemp("", "p")	defer os.RemoveAll(dir)	if err != nil {		return err	}	return nil} func usedBeforeTheCheck(url string) error {	resp, err := http.Get(url)	defer resp.Body.Close()	log(err)	if err != nil {		return err	}	return nil} func notChecked(url string) {	resp, err := http.Get(url)	defer resp.Body.Close()	_ = err} func notAnError(m map[string]*os.File, name string) {	f, ok := open(name)	defer f.Close()	if ok != nil {		return	}} func shadowed(url string) error {	resp, err := http.Get(url)	{		resp := fallback()		defer resp.Body.Close()		if err != nil {			return err		}	}	_ = resp	return nil} func methodResult(c *http.Client, req *http.Request) error {	resp, err := c.Do(req)	defer resp.Close()tx0 reports: resp.Close is deferred before err is checked	if err != nil {		return err	}	return nil} func log(error)                    {}func open(string) (*os.File, *int) { return nil, nil }func fallback() *http.Response     { return nil }
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/go:defer-before-error-check" = "off"

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:defer-before-error-check] reason="..."

tx0 explain tx0/go:defer-before-error-check shows the same in the terminal. Found a false positive? Report it.