Skip to content

Commit ea1fdb9

Browse files
committed
Fix issue with error function vars
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 }) ```
1 parent 29d6a7d commit ea1fdb9

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

analyzer_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ func TestAllUseCases(t *testing.T) {
9797
testName: "HaveOccurred matcher tests",
9898
testData: "a/haveoccurred",
9999
},
100+
{
101+
testName: "nil error-func variable",
102+
testData: "a/issue-171",
103+
},
100104
} {
101105
t.Run(tc.testName, func(tt *testing.T) {
102106
analysistest.Run(tt, analysistest.TestData(), ginkgolinter.NewAnalyzer(), tc.testData)

internal/rules/errorequalnilrule.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ import (
1212
type ErrorEqualNilRule struct{}
1313

1414
func (ErrorEqualNilRule) isApplied(gexp *expression.GomegaExpression, config types.Config) bool {
15-
return !bool(config.SuppressErr) &&
16-
gexp.ActualArgTypeIs(actual.ErrorTypeArgType) &&
15+
if config.SuppressErr {
16+
return false
17+
}
18+
19+
if !gexp.IsAsync() && gexp.ActualArgTypeIs(actual.FuncSigArgType) {
20+
return false
21+
}
22+
23+
return gexp.ActualArgTypeIs(actual.ErrorTypeArgType) &&
1724
gexp.MatcherTypeIs(matcher.BeNilMatcherType|matcher.EqualNilMatcherType)
1825
}
1926

testdata/src/a/issue-171/issue171.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package issue_171
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
)
7+
8+
var _ = Describe("test if issue 171 was solved", func() {
9+
It("should not trigger warning for nil function vars", func() {
10+
f := func(string) error { return nil }
11+
12+
f = nil
13+
14+
Expect(f).To(BeNil())
15+
})
16+
})

0 commit comments

Comments
 (0)