golangci-lint errcheck error: error return value not checked blocks PR
Fixes golangci-lint errcheck failures (error return value not checked) blocking PRs by handling the error or adding a targeted nolint. Use when errcheck flags an unchecked return in CI. Not for other golangci-lint linters.
TL;DR
errcheck fails the PR because a function return value (usually err) is discarded without a check. Wrap the call in an explicit error check, or add a //nolint:errcheck comment with a reason when the error is intentionally ignored.
Error
main.go:42:13: Error return value of `w.Write` is not checked (errcheck)Steps
- Find every errcheck hit: run
golangci-lint run ./...locally and note each file and line. Expected: the sameError return value ... is not checkedlines CI reports. - If the error matters, handle it explicitly:
if _, err := w.Write(b); err != nil { return err }. Expected: errcheck passes and the failure path is no longer silent. - If the error is intentionally ignored (e.g. best-effort logging), annotate with a reason:
//nolint:errcheck // best-effort log write. Expected: errcheck skips the line; the reason documents intent. - For project-wide exclusions of noisy calls, add them to the errcheck
exclude-functionslist in.golangci.yml. Expected:golangci-lint run ./...is clean with no new nolint comments.
When to use
- CI reports
Error return value of ... is not checked (errcheck)and blocks the PR. - The flagged call is in Go code and you control the repository.
When not to use
- The failure comes from a different linter (staticcheck, govet) - those have different fixes.
- You cannot add nolint comments because the project forbids them; handle the error instead.
Tool compatibility
- golangci-lint 1.5x with the errcheck linter enabled.
- Any Go version errcheck supports.
Variant phrasings
error return value of fmt.Sprintf is not checked
Same fix: assign the value or drop the call if the result is unused.
errcheck: defer f.Close() return not checked
Wrap in a closure that checks the error, or nolint with a reason when the close error is irrelevant.
Why it happens
errcheck exists because silently discarded errors hide failures. Go has no exceptions, so an unchecked return is the only signal a failure happened - the linter treats discarding it as a likely bug.
Edge cases
- Blanket
//nolint:errcheckon whole files hides real bugs; keep annotations line-scoped. - Excluding
fmt.Print*style functions in config is fine for CLIs, but keep errcheck on for I/O and network calls. - errcheck does not flag assignments to
_;_ = f()is an explicit (if lazy) acknowledgement.
Provenance
Resolved from the public thread: https://vectle.com/posts/psttRsXQELwXtMovilzBxJ0A