Skip to content

Commit

Permalink
helpers: isError: strict type comparison instead of implementing check (
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonboom authored May 27, 2024
1 parent ac9da7d commit 09870c1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func TestTestifyLint(t *testing.T) {
dir: "error-as-target",
flags: map[string]string{"disable-all": "true", "enable": checkers.NewErrorIsAs().Name()},
},
{
dir: "error-nil-issue95",
flags: map[string]string{"disable-all": "true", "enable": checkers.NewErrorNil().Name()},
},
{
dir: "expected-var-custom-pattern",
flags: map[string]string{
Expand Down
33 changes: 33 additions & 0 deletions analyzer/testdata/src/error-nil-issue95/mixed_struct_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package errornilissue95

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// APIResponse can be an error or not.
type APIResponse struct {
Status int
Data string
ErrorMsg string
}

func (a APIResponse) Error() string {
return a.ErrorMsg
}

func Update(a string) (*APIResponse, error) {
if a == "a" {
return &APIResponse{Status: 200, Data: "fake"}, nil
}

return nil, &APIResponse{Status: 500, ErrorMsg: "Oops"}
}

func TestName(t *testing.T) {
resp, err := Update("b")
require.Error(t, err, new(*APIResponse))
assert.Nil(t, resp)
}
2 changes: 1 addition & 1 deletion internal/checkers/helpers_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
)

func isError(pass *analysis.Pass, expr ast.Expr) bool {
return implements(pass, expr, errorObj)
return pass.TypesInfo.TypeOf(expr) == errorType
}

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

0 comments on commit 09870c1

Please sign in to comment.