Skip to content

Commit

Permalink
bug: support go1.17
Browse files Browse the repository at this point in the history
  • Loading branch information
kulti committed Apr 6, 2022
1 parent 40365c5 commit 3c02d20
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
33 changes: 4 additions & 29 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,35 +262,6 @@ func (t thelper) buildTestCheckFuncOpts(pass *analysis.Pass, ctxType types.Type)
}, true
}

func (t thelper) buildFuzzCheckFuncOpts(pass *analysis.Pass, ctxType types.Type) (checkFuncOpts, bool) {
fObj := analysisutil.ObjectOf(pass, "testing", "F")
if fObj == nil {
return checkFuncOpts{}, false
}

fHelper, _, _ := types.LookupFieldOrMethod(fObj.Type(), true, fObj.Pkg(), "Helper")
if fHelper == nil {
return checkFuncOpts{}, false
}

tFuzz, _, _ := types.LookupFieldOrMethod(fObj.Type(), true, fObj.Pkg(), "Fuzz")
if tFuzz == nil {
return checkFuncOpts{}, false
}

return checkFuncOpts{
skipPrefix: "Fuzz",
varName: "f",
fnHelper: fHelper,
subRun: tFuzz,
hpType: types.NewPointer(fObj.Type()),
ctxType: ctxType,
checkBegin: t.enabledChecks.Enabled(checkFBegin),
checkFirst: t.enabledChecks.Enabled(checkFFirst),
checkName: t.enabledChecks.Enabled(checkFName),
}, true
}

func (t thelper) buildBenchmarkCheckFuncOpts(pass *analysis.Pass, ctxType types.Type) (checkFuncOpts, bool) {
bObj := analysisutil.ObjectOf(pass, "testing", "B")
if bObj == nil {
Expand Down Expand Up @@ -354,6 +325,10 @@ type funcDecl struct {
}

func checkFunc(pass *analysis.Pass, reports *reports, funcDecl funcDecl, opts checkFuncOpts) {
if !opts.checkFirst && !opts.checkBegin && !opts.checkName {
return
}

if opts.skipPrefix != "" && strings.HasPrefix(funcDecl.Name.Name, opts.skipPrefix) {
return
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/analyzer/analyzer_go117.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build !go1.18
// +build !go1.18

package analyzer

import (
"go/types"

"golang.org/x/tools/go/analysis"
)

func (t thelper) buildFuzzCheckFuncOpts(pass *analysis.Pass, ctxType types.Type) (checkFuncOpts, bool) {
return checkFuncOpts{}, true
}
40 changes: 40 additions & 0 deletions pkg/analyzer/analyzer_go118.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//go:build go1.18
// +build go1.18

package analyzer

import (
"go/types"

"github.com/gostaticanalysis/analysisutil"
"golang.org/x/tools/go/analysis"
)

func (t thelper) buildFuzzCheckFuncOpts(pass *analysis.Pass, ctxType types.Type) (checkFuncOpts, bool) {
fObj := analysisutil.ObjectOf(pass, "testing", "F")
if fObj == nil {
return checkFuncOpts{}, false
}

fHelper, _, _ := types.LookupFieldOrMethod(fObj.Type(), true, fObj.Pkg(), "Helper")
if fHelper == nil {
return checkFuncOpts{}, false
}

tFuzz, _, _ := types.LookupFieldOrMethod(fObj.Type(), true, fObj.Pkg(), "Fuzz")
if tFuzz == nil {
return checkFuncOpts{}, false
}

return checkFuncOpts{
skipPrefix: "Fuzz",
varName: "f",
fnHelper: fHelper,
subRun: tFuzz,
hpType: types.NewPointer(fObj.Type()),
ctxType: ctxType,
checkBegin: t.enabledChecks.Enabled(checkFBegin),
checkFirst: t.enabledChecks.Enabled(checkFFirst),
checkName: t.enabledChecks.Enabled(checkFName),
}, true
}

0 comments on commit 3c02d20

Please sign in to comment.