diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go index 3064ac3..a5c5d4a 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/analyzer/analyzer.go @@ -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 { @@ -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 } diff --git a/pkg/analyzer/analyzer_go117.go b/pkg/analyzer/analyzer_go117.go new file mode 100644 index 0000000..ea728a4 --- /dev/null +++ b/pkg/analyzer/analyzer_go117.go @@ -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 +} diff --git a/pkg/analyzer/analyzer_go118.go b/pkg/analyzer/analyzer_go118.go new file mode 100644 index 0000000..14ed5a0 --- /dev/null +++ b/pkg/analyzer/analyzer_go118.go @@ -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 +}