Skip to content

Commit

Permalink
Bug fix: false positive for func returns error func
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nunnatsa committed Nov 11, 2024
1 parent 7465dc6 commit be3cbdb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
4 changes: 4 additions & 0 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions internal/expression/actual/actualarg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

}
Expand All @@ -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
}

Expand Down
20 changes: 20 additions & 0 deletions testdata/src/a/issue-174/issue174.go
Original file line number Diff line number Diff line change
@@ -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")))
})
})

0 comments on commit be3cbdb

Please # to comment.