Skip to content

Commit

Permalink
Fix issue with error function vars
Browse files Browse the repository at this point in the history
Fix false positive for this case:
```go
It("should not trigger warning for nil function vars", func() {
	f := func(string) error { return nil }

	f = nil
	Expect(f).To(BeNil()) // <== should not trigger a warning, but it did
})
```
  • Loading branch information
nunnatsa committed Nov 11, 2024
1 parent 29d6a7d commit ea1fdb9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func TestAllUseCases(t *testing.T) {
testName: "HaveOccurred matcher tests",
testData: "a/haveoccurred",
},
{
testName: "nil error-func variable",
testData: "a/issue-171",
},
} {
t.Run(tc.testName, func(tt *testing.T) {
analysistest.Run(tt, analysistest.TestData(), ginkgolinter.NewAnalyzer(), tc.testData)
Expand Down
11 changes: 9 additions & 2 deletions internal/rules/errorequalnilrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ import (
type ErrorEqualNilRule struct{}

func (ErrorEqualNilRule) isApplied(gexp *expression.GomegaExpression, config types.Config) bool {
return !bool(config.SuppressErr) &&
gexp.ActualArgTypeIs(actual.ErrorTypeArgType) &&
if config.SuppressErr {
return false
}

if !gexp.IsAsync() && gexp.ActualArgTypeIs(actual.FuncSigArgType) {
return false
}

return gexp.ActualArgTypeIs(actual.ErrorTypeArgType) &&
gexp.MatcherTypeIs(matcher.BeNilMatcherType|matcher.EqualNilMatcherType)
}

Expand Down
16 changes: 16 additions & 0 deletions testdata/src/a/issue-171/issue171.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package issue_171

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("test if issue 171 was solved", func() {
It("should not trigger warning for nil function vars", func() {
f := func(string) error { return nil }

f = nil

Expect(f).To(BeNil())
})
})

0 comments on commit ea1fdb9

Please # to comment.