Skip to content

Do not flag a type conversion to an error-implementing type as unchecked - #283

Merged
kisielk merged 3 commits into
kisielk:masterfrom
Eljees:fix-typeconv-not-call-230
Oct 5, 2026
Merged

kisielk merged 3 commits into
kisielk:masterfrom
Eljees:fix-typeconv-not-call-230

Conversation

@Eljees

@Eljees Eljees commented Sep 21, 2026

Copy link
Copy Markdown

errorsByArg decides whether a *ast.CallExpr "returns an error" purely from its
result type (*types.Named / *types.Pointer that implements error). Go's AST
represents a type conversion such as (*T)(x) as a CallExpr exactly like a real
function call, so if T happens to implement error, the conversion's result type
satisfies the error check and gets flagged as an unchecked error -- even though
nothing was called and there is no error return to check.

A common real-world example is a compile-time interface-satisfaction check:

var _ error = (*ValidationError)(nil)

Fix

Guard errorsByArg with v.typesInfo.Types[call.Fun].IsType(), the standard way to
tell a type conversion apart from a call in go/types-based tooling (used the same
way by gopls and similar analyses). When call.Fun denotes a type, the expression
is a conversion, not a call, and is skipped.

Test

Added a case to errcheck/testdata/src/blank/main.go (checked via analysistest
with -blank) reproducing the reporter's snippet: a struct implementing error,
and var _ error = (*T)(nil) at package scope with no // want annotation, since
no diagnostic should fire there.

Verification

Confirmed the added test fails with exactly the reported false positive
(unexpected diagnostic: unchecked error) when the guard is removed, and passes
with it in place. Full go test ./... is green both before and after, aside from
the target test itself.

Fixes #230

…e as an unchecked error

errorsByArg decided whether a *ast.CallExpr returns an error purely from its result type. Go's AST represents a type conversion such as (*T)(x) as a CallExpr exactly like a real function call, so a conversion to a type that happens to implement error (e.g. the common compile-time check var _ error = (*T)(nil)) was flagged as an unchecked error, even though nothing was called.

Guard errorsByArg with typesInfo.Types[call.Fun].IsType(), the standard way to tell a type conversion apart from a call in go/types-based tooling (the same technique gopls uses).

Fixes kisielk#230
Comment thread errcheck/errcheck.go Resolved
Comment thread errcheck/testdata/src/blank/main.go Resolved
Eljees and others added 2 commits October 5, 2026 08:40
Replaces the internal ISSUE230_FIX_MARKER prefix with a plain comment and a
link to kisielk#230, and does the same in
the blank testdata case. No behaviour change.

Signed-off-by: Eljees <[email protected]>
@kisielk
kisielk merged commit d9a1079 into kisielk:master Oct 5, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error return value is not checked (errcheck) for interface assertion

2 participants