Skip to content

Commit 223854c

Browse files
authored
GODRIVER-2156 Enable unconvert linter. (#793)
1 parent 7ea935f commit 223854c

27 files changed

+95
-98
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ linters:
1919
# - structcheck
2020
- typecheck
2121
- unused
22-
# - unconvert
22+
- unconvert
2323
# - unparam
2424
# - varcheck
2525

bson/bsoncodec/default_value_decoders_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ func TestDefaultValueDecoders(t *testing.T) {
764764
"time.Time",
765765
now,
766766
nil,
767-
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.DateTime, Return: int64(now.UnixNano() / int64(time.Millisecond))},
767+
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.DateTime, Return: now.UnixNano() / int64(time.Millisecond)},
768768
bsonrwtest.ReadDateTime,
769769
nil,
770770
},
@@ -828,7 +828,7 @@ func TestDefaultValueDecoders(t *testing.T) {
828828
&DecodeContext{Registry: NewRegistryBuilder().Build()},
829829
&bsonrwtest.ValueReaderWriter{},
830830
bsonrwtest.ReadDocument,
831-
ErrNoDecoder{Type: reflect.TypeOf(string(""))},
831+
ErrNoDecoder{Type: reflect.TypeOf("")},
832832
},
833833
{
834834
"ReadElement Error",
@@ -914,7 +914,7 @@ func TestDefaultValueDecoders(t *testing.T) {
914914
&DecodeContext{Registry: NewRegistryBuilder().Build()},
915915
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.Array},
916916
bsonrwtest.ReadArray,
917-
ErrNoDecoder{Type: reflect.TypeOf(string(""))},
917+
ErrNoDecoder{Type: reflect.TypeOf("")},
918918
},
919919
{
920920
"ReadValue Error",
@@ -1008,7 +1008,7 @@ func TestDefaultValueDecoders(t *testing.T) {
10081008
&DecodeContext{Registry: NewRegistryBuilder().Build()},
10091009
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.Array},
10101010
bsonrwtest.ReadArray,
1011-
ErrNoDecoder{Type: reflect.TypeOf(string(""))},
1011+
ErrNoDecoder{Type: reflect.TypeOf("")},
10121012
},
10131013
{
10141014
"ReadValue Error",
@@ -1319,7 +1319,7 @@ func TestDefaultValueDecoders(t *testing.T) {
13191319
"type not *url.URL",
13201320
int64(0),
13211321
nil,
1322-
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.String, Return: string("http://example.com")},
1322+
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.String, Return: "http://example.com"},
13231323
bsonrwtest.Nothing,
13241324
ValueDecoderError{Name: "URLDecodeValue", Types: []reflect.Type{tURL}, Received: reflect.ValueOf(int64(0))},
13251325
},
@@ -1335,7 +1335,7 @@ func TestDefaultValueDecoders(t *testing.T) {
13351335
"url.Parse error",
13361336
url.URL{},
13371337
nil,
1338-
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.String, Return: string("not-valid-%%%%://")},
1338+
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.String, Return: "not-valid-%%%%://"},
13391339
bsonrwtest.ReadString,
13401340
&url.Error{
13411341
Op: "parse",
@@ -1347,15 +1347,15 @@ func TestDefaultValueDecoders(t *testing.T) {
13471347
"can set false",
13481348
cansettest,
13491349
nil,
1350-
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.String, Return: string("http://example.com")},
1350+
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.String, Return: "http://example.com"},
13511351
bsonrwtest.Nothing,
13521352
ValueDecoderError{Name: "URLDecodeValue", Types: []reflect.Type{tURL}},
13531353
},
13541354
{
13551355
"url.URL",
13561356
url.URL{Scheme: "http", Host: "example.com"},
13571357
nil,
1358-
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.String, Return: string("http://example.com")},
1358+
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.String, Return: "http://example.com"},
13591359
bsonrwtest.ReadString,
13601360
nil,
13611361
},
@@ -1503,7 +1503,7 @@ func TestDefaultValueDecoders(t *testing.T) {
15031503
"ValueUnmarshaler",
15041504
&testValueUnmarshaler{t: bsontype.String, val: bsoncore.AppendString(nil, "hello, world")},
15051505
nil,
1506-
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.String, Return: string("hello, world")},
1506+
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.String, Return: "hello, world"},
15071507
bsonrwtest.ReadString,
15081508
nil,
15091509
},
@@ -3202,7 +3202,7 @@ func TestDefaultValueDecoders(t *testing.T) {
32023202
},
32033203
{
32043204
"String - string",
3205-
string("foo bar baz"),
3205+
"foo bar baz",
32063206
bsontype.String,
32073207
},
32083208
{

bson/bsoncodec/map_codec.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,15 @@ func (mc *MapCodec) decodeKey(key string, keyType reflect.Type) (reflect.Value,
265265
case reflect.String:
266266
keyVal = reflect.ValueOf(key).Convert(keyType)
267267
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
268-
s := string(key)
269-
n, parseErr := strconv.ParseInt(s, 10, 64)
268+
n, parseErr := strconv.ParseInt(key, 10, 64)
270269
if parseErr != nil || reflect.Zero(keyType).OverflowInt(n) {
271-
err = fmt.Errorf("failed to unmarshal number key %v", s)
270+
err = fmt.Errorf("failed to unmarshal number key %v", key)
272271
}
273272
keyVal = reflect.ValueOf(n).Convert(keyType)
274273
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
275-
s := string(key)
276-
n, parseErr := strconv.ParseUint(s, 10, 64)
274+
n, parseErr := strconv.ParseUint(key, 10, 64)
277275
if parseErr != nil || reflect.Zero(keyType).OverflowUint(n) {
278-
err = fmt.Errorf("failed to unmarshal number key %v", s)
276+
err = fmt.Errorf("failed to unmarshal number key %v", key)
279277
break
280278
}
281279
keyVal = reflect.ValueOf(n).Convert(keyType)

bson/bsoncodec/registry_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,13 @@ func TestRegistry(t *testing.T) {
377377
})
378378
t.Run("Type Map", func(t *testing.T) {
379379
reg := NewRegistryBuilder().
380-
RegisterTypeMapEntry(bsontype.String, reflect.TypeOf(string(""))).
380+
RegisterTypeMapEntry(bsontype.String, reflect.TypeOf("")).
381381
RegisterTypeMapEntry(bsontype.Int32, reflect.TypeOf(int(0))).
382382
Build()
383383

384384
var got, want reflect.Type
385385

386-
want = reflect.TypeOf(string(""))
386+
want = reflect.TypeOf("")
387387
got, err := reg.LookupTypeMapEntry(bsontype.String)
388388
noerr(t, err)
389389
if got != want {

bson/bsoncodec/time_codec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestTimeCodec(t *testing.T) {
2222
now := time.Now().Truncate(time.Millisecond)
2323

2424
t.Run("UseLocalTimeZone", func(t *testing.T) {
25-
reader := &bsonrwtest.ValueReaderWriter{BSONType: bsontype.DateTime, Return: int64(now.UnixNano() / int64(time.Millisecond))}
25+
reader := &bsonrwtest.ValueReaderWriter{BSONType: bsontype.DateTime, Return: now.UnixNano() / int64(time.Millisecond)}
2626
testCases := []struct {
2727
name string
2828
opts *bsonoptions.TimeCodecOptions

bson/bsonrw/copier_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func TestCopier(t *testing.T) {
132132
{
133133
"String/dst/error",
134134
&TestValueReaderWriter{bsontype: bsontype.String, err: errors.New("2"), errAfter: llvrwWriteString},
135-
&TestValueReaderWriter{bsontype: bsontype.String, readval: string("hello, world")},
135+
&TestValueReaderWriter{bsontype: bsontype.String, readval: "hello, world"},
136136
errors.New("2"),
137137
},
138138
{
@@ -270,7 +270,7 @@ func TestCopier(t *testing.T) {
270270
{
271271
"Javascript/dst/error",
272272
&TestValueReaderWriter{bsontype: bsontype.JavaScript, err: errors.New("2"), errAfter: llvrwWriteJavascript},
273-
&TestValueReaderWriter{bsontype: bsontype.JavaScript, readval: string("hello, world")},
273+
&TestValueReaderWriter{bsontype: bsontype.JavaScript, readval: "hello, world"},
274274
errors.New("2"),
275275
},
276276
{
@@ -467,7 +467,7 @@ func TestCopier(t *testing.T) {
467467
}
468468
})
469469
t.Run("Non BytesReader", func(t *testing.T) {
470-
llvrw := &TestValueReaderWriter{t: t, bsontype: bsontype.String, readval: string("Hello, world!")}
470+
llvrw := &TestValueReaderWriter{t: t, bsontype: bsontype.String, readval: "Hello, world!"}
471471
btype, got, err := Copier{}.CopyValueToBytes(llvrw)
472472
noerr(t, err)
473473
want := bsoncore.AppendString(nil, "Hello, world!")
@@ -506,7 +506,7 @@ func TestCopier(t *testing.T) {
506506
}
507507
})
508508
t.Run("Non BytesReader", func(t *testing.T) {
509-
llvrw := &TestValueReaderWriter{t: t, bsontype: bsontype.String, readval: string("Hello, world!")}
509+
llvrw := &TestValueReaderWriter{t: t, bsontype: bsontype.String, readval: "Hello, world!"}
510510
btype, got, err := Copier{}.AppendValueBytes(nil, llvrw)
511511
noerr(t, err)
512512
want := bsoncore.AppendString(nil, "Hello, world!")

bson/bsonrw/extjson_wrappers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
func wrapperKeyBSONType(key string) bsontype.Type {
22-
switch string(key) {
22+
switch key {
2323
case "$numberInt":
2424
return bsontype.Int32
2525
case "$numberLong":
@@ -269,7 +269,7 @@ func (ejv *extJSONValue) parseDouble() (float64, error) {
269269
return 0, fmt.Errorf("$numberDouble value should be string, but instead is %s", ejv.t)
270270
}
271271

272-
switch string(ejv.v.(string)) {
272+
switch ejv.v.(string) {
273273
case "Infinity":
274274
return math.Inf(1), nil
275275
case "-Infinity":
@@ -364,7 +364,7 @@ func (ejv *extJSONValue) parseRegex() (pattern, options string, err error) {
364364
for i, key := range regexObj.keys {
365365
val := regexObj.values[i]
366366

367-
switch string(key) {
367+
switch key {
368368
case "pattern":
369369
if patFound {
370370
return "", "", errors.New("duplicate pattern key in $regularExpression")

bson/mgocompat/bson_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ func TestUnmarshalSetterErrSetZero(t *testing.T) {
975975
assert.Nil(t, err, "expected nil error, got: %v", err)
976976

977977
m := map[string]*setterType{}
978-
err = bson.UnmarshalWithRegistry(Registry, []byte(data), m)
978+
err = bson.UnmarshalWithRegistry(Registry, data, m)
979979
assert.Nil(t, err, "expected nil error, got: %v", err)
980980

981981
value, ok := m["field"]

bson/primitive/decimal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func TestDecimal128_JSON(t *testing.T) {
155155
bytes, err := json.Marshal(decimal)
156156
assert.Nil(t, err, "json.Marshal error: %v", err)
157157
got := NewDecimal128(0, 0)
158-
err = json.Unmarshal([]byte(bytes), &got)
158+
err = json.Unmarshal(bytes, &got)
159159
assert.Nil(t, err, "json.Unmarshal error: %v", err)
160160
assert.Equal(t, decimal.h, got.h, "expected h: %v got: %v", decimal.h, got.h)
161161
assert.Equal(t, decimal.l, got.l, "expected l: %v got: %v", decimal.l, got.l)

bson/unmarshal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func TestCachingDecodersNotSharedAcrossRegistries(t *testing.T) {
188188
return nil
189189
}
190190
customReg := NewRegistryBuilder().
191-
RegisterTypeDecoder(tInt32, bsoncodec.ValueDecoderFunc(decodeInt32)).
191+
RegisterTypeDecoder(tInt32, decodeInt32).
192192
Build()
193193

194194
docBytes := bsoncore.BuildDocumentFromElements(

0 commit comments

Comments
 (0)