Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove http.NoBody check #42

Merged
merged 1 commit into from Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 8 additions & 44 deletions pkg/analyzer/analyzer.go
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
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
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
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.