recover is called where it always returns nil and stops no panic

tx0/go:ineffective-recoverdefaultwarningbug

What it finds

recover stops a panic only when a deferred function calls it directly. defer recover() defers recover itself, which the runtime then calls rather than a deferred function, and a function literal that is called on the spot, or started with go, is not deferred at all, so recover in it returns nil and the panic goes on.

Reported are defer recover(), and a call of recover whose nearest enclosing function is a literal that a statement calls right away, without defer, or that a go statement runs. recover must be the builtin.

From the rule's source, ineffective_recover.rs.

Example from the rule's tests
package p func f() {	defer recover()tx0 reports: defer recover() defers recover itself, which then stops no panic	defer func() {		func() {			recover()tx0 reports: recover in a function literal that is called right away always returns nil		}()	}()	go func() {		if r := recover(); r != nil {tx0 reports: recover in a goroutine's own function stops no panic of that goroutine			println(r)		}	}()	defer func() {		if r := recover(); r != nil {			println(r)		}	}()} func handle() {	if r := recover(); r != nil {		println(r)	}}
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:ineffective-recover" = "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:ineffective-recover] reason="..."

tx0 explain tx0/go:ineffective-recover shows the same in the terminal. Found a false positive? Report it.