8000 stylecheck: preserve HTTP import aliases in ST1013 fixes by dajiaohuang · Pull Request #1759 · dominikh/go-tools · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions stylecheck/st1013/st1013.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func run(pass *analysis.Pass) (any, error) {
for _, code := range config.For(pass).HTTPStatusCodeWhitelist {
whitelist[code] = true
}
for _, m := range code.Matches(pass, query) {
for node, m := range code.Matches(pass, query) {
var arg int
switch m.State["name"].(string) {
case "net/http.Error":
Expand Down Expand Up @@ -89,9 +89,16 @@ func run(pass *analysis.Pass) (any, error) {
continue
}
lit := args[arg]
report.Report(pass, lit, fmt.Sprintf("should use constant http.%s instead of numeric literal %d", s, n),
report.FilterGenerated(),
report.Fixes(edit.Fix(fmt.Sprintf("Use http.%s instead of %d", s, n), edit.ReplaceWithString(lit, "http."+s))))
options := []report.Option{report.FilterGenerated()}
if call, ok := node.(*ast.CallExpr); ok {
if sel, ok := call.Fun.(*ast.SelectorExpr); ok {
if pkg, ok := sel.X.(*ast.Ident); ok {
name := pkg.Name + "." + s
options = append(options, report.Fixes(edit.Fix(fmt.Sprintf("Use %s instead of %d", name, n), edit.ReplaceWithString(lit, name))))
}
}
}
report.Report(pass, lit, fmt.Sprintf("should use constant http.%s instead of numeric literal %d", s, n), options...)
}
return nil, nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Package pkg ...
package pkg

import httpalias "net/http"

func fn() {
// Check all the supported functions
httpalias.Error(nil, "", 506) //@ diag(`http.StatusVariantAlsoNegotiates`)
httpalias.Redirect(nil, nil, "", 506) //@ diag(`http.StatusVariantAlsoNegotiates`)
httpalias.StatusText(506) //@ diag(`http.StatusVariantAlsoNegotiates`)
httpalias.RedirectHandler("", 506) //@ diag(`http.StatusVariantAlsoNegotiates`)

// Don't flag literals with no known constant
httpalias.StatusText(600)

// Don't flag constants
httpalias.StatusText(httpalias.StatusAccepted)

// Don't flag items on the whitelist (well known codes)
httpalias.StatusText(404)

httpalias.Error(fn2())
}

func fn2() (httpalias.ResponseWriter, string, int) { return nil, "", 0 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Package pkg ...
package pkg

import httpalias "net/http"

func fn() {
// Check all the supported functions
httpalias.Error(nil, "", httpalias.StatusVariantAlsoNegotiates) //@ diag(`http.StatusVariantAlsoNegotiates`)
httpalias.Redirect(nil, nil, "", httpalias.StatusVariantAlsoNegotiates) //@ diag(`http.StatusVariantAlsoNegotiates`)
httpalias.StatusText(httpalias.StatusVariantAlsoNegotiates) //@ diag(`http.StatusVariantAlsoNegotiates`)
httpalias.RedirectHandler("", httpalias.StatusVariantAlsoNegotiates) //@ diag(`http.StatusVariantAlsoNegotiates`)

// Don't flag literals with no known constant
httpalias.StatusText(600)

// Don't flag constants
httpalias.StatusText(httpalias.StatusAccepted)

// Don't flag items on the whitelist (well known codes)
httpalias.StatusText(404)

httpalias.Error(fn2())
}

func fn2() (httpalias.ResponseWriter, string, int) { return nil, "", 0 }
0