Skip to content

Commit

Permalink
std: fix various nilness findings
Browse files Browse the repository at this point in the history
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 <[email protected]>
Run-TryBot: Alan Donovan <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Auto-Submit: Alan Donovan <[email protected]>
  • Loading branch information
adonovan authored and gopherbot committed Apr 20, 2023
1 parent 5c865c7 commit 9d35ebb
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/bytes/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/internal/bigmod/nat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions src/encoding/xml/xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions src/net/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions src/os/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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)
}

Expand Down
3 changes: 1 addition & 2 deletions src/testing/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 9d35ebb

Please sign in to comment.