1
0
Fork 0
mirror of https://github.com/imjasonh/ctxhttp-analyzer synced 2026-07-06 22:12:38 +00:00
No description
Find a file
Jason Hall 742f21c993 Initial implementation of ctxhttp-analyzer
Go analyzer that detects net/http calls without context and rewrites
them to use http.NewRequestWithContext. Handles http.Get/Post/Head/PostForm,
client methods, and injects context at function boundaries (main, tests,
HTTP handlers).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-25 10:51:39 -04:00
testdata/src Initial implementation of ctxhttp-analyzer 2026-03-25 10:51:39 -04:00
analyzer.go Initial implementation of ctxhttp-analyzer 2026-03-25 10:51:39 -04:00
analyzer_test.go Initial implementation of ctxhttp-analyzer 2026-03-25 10:51:39 -04:00
go.mod Initial implementation of ctxhttp-analyzer 2026-03-25 10:51:39 -04:00
go.sum Initial implementation of ctxhttp-analyzer 2026-03-25 10:51:39 -04:00
lessons.md Initial implementation of ctxhttp-analyzer 2026-03-25 10:51:39 -04:00
LICENSE Initial implementation of ctxhttp-analyzer 2026-03-25 10:51:39 -04:00
main.go Initial implementation of ctxhttp-analyzer 2026-03-25 10:51:39 -04:00
plan.md Initial implementation of ctxhttp-analyzer 2026-03-25 10:51:39 -04:00
README.md Initial implementation of ctxhttp-analyzer 2026-03-25 10:51:39 -04:00

ctxhttp-analyzer

A Go analyzer that detects net/http calls without context and rewrites them to use http.NewRequestWithContext.

Install

go install github.com/imjasonh/ctxhttp-analyzer@latest

Usage

# Report issues
ctxhttp-analyzer ./...

# Auto-fix
ctxhttp-analyzer -fix ./...

# As a go vet tool
go vet -vettool=$(which ctxhttp-analyzer) ./...

What it detects

  • http.NewRequesthttp.NewRequestWithContext
  • http.Get, http.Head, http.Post, http.PostFormhttp.NewRequestWithContext + http.DefaultClient.Do
  • client.Get, client.Head, client.Post, client.PostFormhttp.NewRequestWithContext + client.Do

When a function doesn't already have a context.Context parameter, the analyzer injects one based on the function type:

Function Injected context
Has context.Context param Uses it (by name)
main, init ctx := context.Background()
TestXxx(t), BenchmarkXxx(b) ctx := t.Context()
HTTP handler (w, r) ctx := r.Context()
Anything else ctx := context.TODO()

Limitations

  • Fixes are single-package. Callers in other packages that need ctx plumbed through will get compile errors — fix those by hand.
  • Multi-statement rewrites generate return err in error checks, which may not match the enclosing function's return signature.