From b1ad941f4d658d792178c4e2eab3d6bc9b95140d Mon Sep 17 00:00:00 2001 From: ~wispem-wantex Date: Sun, 18 Oct 2026 16:10:55 -0700 Subject: [PATCH] linters: add autofix for httpstatus --- pkg/httpstatus/analyzer.go | 16 +++++++++- pkg/httpstatus/analyzer_test.go | 4 +-- .../src/configured/configured.go.golden | 29 +++++++++++++++++++ .../testdata/src/default/default.go.golden | 22 ++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 pkg/httpstatus/testdata/src/configured/configured.go.golden create mode 100644 pkg/httpstatus/testdata/src/default/default.go.golden diff --git a/pkg/httpstatus/analyzer.go b/pkg/httpstatus/analyzer.go index a4732de..0ed794a 100644 --- a/pkg/httpstatus/analyzer.go +++ b/pkg/httpstatus/analyzer.go @@ -2,9 +2,11 @@ package httpstatus import ( + "fmt" "go/ast" "go/constant" "go/types" + "strconv" "strings" "golang.org/x/tools/go/analysis" @@ -62,7 +64,19 @@ func NewAnalyzer(settings Settings) *analysis.Analyzer { return } - pass.Reportf(node.Pos(), "HTTP status code %d should use its integer literal", code) + pass.Report(analysis.Diagnostic{ + Pos: node.Pos(), + End: node.End(), + Message: fmt.Sprintf("HTTP status code %d should use its integer literal", code), + SuggestedFixes: []analysis.SuggestedFix{{ + Message: "Use the integer literal", + TextEdits: []analysis.TextEdit{{ + Pos: node.Pos(), + End: node.End(), + NewText: []byte(strconv.Itoa(code)), + }}, + }}, + }) } // Disallows sending non-typical response statuses diff --git a/pkg/httpstatus/analyzer_test.go b/pkg/httpstatus/analyzer_test.go index 7ecde10..a5a471a 100644 --- a/pkg/httpstatus/analyzer_test.go +++ b/pkg/httpstatus/analyzer_test.go @@ -9,7 +9,7 @@ import ( ) func TestAnalyzer(t *testing.T) { - analysistest.Run(t, analysistest.TestData(), httpstatus.Analyzer, "default") + analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), httpstatus.Analyzer, "default") } func TestAnalyzerWithSettings(t *testing.T) { @@ -18,5 +18,5 @@ func TestAnalyzerWithSettings(t *testing.T) { IsRestricted: true, }) - analysistest.Run(t, analysistest.TestData(), analyzer, "configured") + analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), analyzer, "configured") } diff --git a/pkg/httpstatus/testdata/src/configured/configured.go.golden b/pkg/httpstatus/testdata/src/configured/configured.go.golden new file mode 100644 index 0000000..f85f7bf --- /dev/null +++ b/pkg/httpstatus/testdata/src/configured/configured.go.golden @@ -0,0 +1,29 @@ +package configured + +import "net/http" + +const created = 201 // want `HTTP status code 201 should use its integer literal` +const ok = http.StatusOK + +func response(w http.ResponseWriter, r *http.Request, code int) { + http.Error(w, "teapot", 418) // want `non-typical HTTP status code 418 is not allowed` + http.Error(w, "created", 201) + http.Error(w, "variable", code) + + w.WriteHeader(200) // want `non-typical HTTP status code 200 is not allowed` + w.WriteHeader(code) + http.ResponseWriter.WriteHeader(w, 400) // want `non-typical HTTP status code 400 is not allowed` + + http.NotFound(w, r) + _, _ = w.Write([]byte("response")) + http.Redirect(w, r, "/moved", 301) + http.Redirect(w, r, "/created", 201) // want `HTTP status code 201 should use its integer literal` +} + +type unrelatedWriter struct{} + +func (unrelatedWriter) WriteHeader(int) {} + +func unrelated(writer unrelatedWriter) { + writer.WriteHeader(418) +} diff --git a/pkg/httpstatus/testdata/src/default/default.go.golden b/pkg/httpstatus/testdata/src/default/default.go.golden new file mode 100644 index 0000000..38e0c9f --- /dev/null +++ b/pkg/httpstatus/testdata/src/default/default.go.golden @@ -0,0 +1,22 @@ +package cases + +import "net/http" + +const ok = 200 // want `HTTP status code 200 should use its integer literal` +const teapot = http.StatusTeapot + +func response(w http.ResponseWriter, r *http.Request, code int) { + _ = 404 // want `HTTP status code 404 should use its integer literal` + + http.Error(w, "bad request", 400) // want `HTTP status code 400 should use its integer literal` + http.Error(w, "teapot", http.StatusTeapot) + http.Error(w, "created", 201) + w.WriteHeader(504) // want `HTTP status code 504 should use its integer literal` + w.WriteHeader(code) + + http.NotFound(w, r) + _, _ = w.Write([]byte("response")) + + http.Redirect(w, r, "/login", 302) // want `HTTP status code 302 should use its integer literal` + http.Redirect(w, r, "/other", http.StatusMovedPermanently) +}