Skip to content

Commit 1707a89

Browse files
Merge pull request #42 from sashamelentyev/chore/remove-http-no-body
chore: remove http.NoBody check
2 parents 8caaf87 + 234d6f4 commit 1707a89

File tree

8 files changed

+8
-87
lines changed

8 files changed

+8
-87
lines changed

pkg/analyzer/analyzer.go

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const (
2020
CryptoHashFlag = "crypto-hash"
2121
HTTPMethodFlag = "http-method"
2222
HTTPStatusCodeFlag = "http-status-code"
23-
HTTPNoBodyFlag = "http-no-body"
2423
DefaultRPCPathFlag = "default-rpc-path"
2524
)
2625

@@ -41,7 +40,6 @@ func flags() flag.FlagSet {
4140
flags := flag.NewFlagSet("", flag.ExitOnError)
4241
flags.Bool(HTTPMethodFlag, true, "suggest the use of http.MethodXX")
4342
flags.Bool(HTTPStatusCodeFlag, true, "suggest the use of http.StatusXX")
44-
flags.Bool(HTTPNoBodyFlag, false, "suggest the use of http.NoBody")
4543
flags.Bool(TimeWeekdayFlag, false, "suggest the use of time.Weekday")
4644
flags.Bool(TimeMonthFlag, false, "suggest the use of time.Month")
4745
flags.Bool(TimeLayoutFlag, false, "suggest the use of time.Layout")
@@ -79,29 +77,21 @@ func run(pass *analysis.Pass) (interface{}, error) {
7977
}
8078

8179
case "NewRequest":
82-
if lookupFlag(pass, HTTPMethodFlag) {
83-
if basicLit := getBasicLitFromArgs(n.Args, 3, 0, token.STRING); basicLit != nil {
84-
checkHTTPMethod(pass, basicLit)
85-
}
80+
if !lookupFlag(pass, HTTPMethodFlag) {
81+
return
8682
}
8783

88-
if lookupFlag(pass, HTTPNoBodyFlag) {
89-
if ident := getIdentFromArgs(n.Args, 3, 2); ident != nil {
90-
checkHTTPNoBody(pass, ident)
91-
}
84+
if basicLit := getBasicLitFromArgs(n.Args, 3, 0, token.STRING); basicLit != nil {
85+
checkHTTPMethod(pass, basicLit)
9286
}
9387

9488
case "NewRequestWithContext":
95-
if lookupFlag(pass, HTTPMethodFlag) {
96-
if basicLit := getBasicLitFromArgs(n.Args, 4, 1, token.STRING); basicLit != nil {
97-
checkHTTPMethod(pass, basicLit)
98-
}
89+
if !lookupFlag(pass, HTTPMethodFlag) {
90+
return
9991
}
10092

101-
if lookupFlag(pass, HTTPNoBodyFlag) {
102-
if ident := getIdentFromArgs(n.Args, 4, 3); ident != nil {
103-
checkHTTPNoBody(pass, ident)
104-
}
93+
if basicLit := getBasicLitFromArgs(n.Args, 4, 1, token.STRING); basicLit != nil {
94+
checkHTTPMethod(pass, basicLit)
10595
}
10696
}
10797

@@ -208,14 +198,6 @@ func checkHTTPStatusCode(pass *analysis.Pass, basicLit *ast.BasicLit) {
208198
}
209199
}
210200

211-
func checkHTTPNoBody(pass *analysis.Pass, ident *ast.Ident) {
212-
currentVal := ident.Name
213-
214-
if newVal, ok := mapping.HTTPNoBody[currentVal]; ok {
215-
report(pass, ident.Pos(), currentVal, newVal)
216-
}
217-
}
218-
219201
func checkTimeWeekday(pass *analysis.Pass, pos token.Pos, currentVal string) {
220202
if newVal, ok := mapping.TimeWeekday[currentVal]; ok {
221203
report(pass, pos, currentVal, newVal)
@@ -269,24 +251,6 @@ func getBasicLitFromArgs(args []ast.Expr, count, idx int, typ token.Token) *ast.
269251
return basicLit
270252
}
271253

272-
// getIdentFromArgs gets the *ast.Ident of a function argument.
273-
//
274-
// Arguments:
275-
// - count - expected number of argument in function
276-
// - idx - index of the argument to get the *ast.Ident
277-
func getIdentFromArgs(args []ast.Expr, count, idx int) *ast.Ident {
278-
if len(args) != count {
279-
return nil
280-
}
281-
282-
ident, ok := args[idx].(*ast.Ident)
283-
if !ok {
284-
return nil
285-
}
286-
287-
return ident
288-
}
289-
290254
// getBasicLitFromElts gets the *ast.BasicLit of a struct elements.
291255
//
292256
// Arguments:

pkg/analyzer/analyzer_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ func TestUseStdlibVars(t *testing.T) {
3333
if err := a.Flags.Set(analyzer.DefaultRPCPathFlag, "true"); err != nil {
3434
t.Error(err)
3535
}
36-
if err := a.Flags.Set(analyzer.HTTPNoBodyFlag, "true"); err != nil {
37-
t.Error(err)
38-
}
3936

4037
analysistest.Run(t, analysistest.TestData(), a, pkgs...)
4138
}

pkg/analyzer/internal/gen.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ func main() {
8383
templateName: "test-issue32.go.tmpl",
8484
fileName: "pkg/analyzer/testdata/src/a/http/issue32.go",
8585
},
86-
{
87-
mapping: mapping.HTTPNoBody,
88-
packageName: "http_test",
89-
templateName: "test-httpnobody.go.tmpl",
90-
fileName: "pkg/analyzer/testdata/src/a/http/nobody.go",
91-
},
9286
}
9387

9488
for _, operation := range operations {

pkg/analyzer/internal/mapping/mapping.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,3 @@ var TimeLayout = map[string]string{
159159
time.StampMicro: "time.StampMicro",
160160
time.StampNano: "time.StampNano",
161161
}
162-
163-
var HTTPNoBody = map[string]string{
164-
"nil": "http.NoBody",
165-
}

pkg/analyzer/internal/template/test-httpnobody.go.tmpl

Lines changed: 0 additions & 17 deletions
This file was deleted.

pkg/analyzer/testdata/src/a/http/method.go

100644100755
File mode changed.

pkg/analyzer/testdata/src/a/http/nobody.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

pkg/analyzer/testdata/src/a/http/statuscode.go

100644100755
File mode changed.

0 commit comments

Comments
 (0)