From 1ed2e864ac8709945d823d882736c3931c178740 Mon Sep 17 00:00:00 2001 From: Aleksey Bakin Date: Sun, 22 May 2022 11:32:16 +0300 Subject: [PATCH] bug: go1.17 support Fuzzing tests are introduced in go1.18. When linter runs on older go runtime, it's ok, that testing.F is not found. Disable f-checks if testing.F not found. --- pkg/analyzer/analyzer.go | 29 ++++++++++++++++++++++++ pkg/analyzer/analyzer_go117.go | 14 ------------ pkg/analyzer/analyzer_go118.go | 40 ---------------------------------- 3 files changed, 29 insertions(+), 54 deletions(-) delete mode 100644 pkg/analyzer/analyzer_go117.go delete mode 100644 pkg/analyzer/analyzer_go118.go diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go index a5c5d4a..a22fd6a 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/analyzer/analyzer.go @@ -262,6 +262,35 @@ 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{}, true // fuzzing supports since go1.18, it's ok, that testig.F is missed. + } + + 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 { diff --git a/pkg/analyzer/analyzer_go117.go b/pkg/analyzer/analyzer_go117.go deleted file mode 100644 index ea728a4..0000000 --- a/pkg/analyzer/analyzer_go117.go +++ /dev/null @@ -1,14 +0,0 @@ -//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 deleted file mode 100644 index 14ed5a0..0000000 --- a/pkg/analyzer/analyzer_go118.go +++ /dev/null @@ -1,40 +0,0 @@ -//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 -}