Skip to content

Commit

Permalink
fix: Allow type assertions of non-error types (fixes #61)
Browse files Browse the repository at this point in the history
  • Loading branch information
polyfloyd committed Dec 1, 2023
1 parent ef170d2 commit 4f4cde4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions errorlint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ func LintErrorTypeAssertions(fset *token.FileSet, info *TypesInfoExt) []analysis
continue
}

// If the asserted type is not an error, allow the expression.
if !implementsError(info.TypesInfo.Types[typeAssert.Type].Type) {
continue
}

lints = append(lints, analysis.Diagnostic{
Message: "type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors",
Pos: typeAssert.Pos(),
Expand Down
12 changes: 12 additions & 0 deletions errorlint/testdata/src/issues/github-61.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package issues

import (
"errors"
)

func Issue61() {
err := errors.Join(errors.New("err1"), errors.New("err2"))
errs, ok := err.(interface{ Unwrap() []error })
_ = errs
_ = ok
}

0 comments on commit 4f4cde4

Please sign in to comment.