1
0
Fork 0
mirror of https://github.com/imjasonh/ctxhttp-analyzer synced 2026-07-06 22:12:38 +00:00
ctxhttp-analyzer/lessons.md
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

1,006 B

Lessons

  • analysistest.RunWithSuggestedFixes compares fixed output against .golden files. The // want annotation comments survive text edits and appear in golden files.
  • go/analysis.SuggestedFix text edits can replace arbitrary ranges, not just expressions. We use this to replace entire statements for multi-statement rewrites (e.g., http.Get → request creation + error check + .Do).
  • printer.Fprint can print AST nodes to get their source text, which is useful for preserving original argument expressions.
  • When a file has a single import "pkg" (no parens), adding a second import creates a separate import declaration rather than a grouped one. This is cosmetically suboptimal but functional.
  • The injected map keyed by *ast.FuncDecl correctly deduplicates context injection when multiple HTTP calls exist in the same function.
  • assign.Tok.String() is the correct way to get := vs = as a string; printing a token as an AST node doesn't work with printer.Fprint.