Skip to content

Commit 09870c1

Browse files
authored
helpers: isError: strict type comparison instead of implementing check (#97)
1 parent ac9da7d commit 09870c1

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

analyzer/analyzer_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func TestTestifyLint(t *testing.T) {
4141
dir: "error-as-target",
4242
flags: map[string]string{"disable-all": "true", "enable": checkers.NewErrorIsAs().Name()},
4343
},
44+
{
45+
dir: "error-nil-issue95",
46+
flags: map[string]string{"disable-all": "true", "enable": checkers.NewErrorNil().Name()},
47+
},
4448
{
4549
dir: "expected-var-custom-pattern",
4650
flags: map[string]string{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package errornilissue95
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
// APIResponse can be an error or not.
11+
type APIResponse struct {
12+
Status int
13+
Data string
14+
ErrorMsg string
15+
}
16+
17+
func (a APIResponse) Error() string {
18+
return a.ErrorMsg
19+
}
20+
21+
func Update(a string) (*APIResponse, error) {
22+
if a == "a" {
23+
return &APIResponse{Status: 200, Data: "fake"}, nil
24+
}
25+
26+
return nil, &APIResponse{Status: 500, ErrorMsg: "Oops"}
27+
}
28+
29+
func TestName(t *testing.T) {
30+
resp, err := Update("b")
31+
require.Error(t, err, new(*APIResponse))
32+
assert.Nil(t, resp)
33+
}

internal/checkers/helpers_error.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var (
1616
)
1717

1818
func isError(pass *analysis.Pass, expr ast.Expr) bool {
19-
return implements(pass, expr, errorObj)
19+
return pass.TypesInfo.TypeOf(expr) == errorType
2020
}
2121

2222
func isErrorsIsCall(pass *analysis.Pass, ce *ast.CallExpr) bool {

0 commit comments

Comments
 (0)