From be3cbdb108f232e08c2cb36120579ea5fd3474dc Mon Sep 17 00:00:00 2001 From: Nahshon Unna-Tsameret Date: Mon, 11 Nov 2024 15:54:41 +0200 Subject: [PATCH] Bug fix: false positive for func returns error func The linter triggers a warning for this case: ```go func errFunc() func() error { return func() error { return errors.New("error") } } var _ = Describe("test if issue 174 was solved", func() { It("should not trigger", func() { Eventually(errFunc()).Should(MatchError(ContainSubstring("error"))) }) }) ``` The linter fails to identify the actual value as error value. --- analyzer_test.go | 4 ++++ internal/expression/actual/actualarg.go | 14 ++++++++------ testdata/src/a/issue-174/issue174.go | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 testdata/src/a/issue-174/issue174.go diff --git a/analyzer_test.go b/analyzer_test.go index dff0c2d..b229f3d 100644 --- a/analyzer_test.go +++ b/analyzer_test.go @@ -101,6 +101,10 @@ func TestAllUseCases(t *testing.T) { testName: "nil error-func variable", testData: "a/issue-171", }, + { + testName: "matchError with func return error-func", + testData: "a/issue-174", + }, } { t.Run(tc.testName, func(tt *testing.T) { analysistest.Run(tt, analysistest.TestData(), ginkgolinter.NewAnalyzer(), tc.testData) diff --git a/internal/expression/actual/actualarg.go b/internal/expression/actual/actualarg.go index 9d251c4..79f1a6a 100644 --- a/internal/expression/actual/actualarg.go +++ b/internal/expression/actual/actualarg.go @@ -56,12 +56,6 @@ func getActualArgPayload(origActualExpr, actualExprClone *ast.CallExpr, pass *an case *ast.BinaryExpr: arg = parseBinaryExpr(expr, argExprClone.(*ast.BinaryExpr), pass) - - default: - t := pass.TypesInfo.TypeOf(origArgExpr) - if sig, ok := t.(*gotypes.Signature); ok { - arg = getAsyncFuncArg(sig) - } } } @@ -70,6 +64,14 @@ func getActualArgPayload(origActualExpr, actualExprClone *ast.CallExpr, pass *an return arg, actualOffset } + t := pass.TypesInfo.TypeOf(origArgExpr) + if sig, ok := t.(*gotypes.Signature); ok { + arg = getAsyncFuncArg(sig) + if arg != nil { + return arg, actualOffset + } + } + return newRegularArgPayload(origArgExpr, argExprClone, pass), actualOffset } diff --git a/testdata/src/a/issue-174/issue174.go b/testdata/src/a/issue-174/issue174.go new file mode 100644 index 0000000..d81a4ad --- /dev/null +++ b/testdata/src/a/issue-174/issue174.go @@ -0,0 +1,20 @@ +package issue_171 + +import ( + "errors" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func errFunc() func() error { + return func() error { + return errors.New("error") + } +} + +var _ = Describe("test if issue 174 was solved", func() { + It("should not trigger", func() { + Eventually(errFunc()).Should(MatchError(ContainSubstring("error"))) + }) +})