linters: add autofix for httpstatus
All checks were successful
CI / test (push) Successful in 33s

This commit is contained in:
2026-10-18 16:10:55 -07:00
parent 9f10798be1
commit f5b6076dce
4 changed files with 68 additions and 3 deletions

View File

@@ -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

View File

@@ -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")
}

View File

@@ -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)
}

View File

@@ -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)
}