This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
29
pkg/httpstatus/testdata/src/configured/configured.go.golden
vendored
Normal file
29
pkg/httpstatus/testdata/src/configured/configured.go.golden
vendored
Normal 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)
|
||||
}
|
||||
22
pkg/httpstatus/testdata/src/default/default.go.golden
vendored
Normal file
22
pkg/httpstatus/testdata/src/default/default.go.golden
vendored
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user