From 234d6f44a3d7b88d5f4f16bf364d54009a415d18 Mon Sep 17 00:00:00 2001 From: Sasha Melentyev Date: Mon, 8 Aug 2022 13:00:28 +0300 Subject: [PATCH] chore: remove http.NoBody check --- pkg/analyzer/analyzer.go | 52 +++---------------- pkg/analyzer/analyzer_test.go | 3 -- pkg/analyzer/internal/gen.go | 6 --- pkg/analyzer/internal/mapping/mapping.go | 4 -- .../internal/template/test-httpnobody.go.tmpl | 17 ------ pkg/analyzer/testdata/src/a/http/method.go | 0 pkg/analyzer/testdata/src/a/http/nobody.go | 13 ----- .../testdata/src/a/http/statuscode.go | 0 8 files changed, 8 insertions(+), 87 deletions(-) delete mode 100644 pkg/analyzer/internal/template/test-httpnobody.go.tmpl mode change 100644 => 100755 pkg/analyzer/testdata/src/a/http/method.go delete mode 100755 pkg/analyzer/testdata/src/a/http/nobody.go mode change 100644 => 100755 pkg/analyzer/testdata/src/a/http/statuscode.go diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go index 8040578..cff928c 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/analyzer/analyzer.go @@ -20,7 +20,6 @@ const ( CryptoHashFlag = "crypto-hash" HTTPMethodFlag = "http-method" HTTPStatusCodeFlag = "http-status-code" - HTTPNoBodyFlag = "http-no-body" DefaultRPCPathFlag = "default-rpc-path" ) @@ -41,7 +40,6 @@ func flags() flag.FlagSet { flags := flag.NewFlagSet("", flag.ExitOnError) flags.Bool(HTTPMethodFlag, true, "suggest the use of http.MethodXX") flags.Bool(HTTPStatusCodeFlag, true, "suggest the use of http.StatusXX") - flags.Bool(HTTPNoBodyFlag, false, "suggest the use of http.NoBody") flags.Bool(TimeWeekdayFlag, false, "suggest the use of time.Weekday") flags.Bool(TimeMonthFlag, false, "suggest the use of time.Month") flags.Bool(TimeLayoutFlag, false, "suggest the use of time.Layout") @@ -79,29 +77,21 @@ func run(pass *analysis.Pass) (interface{}, error) { } case "NewRequest": - if lookupFlag(pass, HTTPMethodFlag) { - if basicLit := getBasicLitFromArgs(n.Args, 3, 0, token.STRING); basicLit != nil { - checkHTTPMethod(pass, basicLit) - } + if !lookupFlag(pass, HTTPMethodFlag) { + return } - if lookupFlag(pass, HTTPNoBodyFlag) { - if ident := getIdentFromArgs(n.Args, 3, 2); ident != nil { - checkHTTPNoBody(pass, ident) - } + if basicLit := getBasicLitFromArgs(n.Args, 3, 0, token.STRING); basicLit != nil { + checkHTTPMethod(pass, basicLit) } case "NewRequestWithContext": - if lookupFlag(pass, HTTPMethodFlag) { - if basicLit := getBasicLitFromArgs(n.Args, 4, 1, token.STRING); basicLit != nil { - checkHTTPMethod(pass, basicLit) - } + if !lookupFlag(pass, HTTPMethodFlag) { + return } - if lookupFlag(pass, HTTPNoBodyFlag) { - if ident := getIdentFromArgs(n.Args, 4, 3); ident != nil { - checkHTTPNoBody(pass, ident) - } + if basicLit := getBasicLitFromArgs(n.Args, 4, 1, token.STRING); basicLit != nil { + checkHTTPMethod(pass, basicLit) } } @@ -208,14 +198,6 @@ func checkHTTPStatusCode(pass *analysis.Pass, basicLit *ast.BasicLit) { } } -func checkHTTPNoBody(pass *analysis.Pass, ident *ast.Ident) { - currentVal := ident.Name - - if newVal, ok := mapping.HTTPNoBody[currentVal]; ok { - report(pass, ident.Pos(), currentVal, newVal) - } -} - func checkTimeWeekday(pass *analysis.Pass, pos token.Pos, currentVal string) { if newVal, ok := mapping.TimeWeekday[currentVal]; ok { report(pass, pos, currentVal, newVal) @@ -269,24 +251,6 @@ func getBasicLitFromArgs(args []ast.Expr, count, idx int, typ token.Token) *ast. return basicLit } -// getIdentFromArgs gets the *ast.Ident of a function argument. -// -// Arguments: -// - count - expected number of argument in function -// - idx - index of the argument to get the *ast.Ident -func getIdentFromArgs(args []ast.Expr, count, idx int) *ast.Ident { - if len(args) != count { - return nil - } - - ident, ok := args[idx].(*ast.Ident) - if !ok { - return nil - } - - return ident -} - // getBasicLitFromElts gets the *ast.BasicLit of a struct elements. // // Arguments: diff --git a/pkg/analyzer/analyzer_test.go b/pkg/analyzer/analyzer_test.go index cc9f3dc..8f13385 100644 --- a/pkg/analyzer/analyzer_test.go +++ b/pkg/analyzer/analyzer_test.go @@ -33,9 +33,6 @@ func TestUseStdlibVars(t *testing.T) { if err := a.Flags.Set(analyzer.DefaultRPCPathFlag, "true"); err != nil { t.Error(err) } - if err := a.Flags.Set(analyzer.HTTPNoBodyFlag, "true"); err != nil { - t.Error(err) - } analysistest.Run(t, analysistest.TestData(), a, pkgs...) } diff --git a/pkg/analyzer/internal/gen.go b/pkg/analyzer/internal/gen.go index 1ae3b93..3855672 100644 --- a/pkg/analyzer/internal/gen.go +++ b/pkg/analyzer/internal/gen.go @@ -83,12 +83,6 @@ func main() { templateName: "test-issue32.go.tmpl", fileName: "pkg/analyzer/testdata/src/a/http/issue32.go", }, - { - mapping: mapping.HTTPNoBody, - packageName: "http_test", - templateName: "test-httpnobody.go.tmpl", - fileName: "pkg/analyzer/testdata/src/a/http/nobody.go", - }, } for _, operation := range operations { diff --git a/pkg/analyzer/internal/mapping/mapping.go b/pkg/analyzer/internal/mapping/mapping.go index 7f53c35..56fae1b 100644 --- a/pkg/analyzer/internal/mapping/mapping.go +++ b/pkg/analyzer/internal/mapping/mapping.go @@ -159,7 +159,3 @@ var TimeLayout = map[string]string{ time.StampMicro: "time.StampMicro", time.StampNano: "time.StampNano", } - -var HTTPNoBody = map[string]string{ - "nil": "http.NoBody", -} diff --git a/pkg/analyzer/internal/template/test-httpnobody.go.tmpl b/pkg/analyzer/internal/template/test-httpnobody.go.tmpl deleted file mode 100644 index c0b2843..0000000 --- a/pkg/analyzer/internal/template/test-httpnobody.go.tmpl +++ /dev/null @@ -1,17 +0,0 @@ -// Code generated by usestdlibvars, DO NOT EDIT. - -package {{ .PackageName }} - -import "net/http" - -func _() { -{{- range $key, $value := .Mapping }} - _, _ = http.NewRequest(http.MethodGet, "", {{ $key }}) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` -{{- end }} -} - -func _() { -{{- range $key, $value := .Mapping }} - _, _ = http.NewRequestWithContext(nil, http.MethodGet, "", {{ $key }}) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` -{{- end }} -} diff --git a/pkg/analyzer/testdata/src/a/http/method.go b/pkg/analyzer/testdata/src/a/http/method.go old mode 100644 new mode 100755 diff --git a/pkg/analyzer/testdata/src/a/http/nobody.go b/pkg/analyzer/testdata/src/a/http/nobody.go deleted file mode 100755 index 9d3a43f..0000000 --- a/pkg/analyzer/testdata/src/a/http/nobody.go +++ /dev/null @@ -1,13 +0,0 @@ -// Code generated by usestdlibvars, DO NOT EDIT. - -package http_test - -import "net/http" - -func _() { - _, _ = http.NewRequest(http.MethodGet, "", nil) // want `"nil" can be replaced by http\.NoBody` -} - -func _() { - _, _ = http.NewRequestWithContext(nil, http.MethodGet, "", nil) // want `"nil" can be replaced by http\.NoBody` -} diff --git a/pkg/analyzer/testdata/src/a/http/statuscode.go b/pkg/analyzer/testdata/src/a/http/statuscode.go old mode 100644 new mode 100755