mirror of
https://github.com/golang/go.git
synced 2025-05-17 13:24:38 +00:00
errors: return early for Is(nil, ...)
If err is nil it wouldn't match any given target error except for nil, so we can return early to speed up cases where Is is used without a preceding err != nil check. Change-Id: Ib33cff50453fe070f06871ce8074694c81ab787b Reviewed-on: https://go-review.googlesource.com/c/go/+/576015 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
parent
a4440e3d23
commit
af43932c20
@ -42,7 +42,7 @@ func Unwrap(err error) error {
|
||||
// an example in the standard library. An Is method should only shallowly
|
||||
// compare err and the target and not call [Unwrap] on either.
|
||||
func Is(err, target error) bool {
|
||||
if target == nil {
|
||||
if err == nil || target == nil {
|
||||
return err == target
|
||||
}
|
||||
|
||||
|
@ -30,6 +30,7 @@ func TestIs(t *testing.T) {
|
||||
match bool
|
||||
}{
|
||||
{nil, nil, true},
|
||||
{nil, err1, false},
|
||||
{err1, nil, false},
|
||||
{err1, err1, true},
|
||||
{erra, err1, true},
|
||||
|
Loading…
x
Reference in New Issue
Block a user