Skip to content

Commit f36a68d

Browse files
dsnetneild
andauthored
Pre-declare global type variables (#302)
Co-authored-by: Damien Neil <[email protected]>
1 parent 5dac6aa commit f36a68d

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

cmp/compare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func rootStep(x, y interface{}) PathStep {
144144
// so that they have the same parent type.
145145
var t reflect.Type
146146
if !vx.IsValid() || !vy.IsValid() || vx.Type() != vy.Type() {
147-
t = reflect.TypeOf((*interface{})(nil)).Elem()
147+
t = anyType
148148
if vx.IsValid() {
149149
vvx := reflect.New(t).Elem()
150150
vvx.Set(vx)

cmp/report_compare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (opts formatOptions) FormatDiff(v *valueNode, ptrs *pointerReferences) (out
115115

116116
// For leaf nodes, format the value based on the reflect.Values alone.
117117
// As a special case, treat equal []byte as a leaf nodes.
118-
isBytes := v.Type.Kind() == reflect.Slice && v.Type.Elem() == reflect.TypeOf(byte(0))
118+
isBytes := v.Type.Kind() == reflect.Slice && v.Type.Elem() == byteType
119119
isEqualBytes := isBytes && v.NumDiff+v.NumIgnored+v.NumTransformed == 0
120120
if v.MaxDepth == 0 || isEqualBytes {
121121
switch opts.DiffMode {

cmp/report_reflect.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ import (
1616
"github.com/google/go-cmp/cmp/internal/value"
1717
)
1818

19+
var (
20+
anyType = reflect.TypeOf((*interface{})(nil)).Elem()
21+
stringType = reflect.TypeOf((*string)(nil)).Elem()
22+
bytesType = reflect.TypeOf((*[]byte)(nil)).Elem()
23+
byteType = reflect.TypeOf((*byte)(nil)).Elem()
24+
)
25+
1926
type formatValueOptions struct {
2027
// AvoidStringer controls whether to avoid calling custom stringer
2128
// methods like error.Error or fmt.Stringer.String.
@@ -205,7 +212,7 @@ func (opts formatOptions) FormatValue(v reflect.Value, parentKind reflect.Kind,
205212
}
206213

207214
// Check whether this is a []byte of text data.
208-
if t.Elem() == reflect.TypeOf(byte(0)) {
215+
if t.Elem() == byteType {
209216
b := v.Bytes()
210217
isPrintSpace := func(r rune) bool { return unicode.IsPrint(r) || unicode.IsSpace(r) }
211218
if len(b) > 0 && utf8.Valid(b) && len(bytes.TrimFunc(b, isPrintSpace)) == 0 {

cmp/report_slices.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (opts formatOptions) FormatDiffSlice(v *valueNode) textNode {
104104
case t.Kind() == reflect.String:
105105
sx, sy = vx.String(), vy.String()
106106
isString = true
107-
case t.Kind() == reflect.Slice && t.Elem() == reflect.TypeOf(byte(0)):
107+
case t.Kind() == reflect.Slice && t.Elem() == byteType:
108108
sx, sy = string(vx.Bytes()), string(vy.Bytes())
109109
isString = true
110110
case t.Kind() == reflect.Array:
@@ -232,7 +232,7 @@ func (opts formatOptions) FormatDiffSlice(v *valueNode) textNode {
232232
var out textNode = &textWrap{Prefix: "(", Value: list2, Suffix: ")"}
233233
switch t.Kind() {
234234
case reflect.String:
235-
if t != reflect.TypeOf(string("")) {
235+
if t != stringType {
236236
out = opts.FormatType(t, out)
237237
}
238238
case reflect.Slice:
@@ -327,12 +327,12 @@ func (opts formatOptions) FormatDiffSlice(v *valueNode) textNode {
327327
switch t.Kind() {
328328
case reflect.String:
329329
out = &textWrap{Prefix: "strings.Join(", Value: out, Suffix: fmt.Sprintf(", %q)", delim)}
330-
if t != reflect.TypeOf(string("")) {
330+
if t != stringType {
331331
out = opts.FormatType(t, out)
332332
}
333333
case reflect.Slice:
334334
out = &textWrap{Prefix: "bytes.Join(", Value: out, Suffix: fmt.Sprintf(", %q)", delim)}
335-
if t != reflect.TypeOf([]byte(nil)) {
335+
if t != bytesType {
336336
out = opts.FormatType(t, out)
337337
}
338338
}

0 commit comments

Comments
 (0)