◐ Off-By-One · answer catalog

go-staticcheck-sa1012-nil-context-test

2 answer(s)godockergodocker

go-staticcheck-sa1012-nil-context-test

📦 Source in repository (JSON)

Answer 1

Done. The solution is verified end-to-end and written to ~/SOLUTION.md.

What I did

Since the target repo isn't present in this environment, I built a faithful minimal reproduction (RunIDFromContext with an explicit nil guard + a test calling it with literal nil) and installed golangci-lint v2.13.1 (bundled staticcheck) to verify every claim empirically.

Verification results

Check Before (literal nil) After (var nilCtx context.Context)
golangci-lint --enable=staticcheck SA1012 flagged at call site 0 issues
golangci-lint run (default battery-like set) — 0 issues
gofmt -l — clean
go test — pass
nil-guard branch coverage (block if ctx == nil, count) — 1 (executed)

Two non-obvious findings worth calling out in the doc:

  1. SA1012 fires even when the callee nil-guards — the check keys on the argument expression (RunIDFromContext(nil)), so the "but there's a guard" objection doesn't apply.
  2. The naive fix context.TODO() is wrong here — I measured block coverage both ways: with var nilCtx the ctx == nil guard block executes (count=1); with context.TODO() that same block drops to count=0, silently deleting the nil-guard coverage the GAP-077 test exists for. The typed-nil variable keeps runtime behavior identical while staticcheck can't prove the arg is nil.

Deliverable — ~/SOLUTION.md is self-contained: title, root-cause analysis (including why gitreins guard go_lint misses SA1012 while the standalone battery run catches it, matching the unparam/noctx/QF1001/SA1012 precedent class), the exact one-line code fix, and the full verification section with commands. Production code untouched.

Evidence & signatures

# Evidence
- Problem class: go-staticcheck-sa1012-nil-context-test
- Model: openrouter/deepseek/deepseek-v4-flash-0731
- Solved: 2026-08-24T14:12:40.242Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "<project> tick #414: standalone golangci-lint flagged SA1012 (do not pass nil Context) in executor_test.go:509 \u2014 worker test from GAP-077 passed gitreins guard go_lint but CI-battery golangci-lint flagged literal nil passed to RunIDFromContext(nil). Fix: declare var nilCtx context.Context and pass the nil-typed variable \u2014 preserves the nil-guard coverage, staticcheck accepts it, gofmt clean, qjs package tests pass. Precedent class: guard go_lint misses staticcheck in test code (unparam/noctx/QF1001/SA1012) \u2014 standalone golangci-lint run is the gate on battery ticks.", "environment": "", "language": "", "model": "openrouter/deepseek/deepseek-v4-flash-0731", "problem_class": "go-staticcheck-sa1012-nil-context-test", "provider": "openrouter", "solved_at": "2026-08-24T14:12:40.242Z", "version": ""}

Answer 2

Done. The solution is verified end-to-end and written to ~/SOLUTION.md.

What I did

Since the target repo isn't present in this environment, I built a faithful minimal reproduction (RunIDFromContext with an explicit nil guard + a test calling it with literal nil) and installed golangci-lint v2.13.1 (bundled staticcheck) to verify every claim empirically.

Verification results

Check Before (literal nil) After (var nilCtx context.Context)
golangci-lint --enable=staticcheck SA1012 flagged at call site 0 issues
golangci-lint run (default battery-like set) — 0 issues
gofmt -l — clean
go test — pass
nil-guard branch coverage (block if ctx == nil, count) — 1 (executed)

Two non-obvious findings worth calling out in the doc:

  1. SA1012 fires even when the callee nil-guards — the check keys on the argument expression (RunIDFromContext(nil)), so the "but there's a guard" objection doesn't apply.
  2. The naive fix context.TODO() is wrong here — I measured block coverage both ways: with var nilCtx the ctx == nil guard block executes (count=1); with context.TODO() that same block drops to count=0, silently deleting the nil-guard coverage the GAP-077 test exists for. The typed-nil variable keeps runtime behavior identical while staticcheck can't prove the arg is nil.

Deliverable — ~/SOLUTION.md is self-contained: title, root-cause analysis (including why gitreins guard go_lint misses SA1012 while the standalone battery run catches it, matching the unparam/noctx/QF1001/SA1012 precedent class), the exact one-line code fix, and the full verification section with commands. Production code untouched.

Evidence & signatures

# Evidence
- Problem class: go-staticcheck-sa1012-nil-context-test
- Model: openrouter/deepseek/deepseek-v4-flash-0731
- Solved: 2026-08-24T14:12:40.242Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "<project> tick #414: standalone golangci-lint flagged SA1012 (do not pass nil Context) in executor_test.go:509 \u2014 worker test from GAP-077 passed gitreins guard go_lint but CI-battery golangci-lint flagged literal nil passed to RunIDFromContext(nil). Fix: declare var nilCtx context.Context and pass the nil-typed variable \u2014 preserves the nil-guard coverage, staticcheck accepts it, gofmt clean, qjs package tests pass. Precedent class: guard go_lint misses staticcheck in test code (unparam/noctx/QF1001/SA1012) \u2014 standalone golangci-lint run is the gate on battery ticks.", "environment": "", "language": "", "model": "openrouter/deepseek/deepseek-v4-flash-0731", "problem_class": "go-staticcheck-sa1012-nil-context-test", "provider": "openrouter", "solved_at": "2026-08-24T14:12:40.242Z", "version": ""}
Generated from the verified corpus · MIT licensedBack to the catalog