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(

mongo/change_stream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func newChangeStream(ctx context.Context, config changeStreamConfig, pipeline in
141141
cs.cursorOptions.BatchSize = *cs.options.BatchSize
142142
}
143143
if cs.options.MaxAwaitTime != nil {
144-
cs.cursorOptions.MaxTimeMS = int64(time.Duration(*cs.options.MaxAwaitTime) / time.Millisecond)
144+
cs.cursorOptions.MaxTimeMS = int64(*cs.options.MaxAwaitTime / time.Millisecond)
145145
}
146146

147147
switch cs.streamType {

mongo/collection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ func (coll *Collection) Distinct(ctx context.Context, fieldName string, filter i
10451045
selector := makeReadPrefSelector(sess, coll.readSelector, coll.client.localThreshold)
10461046
option := options.MergeDistinctOptions(opts...)
10471047

1048-
op := operation.NewDistinct(fieldName, bsoncore.Document(f)).
1048+
op := operation.NewDistinct(fieldName, f).
10491049
Session(sess).ClusterClock(coll.client.clock).
10501050
Database(coll.db.name).Collection(coll.name).CommandMonitor(coll.client.monitor).
10511051
Deployment(coll.client.deployment).ReadConcern(rc).ReadPreference(coll.readPreference).

mongo/gridfs/download_stream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func (ds *DownloadStream) fillBuffer(ctx context.Context) error {
267267
if ds.expectedChunk == ds.numChunks {
268268
// final chunk can be fewer than ds.chunkSize bytes
269269
bytesDownloaded := int64(ds.chunkSize) * (int64(ds.expectedChunk) - int64(1))
270-
bytesRemaining := ds.fileLen - int64(bytesDownloaded)
270+
bytesRemaining := ds.fileLen - bytesDownloaded
271271

272272
if int64(bytesLen) != bytesRemaining {
273273
return ErrWrongSize

mongo/gridfs/upload_stream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (us *UploadStream) uploadChunks(ctx context.Context, uploadPartial bool) er
161161
numChunks = int(math.Floor(chunks))
162162
}
163163

164-
docs := make([]interface{}, int(numChunks))
164+
docs := make([]interface{}, numChunks)
165165

166166
id, err := convertFileID(us.FileID)
167167
if err != nil {

mongo/integration/operation_legacy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func runFindWithOptions(mt *mtest.T) opQuery {
197197
{"$orderby", sort},
198198
}
199199
return opQuery{
200-
flags: wiremessage.QueryFlag(wiremessage.Partial | wiremessage.TailableCursor | wiremessage.NoCursorTimeout | wiremessage.OplogReplay | wiremessage.SecondaryOK),
200+
flags: wiremessage.Partial | wiremessage.TailableCursor | wiremessage.NoCursorTimeout | wiremessage.OplogReplay | wiremessage.SecondaryOK,
201201
fullCollectionName: fullCollName(mt, mt.Coll.Name()),
202202
numToSkip: 1,
203203
numToReturn: 2,

mongo/integration/unified/collection_operation_execution.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,15 @@ func executeCountDocuments(ctx context.Context, operation *operation) (*operatio
169169
}
170170
opts.SetCollation(collation)
171171
case "filter":
172-
filter = bson.Raw(val.Document())
172+
filter = val.Document()
173173
case "hint":
174174
hint, err := createHint(val)
175175
if err != nil {
176176
return nil, fmt.Errorf("error creating hint: %v", err)
177177
}
178178
opts.SetHint(hint)
179179
case "limit":
180-
opts.SetLimit(int64(val.Int64()))
180+
opts.SetLimit(val.Int64())
181181
case "maxTimeMS":
182182
opts.SetMaxTime(time.Duration(val.Int32()) * time.Millisecond)
183183
case "skip":

mongo/mongo_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func TestMongoHelpers(t *testing.T) {
179179
primitive.A{"5"},
180180
nil,
181181
false,
182-
MarshalError{Value: string(""), Err: errors.New("WriteString can only write while positioned on a Element or Value but is positioned on a TopLevel")},
182+
MarshalError{Value: "", Err: errors.New("WriteString can only write while positioned on a Element or Value but is positioned on a TopLevel")},
183183
},
184184
{
185185
"primitive.A/success",
@@ -196,7 +196,7 @@ func TestMongoHelpers(t *testing.T) {
196196
bson.A{"5"},
197197
nil,
198198
false,
199-
MarshalError{Value: string(""), Err: errors.New("WriteString can only write while positioned on a Element or Value but is positioned on a TopLevel")},
199+
MarshalError{Value: "", Err: errors.New("WriteString can only write while positioned on a Element or Value but is positioned on a TopLevel")},
200200
},
201201
{
202202
"bson.A/success",
@@ -213,7 +213,7 @@ func TestMongoHelpers(t *testing.T) {
213213
[]interface{}{"5"},
214214
nil,
215215
false,
216-
MarshalError{Value: string(""), Err: errors.New("WriteString can only write while positioned on a Element or Value but is positioned on a TopLevel")},
216+
MarshalError{Value: "", Err: errors.New("WriteString can only write while positioned on a Element or Value but is positioned on a TopLevel")},
217217
},
218218
{
219219
"[]interface{}/success",

mongo/writeconcern/writeconcern.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (wc *WriteConcern) MarshalBSONValue() (bsontype.Type, []byte, error) {
105105

106106
elems = bsoncore.AppendInt32Element(elems, "w", int32(t))
107107
case string:
108-
elems = bsoncore.AppendStringElement(elems, "w", string(t))
108+
elems = bsoncore.AppendStringElement(elems, "w", t)
109109
}
110110
}
111111

x/bsonx/bsoncore/value.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ func (v Value) Time() time.Time {
602602
if !ok {
603603
panic(NewInsufficientBytesError(v.Data, v.Data))
604604
}
605-
return time.Unix(int64(dt)/1000, int64(dt)%1000*1000000)
605+
return time.Unix(dt/1000, dt%1000*1000000)
606606
}
607607

608608
// TimeOK is the same as Time, except it returns a boolean instead of
@@ -615,7 +615,7 @@ func (v Value) TimeOK() (time.Time, bool) {
615615
if !ok {
616616
return time.Time{}, false
617617
}
618-
return time.Unix(int64(dt)/1000, int64(dt)%1000*1000000), true
618+
return time.Unix(dt/1000, dt%1000*1000000), true
619619
}
620620

621621
// Regex returns the BSON regex value the Value represents. It panics if the value is a BSON

0 commit comments

Comments
 (0)