Skip to content

Commit

Permalink
Merge pull request #42 from sashamelentyev/chore/remove-http-no-body
Browse files Browse the repository at this point in the history
chore: remove http.NoBody check
  • Loading branch information
sashamelentyev authored Aug 8, 2022
2 parents 8caaf87 + 234d6f4 commit 1707a89
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 87 deletions.
52 changes: 8 additions & 44 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const (
CryptoHashFlag = "crypto-hash"
HTTPMethodFlag = "http-method"
HTTPStatusCodeFlag = "http-status-code"
HTTPNoBodyFlag = "http-no-body"
DefaultRPCPathFlag = "default-rpc-path"
)

Expand All @@ -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")
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 0 additions & 3 deletions pkg/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
6 changes: 0 additions & 6 deletions pkg/analyzer/internal/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions pkg/analyzer/internal/mapping/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
17 changes: 0 additions & 17 deletions pkg/analyzer/internal/template/test-httpnobody.go.tmpl

This file was deleted.

Empty file modified pkg/analyzer/testdata/src/a/http/method.go
100644 → 100755
Empty file.
13 changes: 0 additions & 13 deletions pkg/analyzer/testdata/src/a/http/nobody.go

This file was deleted.

Empty file modified pkg/analyzer/testdata/src/a/http/statuscode.go
100644 → 100755
Empty file.

0 comments on commit 1707a89

Please sign in to comment.