mirror of
https://github.com/imjasonh/ctxhttp-analyzer
synced 2026-07-06 22:12:38 +00:00
No description
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> |
||
|---|---|---|
| testdata/src | ||
| analyzer.go | ||
| analyzer_test.go | ||
| go.mod | ||
| go.sum | ||
| lessons.md | ||
| LICENSE | ||
| main.go | ||
| plan.md | ||
| README.md | ||
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.NewRequest→http.NewRequestWithContexthttp.Get,http.Head,http.Post,http.PostForm→http.NewRequestWithContext+http.DefaultClient.Doclient.Get,client.Head,client.Post,client.PostForm→http.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
ctxplumbed through will get compile errors — fix those by hand. - Multi-statement rewrites generate
return errin error checks, which may not match the enclosing function's return signature.