Skip to content

Commit

Permalink
Fix false positive when asserting an error pointer
Browse files Browse the repository at this point in the history
When using a pointer as an error, like in:

```go
type myErr struct {
    code int
}

func (e *myErr) Error() string {
    ...
}

...
err = &myErr{code: 404}
Expect(err).To(HaveOccured())
```

ginkgolinter mistakely detects `err` as not error type.

This PR fixes this issue.
  • Loading branch information
nunnatsa committed Dec 11, 2024
1 parent de40b9e commit db3b623
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/expression/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ func IsExprError(pass *analysis.Pass, expr ast.Expr) bool {
return interfaces.ImplementsError(actualArgType)

case *gotypes.Pointer:
if interfaces.ImplementsError(t) {
return true
}

if tt, ok := t.Elem().(*gotypes.Named); ok {
return interfaces.ImplementsError(tt)
}
Expand Down
35 changes: 35 additions & 0 deletions testdata/src/a/haveoccurred/haveoccurred.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,38 @@ var _ = Describe("Succeed", func() {
})

})

type myError struct {
e string
}

func (e *myError) Error() string {
return e.e
}

func retErr(str string) *myError {
return &myError{e: str}
}

var _ = Describe("pointer error", func() {
Context("err pointer var", func() {
It("should not trigger warning", func() {
err := &myError{e: "err"}
Expect(err).To(HaveOccurred())
})

})

Context("returned value", func() {
It("should not trigger warning", func() {
err := retErr("err")
Expect(err).To(HaveOccurred())
})
})

Context("Succeed", func() {
It("should not trigger warning", func() {
Expect(retErr("err")).ToNot(Succeed())
})
})
})

0 comments on commit db3b623

Please sign in to comment.