From 7cbd7e867b843153c8a5ec359951921a1213d7b2 Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Thu, 1 Oct 2026 21:46:07 +0800 Subject: [PATCH] stylecheck: preserve HTTP import aliases in ST1013 fixes --- stylecheck/st1013/st1013.go | 15 ++++++++--- .../CheckHTTPStatusCodesAlias.go | 25 +++++++++++++++++++ .../CheckHTTPStatusCodesAlias.go.golden | 25 +++++++++++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 stylecheck/st1013/testdata/go1.0/CheckHTTPStatusCodesAlias/CheckHTTPStatusCodesAlias.go create mode 100644 stylecheck/st1013/testdata/go1.0/CheckHTTPStatusCodesAlias/CheckHTTPStatusCodesAlias.go.golden diff --git a/stylecheck/st1013/st1013.go b/stylecheck/st1013/st1013.go index 01fe71c7d..04ee67680 100644 --- a/stylecheck/st1013/st1013.go +++ b/stylecheck/st1013/st1013.go @@ -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": @@ -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 } diff --git a/stylecheck/st1013/testdata/go1.0/CheckHTTPStatusCodesAlias/CheckHTTPStatusCodesAlias.go b/stylecheck/st1013/testdata/go1.0/CheckHTTPStatusCodesAlias/CheckHTTPStatusCodesAlias.go new file mode 100644 index 000000000..3c31da5b8 --- /dev/null +++ b/stylecheck/st1013/testdata/go1.0/CheckHTTPStatusCodesAlias/CheckHTTPStatusCodesAlias.go @@ -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 } diff --git a/stylecheck/st1013/testdata/go1.0/CheckHTTPStatusCodesAlias/CheckHTTPStatusCodesAlias.go.golden b/stylecheck/st1013/testdata/go1.0/CheckHTTPStatusCodesAlias/CheckHTTPStatusCodesAlias.go.golden new file mode 100644 index 000000000..7e70c4d00 --- /dev/null +++ b/stylecheck/st1013/testdata/go1.0/CheckHTTPStatusCodesAlias/CheckHTTPStatusCodesAlias.go.golden @@ -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 }