]> Cypherpunks repositories - gostls13.git/commitdiff
errors: return early for Is(nil, ...)
authorTobias Klauser <tklauser@distanz.ch>
Wed, 3 Apr 2024 09:50:36 +0000 (11:50 +0200)
committerGopher Robot <gobot@golang.org>
Wed, 3 Apr 2024 22:49:49 +0000 (22:49 +0000)
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>
src/errors/wrap.go
src/errors/wrap_test.go

index 88ee0a9281f2f13b6e2bcaeb77163a4760497725..57060517b552c73c819c8e416d374a2c01908de7 100644 (file)
@@ -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
        }
 
index 0a7bc5d16a1344c87cb7c802cd5c9fb6726dd1c8..58ed95fd9a0fc735e7ffe774304ddea1a5ab9e2a 100644 (file)
@@ -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},