Skip to content

Commit ad46855

Browse files
authored
GODRIVER-2156 Enable deadcode linter. (#770)
1 parent 8a397a4 commit ad46855

38 files changed

+11
-349
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ linters:
55
disable-all: true
66
# TODO(GODRIVER-2156): Enable all commented-out linters.
77
enable:
8-
# - deadcode
8+
- deadcode
99
- errcheck
1010
# - errorlint
1111
- goimports

benchmark/bson.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,3 @@ func loadSourceRaw(pathParts ...string) (bson.Raw, error) {
5555

5656
return bson.Raw(raw), nil
5757
}
58-
59-
func loadSourceD(pathParts ...string) (bson.D, error) {
60-
data, err := ioutil.ReadFile(filepath.Join(pathParts...))
61-
if err != nil {
62-
return nil, err
63-
}
64-
doc := bson.D{}
65-
err = bson.UnmarshalExtJSON(data, true, &doc)
66-
if err != nil {
67-
return nil, err
68-
}
69-
70-
if len(doc) == 0 {
71-
return nil, errors.New("empty bson document")
72-
}
73-
74-
return doc, nil
75-
}

benchmark/harness.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const (
2222
tenThousand = ten * thousand
2323
hundredThousand = hundred * thousand
2424
million = hundred * hundredThousand
25-
halfMillion = five * hundredThousand
2625

2726
ExecutionTimeout = five * time.Minute
2827
StandardRuntime = time.Minute

bson/bson_corpus_spec_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/google/go-cmp/cmp"
2323
"github.com/stretchr/testify/require"
2424
"github.com/tidwall/pretty"
25-
"go.mongodb.org/mongo-driver/bson/bsoncodec"
2625
"go.mongodb.org/mongo-driver/bson/primitive"
2726
"go.mongodb.org/mongo-driver/internal/testutil/assert"
2827
)
@@ -61,12 +60,6 @@ type parseErrorTestCase struct {
6160

6261
const dataDir = "../data/bson-corpus/"
6362

64-
var dvd bsoncodec.DefaultValueDecoders
65-
var dve bsoncodec.DefaultValueEncoders
66-
67-
var dc = bsoncodec.DecodeContext{Registry: NewRegistryBuilder().Build()}
68-
var ec = bsoncodec.EncodeContext{Registry: NewRegistryBuilder().Build()}
69-
7063
func findJSONFilesInDir(t *testing.T, dir string) []string {
7164
files := make([]string, 0)
7265

bson/bson_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"time"
1616

1717
"github.com/google/go-cmp/cmp"
18-
"github.com/stretchr/testify/require"
1918
"go.mongodb.org/mongo-driver/bson/bsoncodec"
2019
"go.mongodb.org/mongo-driver/bson/bsonoptions"
2120
"go.mongodb.org/mongo-driver/bson/bsontype"
@@ -31,10 +30,6 @@ func noerr(t *testing.T, err error) {
3130
}
3231
}
3332

34-
func requireErrEqual(t *testing.T, err1 error, err2 error) {
35-
require.True(t, compareErrors(err1, err2))
36-
}
37-
3833
func TestTimeRoundTrip(t *testing.T) {
3934
val := struct {
4035
Value time.Time

bson/bsoncodec/bsoncodec_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ func compareDecimal128(d1, d2 primitive.Decimal128) bool {
9292
return true
9393
}
9494

95-
func compareStrings(s1, s2 string) bool { return s1 == s2 }
96-
9795
type noPrivateFields struct {
9896
a string
9997
}

bson/bsoncodec/registry_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,6 @@ func TestRegistry(t *testing.T) {
411411

412412
type fakeType1 struct{ b bool }
413413
type fakeType2 struct{ b bool }
414-
type fakeType3 struct{ b bool }
415414
type fakeType4 struct{ b bool }
416415
type fakeType5 func(string, string) string
417416
type fakeStructCodec struct{ fakeCodec }

bson/bsoncodec/types.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,12 @@ import (
1616
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
1717
)
1818

19-
var ptBool = reflect.TypeOf((*bool)(nil))
20-
var ptInt8 = reflect.TypeOf((*int8)(nil))
21-
var ptInt16 = reflect.TypeOf((*int16)(nil))
22-
var ptInt32 = reflect.TypeOf((*int32)(nil))
23-
var ptInt64 = reflect.TypeOf((*int64)(nil))
24-
var ptInt = reflect.TypeOf((*int)(nil))
25-
var ptUint8 = reflect.TypeOf((*uint8)(nil))
26-
var ptUint16 = reflect.TypeOf((*uint16)(nil))
27-
var ptUint32 = reflect.TypeOf((*uint32)(nil))
28-
var ptUint64 = reflect.TypeOf((*uint64)(nil))
29-
var ptUint = reflect.TypeOf((*uint)(nil))
30-
var ptFloat32 = reflect.TypeOf((*float32)(nil))
31-
var ptFloat64 = reflect.TypeOf((*float64)(nil))
32-
var ptString = reflect.TypeOf((*string)(nil))
33-
3419
var tBool = reflect.TypeOf(false)
35-
var tFloat32 = reflect.TypeOf(float32(0))
3620
var tFloat64 = reflect.TypeOf(float64(0))
37-
var tInt = reflect.TypeOf(int(0))
38-
var tInt8 = reflect.TypeOf(int8(0))
39-
var tInt16 = reflect.TypeOf(int16(0))
4021
var tInt32 = reflect.TypeOf(int32(0))
4122
var tInt64 = reflect.TypeOf(int64(0))
4223
var tString = reflect.TypeOf("")
4324
var tTime = reflect.TypeOf(time.Time{})
44-
var tUint = reflect.TypeOf(uint(0))
45-
var tUint8 = reflect.TypeOf(uint8(0))
46-
var tUint16 = reflect.TypeOf(uint16(0))
47-
var tUint32 = reflect.TypeOf(uint32(0))
48-
var tUint64 = reflect.TypeOf(uint64(0))
4925

5026
var tEmpty = reflect.TypeOf((*interface{})(nil)).Elem()
5127
var tByteSlice = reflect.TypeOf([]byte(nil))
@@ -74,7 +50,6 @@ var tDecimal = reflect.TypeOf(primitive.Decimal128{})
7450
var tMinKey = reflect.TypeOf(primitive.MinKey{})
7551
var tMaxKey = reflect.TypeOf(primitive.MaxKey{})
7652
var tD = reflect.TypeOf(primitive.D{})
77-
var tM = reflect.TypeOf(primitive.M{})
7853
var tA = reflect.TypeOf(primitive.A{})
7954
var tE = reflect.TypeOf(primitive.E{})
8055

bson/bsonrw/extjson_writer.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ import (
2222
"go.mongodb.org/mongo-driver/bson/primitive"
2323
)
2424

25-
var ejvwPool = sync.Pool{
26-
New: func() interface{} {
27-
return new(extJSONValueWriter)
28-
},
29-
}
30-
3125
// ExtJSONValueWriterPool is a pool for ExtJSON ValueWriters.
3226
type ExtJSONValueWriterPool struct {
3327
pool sync.Pool

bson/bsonrw/value_reader_writer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
type VRWInvoked byte
1818

1919
const (
20-
llvrwNothing VRWInvoked = iota
21-
llvrwReadArray
20+
_ = iota
21+
llvrwReadArray VRWInvoked = 1
2222
llvrwReadBinary
2323
llvrwReadBoolean
2424
llvrwReadDocument

bson/bsonrw/value_writer_test.go

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -366,73 +366,3 @@ type errWriter struct {
366366
}
367367

368368
func (ew errWriter) Write([]byte) (int, error) { return 0, ew.err }
369-
370-
func vwBasicDoc(t *testing.T, vw *valueWriter) {
371-
dw, err := vw.WriteDocument()
372-
noerr(t, err)
373-
vw2, err := dw.WriteDocumentElement("foo")
374-
noerr(t, err)
375-
err = vw2.WriteBoolean(true)
376-
noerr(t, err)
377-
err = dw.WriteDocumentEnd()
378-
noerr(t, err)
379-
380-
return
381-
}
382-
383-
func vwBasicArray(t *testing.T, vw *valueWriter) {
384-
dw, err := vw.WriteDocument()
385-
noerr(t, err)
386-
vw2, err := dw.WriteDocumentElement("foo")
387-
noerr(t, err)
388-
aw, err := vw2.WriteArray()
389-
noerr(t, err)
390-
vw2, err = aw.WriteArrayElement()
391-
noerr(t, err)
392-
err = vw2.WriteBoolean(true)
393-
noerr(t, err)
394-
err = aw.WriteArrayEnd()
395-
noerr(t, err)
396-
err = dw.WriteDocumentEnd()
397-
noerr(t, err)
398-
399-
return
400-
}
401-
402-
func vwNestedDoc(t *testing.T, vw *valueWriter) {
403-
dw, err := vw.WriteDocument()
404-
noerr(t, err)
405-
vw2, err := dw.WriteDocumentElement("foo")
406-
noerr(t, err)
407-
dw2, err := vw2.WriteDocument()
408-
noerr(t, err)
409-
vw3, err := dw2.WriteDocumentElement("bar")
410-
noerr(t, err)
411-
err = vw3.WriteBoolean(true)
412-
noerr(t, err)
413-
err = dw2.WriteDocumentEnd()
414-
noerr(t, err)
415-
err = dw.WriteDocumentEnd()
416-
noerr(t, err)
417-
418-
return
419-
}
420-
421-
func vwCodeWithScopeNoNested(t *testing.T, vw *valueWriter) {
422-
dw, err := vw.WriteDocument()
423-
noerr(t, err)
424-
vw2, err := dw.WriteDocumentElement("foo")
425-
noerr(t, err)
426-
dw2, err := vw2.WriteCodeWithScope("var hello = world;")
427-
noerr(t, err)
428-
vw2, err = dw2.WriteDocumentElement("bar")
429-
noerr(t, err)
430-
err = vw2.WriteBoolean(false)
431-
noerr(t, err)
432-
err = dw2.WriteDocumentEnd()
433-
noerr(t, err)
434-
err = dw.WriteDocumentEnd()
435-
noerr(t, err)
436-
437-
return
438-
}

bson/encoder_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,6 @@ type byteMarshaler []byte
138138

139139
func (bm byteMarshaler) MarshalBSON() ([]byte, error) { return bm, nil }
140140

141-
type _Interface interface {
142-
method()
143-
}
144-
145141
type _impl struct {
146142
Foo string
147143
}

bson/marshal_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2323
)
2424

25+
var tInt32 = reflect.TypeOf(int32(0))
26+
2527
func TestMarshalAppendWithRegistry(t *testing.T) {
2628
for _, tc := range marshalingTestCases {
2729
t.Run(tc.name, func(t *testing.T) {

bson/mgocompat/bson_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,9 +1119,6 @@ type condTime struct {
11191119
type condStruct struct {
11201120
V struct{ A []int } `bson:",omitempty"`
11211121
}
1122-
type condRaw struct {
1123-
V bson.RawValue `bson:",omitempty"`
1124-
}
11251122

11261123
type shortInt struct {
11271124
V int64 `bson:",minsize"`
@@ -1142,9 +1139,6 @@ type shortNonEmptyInt struct {
11421139
type inlineInt struct {
11431140
V struct{ A, B int } `bson:",inline"`
11441141
}
1145-
type inlineCantPtr struct {
1146-
V *struct{ A, B int } `bson:",inline"`
1147-
}
11481142
type inlineDupName struct {
11491143
A int
11501144
V struct{ A, B int } `bson:",inline"`
@@ -1283,7 +1277,6 @@ var (
12831277
int64var = int64(42)
12841278
int64ptr = &int64var
12851279
intvar = int(42)
1286-
intptr = &intvar
12871280

12881281
gsintvar = getterSetterInt(42)
12891282
)

bson/primitive_codecs.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import (
1414
"go.mongodb.org/mongo-driver/bson/bsonrw"
1515
)
1616

17+
var tRawValue = reflect.TypeOf(RawValue{})
18+
var tRaw = reflect.TypeOf(Raw(nil))
19+
1720
var primitiveCodecs PrimitiveCodecs
1821

1922
// PrimitiveCodecs is a namespace for all of the default bsoncodec.Codecs for the primitive types

bson/primitive_codecs_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,3 @@ type zeroTest struct {
10701070
func (z zeroTest) IsZero() bool { return z.reportZero }
10711071

10721072
func compareZeroTest(_, _ zeroTest) bool { return true }
1073-
1074-
type nonZeroer struct {
1075-
value bool
1076-
}

bson/raw.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
// ErrNilReader indicates that an operation was attempted on a nil bson.Reader.
1717
var ErrNilReader = errors.New("nil reader")
18-
var errValidateDone = errors.New("validation loop complete")
1918

2019
// Raw is a wrapper around a byte slice. It will interpret the slice as a
2120
// BSON document. This type is a wrapper around a bsoncore.Document. Errors returned from the
@@ -84,9 +83,3 @@ func (r Raw) IndexErr(index uint) (RawElement, error) {
8483

8584
// String implements the fmt.Stringer interface.
8685
func (r Raw) String() string { return bsoncore.Document(r).String() }
87-
88-
// readi32 is a helper function for reading an int32 from slice of bytes.
89-
func readi32(b []byte) int32 {
90-
_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
91-
return int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
92-
}

bson/types.go

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77
package bson
88

99
import (
10-
"reflect"
11-
"time"
12-
1310
"go.mongodb.org/mongo-driver/bson/bsontype"
14-
"go.mongodb.org/mongo-driver/bson/primitive"
1511
)
1612

1713
// These constants uniquely refer to each BSON type.
@@ -38,48 +34,3 @@ const (
3834
TypeMinKey = bsontype.MinKey
3935
TypeMaxKey = bsontype.MaxKey
4036
)
41-
42-
var tBinary = reflect.TypeOf(primitive.Binary{})
43-
var tBool = reflect.TypeOf(false)
44-
var tCodeWithScope = reflect.TypeOf(primitive.CodeWithScope{})
45-
var tDBPointer = reflect.TypeOf(primitive.DBPointer{})
46-
var tDecimal = reflect.TypeOf(primitive.Decimal128{})
47-
var tD = reflect.TypeOf(D{})
48-
var tA = reflect.TypeOf(A{})
49-
var tDateTime = reflect.TypeOf(primitive.DateTime(0))
50-
var tUndefined = reflect.TypeOf(primitive.Undefined{})
51-
var tNull = reflect.TypeOf(primitive.Null{})
52-
var tRawValue = reflect.TypeOf(RawValue{})
53-
var tFloat32 = reflect.TypeOf(float32(0))
54-
var tFloat64 = reflect.TypeOf(float64(0))
55-
var tInt = reflect.TypeOf(int(0))
56-
var tInt8 = reflect.TypeOf(int8(0))
57-
var tInt16 = reflect.TypeOf(int16(0))
58-
var tInt32 = reflect.TypeOf(int32(0))
59-
var tInt64 = reflect.TypeOf(int64(0))
60-
var tJavaScript = reflect.TypeOf(primitive.JavaScript(""))
61-
var tOID = reflect.TypeOf(primitive.ObjectID{})
62-
var tRaw = reflect.TypeOf(Raw(nil))
63-
var tRegex = reflect.TypeOf(primitive.Regex{})
64-
var tString = reflect.TypeOf("")
65-
var tSymbol = reflect.TypeOf(primitive.Symbol(""))
66-
var tTime = reflect.TypeOf(time.Time{})
67-
var tTimestamp = reflect.TypeOf(primitive.Timestamp{})
68-
var tUint = reflect.TypeOf(uint(0))
69-
var tUint8 = reflect.TypeOf(uint8(0))
70-
var tUint16 = reflect.TypeOf(uint16(0))
71-
var tUint32 = reflect.TypeOf(uint32(0))
72-
var tUint64 = reflect.TypeOf(uint64(0))
73-
var tMinKey = reflect.TypeOf(primitive.MinKey{})
74-
var tMaxKey = reflect.TypeOf(primitive.MaxKey{})
75-
76-
var tEmpty = reflect.TypeOf((*interface{})(nil)).Elem()
77-
var tEmptySlice = reflect.TypeOf([]interface{}(nil))
78-
79-
var zeroVal reflect.Value
80-
81-
// this references the quantity of milliseconds between zero time and
82-
// the unix epoch. useful for making sure that we convert time.Time
83-
// objects correctly to match the legacy bson library's handling of
84-
// time.Time values.
85-
const zeroEpochMs = int64(62135596800000)

internal/testutil/ops.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ func DropCollection(t *testing.T, dbname, colname string) {
5454
}
5555
}
5656

57-
func autoDropDB(t *testing.T, topo *topology.Topology) {
58-
err := operation.NewCommand(bsoncore.BuildDocument(nil, bsoncore.AppendInt32Element(nil, "dropDatabase", 1))).
59-
Database(DBName(t)).ServerSelector(description.WriteSelector()).Deployment(topo).
60-
Execute(context.Background())
61-
require.NoError(t, err)
62-
}
63-
6457
// AutoInsertDocs inserts the docs into the test cluster.
6558
func AutoInsertDocs(t *testing.T, writeConcern *writeconcern.WriteConcern, docs ...bsoncore.Document) {
6659
InsertDocs(t, DBName(t), ColName(t), writeConcern, docs...)

0 commit comments

Comments
 (0)