Impossible Type Assertion

ID

go.impossible_type_assertion

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Go

Tags

reliability, suspicious

Description

Reports a type assertion x.(T) where the static interface type of x and the asserted interface T declare a method with the same name but incompatible signatures. No concrete type can implement both, so the assertion can never succeed.

Rationale

If Source declares Foo() int and Target declares Foo() string, a single type cannot satisfy both — implementing one excludes the other. The single-result form x.(Target) therefore always panics and the two-result form always returns ok == false. The check is conservative: it requires both the source and asserted types to resolve to interfaces declared in the same file and a shared method name with differing signatures. Disjoint method names (where some type can satisfy both) are never flagged.

type Source interface { Foo() int }
type Target interface { Foo() string }
type Other  interface { Bar() int }

_ = s.(Target) // FLAW — Foo signatures conflict; no type implements both
_ = s.(Other)  // OK — disjoint methods; a type can implement both

Remediation

Assert to an interface that is compatible with the source, or rework the interfaces so the conflicting method has a single, consistent signature.