The else branch of a type assertion uses the new variable, which holds a zero value there

tx0/go:shadowed-assertion-elsedefaultwarningbug

What it finds

``go if v, ok := v.(fmt.Stringer); ok { return v.String() } else { return fmt.Sprint(v) } ` The new v of the if statement is in scope in the else` branch too, where the assertion has failed, so it holds the zero value of the asserted type, here a nil interface, rather than the original value.

Reported is the first use, in an else branch, of a variable that the if statement declares from a two-valued type assertion of a variable of the same name, when the condition is the plain ok variable.

From the rule's source, shadowed_assertion_else.rs.

Example from the rule's tests
package p import "fmt" func f(v any) string {	if v, ok := v.(fmt.Stringer); ok {		return v.String()	} else {		return fmt.Sprint(v)tx0 reports: v here is the zero value of the failed assertion	}} func g(v any) string {	if s, ok := v.(fmt.Stringer); ok {		return s.String()	} else {		return fmt.Sprint(v)	}}
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:shadowed-assertion-else" = "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:shadowed-assertion-else] reason="..."

tx0 explain tx0/go:shadowed-assertion-else shows the same in the terminal. Found a false positive? Report it.