From 9d35ebba062cae9cc7bd716ad279ac7d44060a93 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 19 Apr 2023 18:48:00 -0400 Subject: [PATCH] std: fix various nilness findings Found by running $ go run golang.org/x/tools/go/analysis/passes/nilness/cmd/nilness@latest std No actual bugs--other than one panic(nil)--but a few places where error nilness was unclear. Change-Id: Ia916ba30f46f29c1bcf928cc62280169b922463a Reviewed-on: https://go-review.googlesource.com/c/go/+/486675 Reviewed-by: Ian Lance Taylor Run-TryBot: Alan Donovan TryBot-Result: Gopher Robot Auto-Submit: Alan Donovan --- src/bytes/buffer_test.go | 2 +- src/crypto/internal/bigmod/nat_test.go | 2 +- src/encoding/xml/xml_test.go | 5 +++-- src/net/timeout_test.go | 7 ++++--- src/os/timeout_test.go | 7 ++++--- src/testing/example.go | 3 +-- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/bytes/buffer_test.go b/src/bytes/buffer_test.go index 81476fbae15d45..845e5e2209e53e 100644 --- a/src/bytes/buffer_test.go +++ b/src/bytes/buffer_test.go @@ -268,7 +268,7 @@ type panicReader struct{ panic bool } func (r panicReader) Read(p []byte) (int, error) { if r.panic { - panic(nil) + panic("oops") } return 0, io.EOF } diff --git a/src/crypto/internal/bigmod/nat_test.go b/src/crypto/internal/bigmod/nat_test.go index 6431d259547cc3..4593a2e4932795 100644 --- a/src/crypto/internal/bigmod/nat_test.go +++ b/src/crypto/internal/bigmod/nat_test.go @@ -181,7 +181,7 @@ func TestSetBytes(t *testing.T) { } continue } - if err == nil && tt.fail { + if tt.fail { t.Errorf("%d: unexpected success", i) continue } diff --git a/src/encoding/xml/xml_test.go b/src/encoding/xml/xml_test.go index 8205ac31484d84..f5c7259cfb45c1 100644 --- a/src/encoding/xml/xml_test.go +++ b/src/encoding/xml/xml_test.go @@ -1359,8 +1359,9 @@ func TestParseErrors(t *testing.T) { } continue } - if err == nil || err == io.EOF { - t.Errorf("parse %s: have no error, expected a non-nil error", test.src) + // Inv: err != nil + if err == io.EOF { + t.Errorf("parse %s: unexpected EOF", test.src) continue } if !strings.Contains(err.Error(), test.err) { diff --git a/src/net/timeout_test.go b/src/net/timeout_test.go index 6b3554ed7987e7..89605d92fc7679 100644 --- a/src/net/timeout_test.go +++ b/src/net/timeout_test.go @@ -812,10 +812,11 @@ func TestWriteTimeoutFluctuation(t *testing.T) { t.Logf("SetWriteDeadline(+%v)", d) t0 := time.Now() deadline := t0.Add(d) - if err = c.SetWriteDeadline(deadline); err != nil { + if err := c.SetWriteDeadline(deadline); err != nil { t.Fatalf("SetWriteDeadline(%v): %v", deadline, err) } var n int64 + var err error for { var dn int dn, err = c.Write([]byte("TIMEOUT TRANSMITTER")) @@ -825,8 +826,8 @@ func TestWriteTimeoutFluctuation(t *testing.T) { } } t1 := time.Now() - - if err == nil || !err.(Error).Timeout() { + // Inv: err != nil + if !err.(Error).Timeout() { t.Fatalf("Write did not return (any, timeout): (%d, %v)", n, err) } if perr := parseWriteError(err); perr != nil { diff --git a/src/os/timeout_test.go b/src/os/timeout_test.go index 3cf06d56477364..a65d703ebfa969 100644 --- a/src/os/timeout_test.go +++ b/src/os/timeout_test.go @@ -355,10 +355,11 @@ func TestWriteTimeoutFluctuation(t *testing.T) { t.Logf("SetWriteDeadline(+%v)", d) t0 := time.Now() deadline := t0.Add(d) - if err = w.SetWriteDeadline(deadline); err != nil { + if err := w.SetWriteDeadline(deadline); err != nil { t.Fatalf("SetWriteDeadline(%v): %v", deadline, err) } var n int64 + var err error for { var dn int dn, err = w.Write([]byte("TIMEOUT TRANSMITTER")) @@ -368,8 +369,8 @@ func TestWriteTimeoutFluctuation(t *testing.T) { } } t1 := time.Now() - - if err == nil || !isDeadlineExceeded(err) { + // Inv: err != nil + if !isDeadlineExceeded(err) { t.Fatalf("Write did not return (any, timeout): (%d, %v)", n, err) } diff --git a/src/testing/example.go b/src/testing/example.go index f618b06de114b0..42ee555cb28f7d 100644 --- a/src/testing/example.go +++ b/src/testing/example.go @@ -93,8 +93,7 @@ func (eg *InternalExample) processRunResult(stdout string, timeSpent time.Durati if recovered != nil { // Propagate the previously recovered result, by panicking. panic(recovered) - } - if !finished && recovered == nil { + } else if !finished { panic(errNilPanicOrGoexit) }