Skip to content

Commit e13c645

Browse files
committed
modernize: replace interface{} with any and fix deepequalerrors
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
1 parent 720bdab commit e13c645

File tree

6 files changed

+12
-14
lines changed

6 files changed

+12
-14
lines changed

bench_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func yesErrors(at, depth int) error {
2323

2424
// GlobalE is an exported global to store the result of benchmark results,
2525
// preventing the compiler from optimising the benchmark functions away.
26-
var GlobalE interface{}
26+
var GlobalE any
2727

2828
func BenchmarkErrors(b *testing.B) {
2929
type run struct {

errors.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func New(message string) error {
109109
// Errorf formats according to a format specifier and returns the string
110110
// as a value that satisfies error.
111111
// Errorf also records the stack trace at the point it was called.
112-
func Errorf(format string, args ...interface{}) error {
112+
func Errorf(format string, args ...any) error {
113113
return &fundamental{
114114
msg: fmt.Sprintf(format, args...),
115115
stack: callers(),
@@ -198,7 +198,7 @@ func Wrap(err error, message string) error {
198198
// Wrapf returns an error annotating err with a stack trace
199199
// at the point Wrapf is called, and the format specifier.
200200
// If err is nil, Wrapf returns nil.
201-
func Wrapf(err error, format string, args ...interface{}) error {
201+
func Wrapf(err error, format string, args ...any) error {
202202
if err == nil {
203203
return nil
204204
}
@@ -226,7 +226,7 @@ func WithMessage(err error, message string) error {
226226

227227
// WithMessagef annotates err with the format specifier.
228228
// If err is nil, WithMessagef returns nil.
229-
func WithMessagef(err error, format string, args ...interface{}) error {
229+
func WithMessagef(err error, format string, args ...any) error {
230230
if err == nil {
231231
return nil
232232
}

errors_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"io"
7-
"reflect"
87
"testing"
98
)
109

@@ -100,7 +99,7 @@ func TestCause(t *testing.T) {
10099

101100
for i, tt := range tests {
102101
got := Cause(tt.err)
103-
if !reflect.DeepEqual(got, tt.want) {
102+
if got != tt.want {
104103
t.Errorf("test %d: got %#v, want %#v", i+1, got, tt.want)
105104
}
106105
}

format_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ func TestFormatWrappedNew(t *testing.T) {
384384
}
385385
}
386386

387-
func testFormatRegexp(t *testing.T, n int, arg interface{}, format, want string) {
387+
func testFormatRegexp(t *testing.T, n int, arg any, format, want string) {
388388
t.Helper()
389389
got := fmt.Sprintf(format, arg)
390390
gotLines := strings.Split(got, "\n")
@@ -477,7 +477,7 @@ func parseBlocks(input string, detectStackboundaries bool) ([]string, error) {
477477
return blocks, nil
478478
}
479479

480-
func testFormatCompleteCompare(t *testing.T, n int, arg interface{}, format string, want []string, detectStackBoundaries bool) {
480+
func testFormatCompleteCompare(t *testing.T, n int, arg any, format string, want []string, detectStackBoundaries bool) {
481481
gotStr := fmt.Sprintf(format, arg)
482482

483483
got, err := parseBlocks(gotStr, detectStackBoundaries)

unwrap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ func Is(err, target error) bool { return stderrors.Is(err, target) }
2020
// repeatedly calling Unwrap.
2121
//
2222
// An error matches target if the error's concrete value is assignable to the value
23-
// pointed to by target, or if the error has a method As(interface{}) bool such that
23+
// pointed to by target, or if the error has a method As(any) bool such that
2424
// As(target) returns true. In the latter case, the As method is responsible for
2525
// setting target.
2626
//
2727
// As will panic if target is not a non-nil pointer to either a type that implements
2828
// error, or to any interface type. As returns false if err is nil.
29-
func As(err error, target interface{}) bool { return stderrors.As(err, target) }
29+
func As(err error, target any) bool { return stderrors.As(err, target) }
3030

3131
// Unwrap returns the result of calling the Unwrap method on err, if err's
3232
// type contains an Unwrap method returning error.

unwrap_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package errors
33
import (
44
stderrors "errors"
55
"fmt"
6-
"reflect"
76
"testing"
87
)
98

@@ -80,7 +79,7 @@ func TestAs(t *testing.T) {
8079

8180
type args struct {
8281
err error
83-
target interface{}
82+
target any
8483
}
8584
tests := []struct {
8685
name string
@@ -127,7 +126,7 @@ func TestAs(t *testing.T) {
127126
}
128127

129128
ce := tt.args.target.(*customErr)
130-
if !reflect.DeepEqual(err, *ce) {
129+
if err != *ce {
131130
t.Errorf("set target error failed, target error is %v", *ce)
132131
}
133132
})
@@ -168,7 +167,7 @@ func TestUnwrap(t *testing.T) {
168167
}
169168
for _, tt := range tests {
170169
t.Run(tt.name, func(t *testing.T) {
171-
if err := Unwrap(tt.args.err); !reflect.DeepEqual(err, tt.want) {
170+
if err := Unwrap(tt.args.err); err != tt.want {
172171
t.Errorf("Unwrap() error = %v, want %v", err, tt.want)
173172
}
174173
})

0 commit comments

Comments
 (0)