Skip to content

Commit 839781e

Browse files
committed
chores(linter): enable rest of the linters
1 parent de9d880 commit 839781e

32 files changed

+186
-273
lines changed

.golangci.yml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ run:
55
linters-settings:
66
lll:
77
line-length: 120
8+
staticcheck:
9+
checks:
10+
- all
11+
- '-SA1019' # it is okay to use math/rand at times
812

913
linters:
1014
disable-all: true
1115
enable:
12-
# - errcheck
13-
# - ineffassign
14-
# - gas
16+
- errcheck
17+
- ineffassign
18+
# - gas
1519
- gofmt
16-
# - gosimple
17-
# - govet
18-
# - lll
19-
# - unused
20-
# - staticcheck
20+
- gosimple
21+
- govet
22+
- lll
23+
- unused
24+
- staticcheck
2125
- goimports

backup.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ func (stream *Stream) Backup(w io.Writer, since uint64) (uint64, error) {
7979
var valCopy []byte
8080
if !item.IsDeletedOrExpired() {
8181
// No need to copy value, if item is deleted or expired.
82-
var err error
83-
err = item.Value(func(val []byte) error {
82+
err := item.Value(func(val []byte) error {
8483
valCopy = a.Copy(val)
8584
return nil
8685
})

badger/cmd/flatten.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func flatten(cmd *cobra.Command, args []string) error {
6767
if err != nil {
6868
return err
6969
}
70-
if fo.compressionType < 0 || fo.compressionType > 2 {
70+
if fo.compressionType > 2 {
7171
return errors.Errorf(
7272
"compression value must be one of 0 (disabled), 1 (Snappy), or 2 (ZSTD)")
7373
}

badger/cmd/rotate_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ func TestRotatePlainTextToEncrypted(t *testing.T) {
108108
db, err := badger.Open(opts)
109109
require.NoError(t, err)
110110

111-
db.Update(func(txn *badger.Txn) error {
111+
require.NoError(t, db.Update(func(txn *badger.Txn) error {
112112
return txn.Set([]byte("foo"), []byte("bar"))
113-
})
113+
}))
114114

115115
require.NoError(t, db.Close())
116116

@@ -140,7 +140,7 @@ func TestRotatePlainTextToEncrypted(t *testing.T) {
140140
db, err = badger.Open(opts)
141141
require.NoError(t, err)
142142

143-
db.View(func(txn *badger.Txn) error {
143+
require.NoError(t, db.View(func(txn *badger.Txn) error {
144144
iopt := badger.DefaultIteratorOptions
145145
it := txn.NewIterator(iopt)
146146
defer it.Close()
@@ -150,6 +150,6 @@ func TestRotatePlainTextToEncrypted(t *testing.T) {
150150
}
151151
require.Equal(t, 1, count)
152152
return nil
153-
})
153+
}))
154154
require.NoError(t, db.Close())
155155
}

badger/cmd/stream.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func stream(cmd *cobra.Command, args []string) error {
8585
WithEncryptionKey(encKey)
8686

8787
// Options for output DB.
88-
if so.compressionType < 0 || so.compressionType > 2 {
88+
if so.compressionType > 2 {
8989
return errors.Errorf(
9090
"compression value must be one of 0 (disabled), 1 (Snappy), or 2 (ZSTD)")
9191
}
@@ -126,6 +126,7 @@ func stream(cmd *cobra.Command, args []string) error {
126126
f, err := os.OpenFile(so.outFile, os.O_RDWR|os.O_CREATE, 0666)
127127
y.Check(err)
128128
_, err = stream.Backup(f, 0)
129+
y.Check(err)
129130
}
130131
fmt.Println("Done.")
131132
return err

badger/cmd/write_bench.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func writeBench(cmd *cobra.Command, args []string) error {
314314
}
315315

316316
c.SignalAndWait()
317-
fmt.Printf(db.LevelsToString())
317+
fmt.Println(db.LevelsToString())
318318
return err
319319
}
320320

@@ -401,7 +401,7 @@ func reportStats(c *z.Closer, db *badger.DB) {
401401
humanize.IBytes(uint64(z.NumAllocBytes())))
402402

403403
if count%10 == 0 {
404-
fmt.Printf(db.LevelsToString())
404+
fmt.Println(db.LevelsToString())
405405
}
406406
}
407407
}

db.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ var (
4949
bannedNsKey = []byte("!badger!banned") // For storing the banned namespaces.
5050
)
5151

52-
const (
53-
maxNumSplits = 128
54-
)
55-
5652
type closers struct {
5753
updateSize *z.Closer
5854
compactors *z.Closer
@@ -1872,7 +1868,10 @@ func (db *DB) Subscribe(ctx context.Context, cb func(kv *KVList) error, matches
18721868
}
18731869

18741870
c := z.NewCloser(1)
1875-
s := db.pub.newSubscriber(c, matches)
1871+
s, err := db.pub.newSubscriber(c, matches)
1872+
if err != nil {
1873+
return y.Wrapf(err, "while creating a new subscriber")
1874+
}
18761875
slurp := func(batch *pb.KVList) error {
18771876
for {
18781877
select {
@@ -1926,11 +1925,6 @@ func (db *DB) Subscribe(ctx context.Context, cb func(kv *KVList) error, matches
19261925
}
19271926
}
19281927

1929-
// shouldEncrypt returns bool, which tells whether to encrypt or not.
1930-
func (db *DB) shouldEncrypt() bool {
1931-
return len(db.opt.EncryptionKey) > 0
1932-
}
1933-
19341928
func (db *DB) syncDir(dir string) error {
19351929
if db.opt.InMemory {
19361930
return nil
@@ -1971,7 +1965,7 @@ func (db *DB) StreamDB(outOptions Options) error {
19711965
defer outDB.Close()
19721966
writer := outDB.NewStreamWriter()
19731967
if err := writer.Prepare(); err != nil {
1974-
y.Wrapf(err, "cannot create stream writer in out DB at %s", outDir)
1968+
return y.Wrapf(err, "cannot create stream writer in out DB at %s", outDir)
19751969
}
19761970

19771971
// Stream contents of DB to the output DB.

db2_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ func TestMaxVersion(t *testing.T) {
883883
rand.Read(k)
884884
// Create multiple version of the same key.
885885
for i := 1; i <= N; i++ {
886-
wb.SetEntryAt(&Entry{Key: k}, uint64(i))
886+
require.NoError(t, wb.SetEntryAt(&Entry{Key: k}, uint64(i)))
887887
}
888888
require.NoError(t, wb.Flush())
889889

@@ -906,7 +906,7 @@ func TestMaxVersion(t *testing.T) {
906906

907907
// This will create commits from 1 to N.
908908
for i := 1; i <= N; i++ {
909-
wb.SetEntryAt(&Entry{Key: []byte(fmt.Sprintf("%d", i))}, uint64(i))
909+
require.NoError(t, wb.SetEntryAt(&Entry{Key: []byte(fmt.Sprintf("%d", i))}, uint64(i)))
910910
}
911911
require.NoError(t, wb.Flush())
912912

@@ -1001,12 +1001,12 @@ func TestKeyCount(t *testing.T) {
10011001

10021002
write := func(kvs *pb.KVList) error {
10031003
buf := z.NewBuffer(1<<20, "test")
1004-
defer buf.Release()
1004+
defer func() { require.NoError(t, buf.Release()) }()
10051005

10061006
for _, kv := range kvs.Kv {
10071007
KVToBuffer(kv, buf)
10081008
}
1009-
writer.Write(buf)
1009+
require.NoError(t, writer.Write(buf))
10101010
return nil
10111011
}
10121012

db_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ func TestGetMore(t *testing.T) {
610610
}
611611
require.NoError(t, txn.Commit())
612612
}
613-
db.validate()
613+
require.NoError(t, db.validate())
614614
for i := 0; i < n; i++ {
615615
if (i % 10000) == 0 {
616616
// Display some progress. Right now, it's not very fast with no caching.
@@ -643,7 +643,7 @@ func TestExistsMore(t *testing.T) {
643643
}
644644
require.NoError(t, txn.Commit())
645645
}
646-
db.validate()
646+
require.NoError(t, db.validate())
647647

648648
for i := 0; i < n; i++ {
649649
if (i % 1000) == 0 {
@@ -673,7 +673,7 @@ func TestExistsMore(t *testing.T) {
673673
}
674674
require.NoError(t, txn.Commit())
675675
}
676-
db.validate()
676+
require.NoError(t, db.validate())
677677
for i := 0; i < n; i++ {
678678
if (i % 10000) == 0 {
679679
// Display some progress. Right now, it's not very fast with no caching.
@@ -1231,7 +1231,7 @@ func TestDiscardVersionsBelow(t *testing.T) {
12311231
opts.PrefetchValues = false
12321232

12331233
// Verify that there are 4 versions, and record 3rd version (2nd from top in iteration)
1234-
db.View(func(txn *Txn) error {
1234+
require.NoError(t, db.View(func(txn *Txn) error {
12351235
it := txn.NewIterator(opts)
12361236
defer it.Close()
12371237
var count int
@@ -1245,7 +1245,7 @@ func TestDiscardVersionsBelow(t *testing.T) {
12451245
}
12461246
require.Equal(t, 4, count)
12471247
return nil
1248-
})
1248+
}))
12491249

12501250
// Set new version and discard older ones.
12511251
err := db.Update(func(txn *Txn) error {
@@ -1255,7 +1255,7 @@ func TestDiscardVersionsBelow(t *testing.T) {
12551255

12561256
// Verify that there are only 2 versions left, and versions
12571257
// below ts have been deleted.
1258-
db.View(func(txn *Txn) error {
1258+
require.NoError(t, db.View(func(txn *Txn) error {
12591259
it := txn.NewIterator(opts)
12601260
defer it.Close()
12611261
var count int
@@ -1269,7 +1269,7 @@ func TestDiscardVersionsBelow(t *testing.T) {
12691269
}
12701270
require.Equal(t, 1, count)
12711271
return nil
1272-
})
1272+
}))
12731273
})
12741274
}
12751275

@@ -1478,7 +1478,7 @@ func TestGetSetDeadlock(t *testing.T) {
14781478
timeout, done := time.After(10*time.Second), make(chan bool)
14791479

14801480
go func() {
1481-
db.Update(func(txn *Txn) error {
1481+
require.NoError(t, db.Update(func(txn *Txn) error {
14821482
item, err := txn.Get(key)
14831483
require.NoError(t, err)
14841484
err = item.Value(nil) // This take a RLock on file
@@ -1488,7 +1488,7 @@ func TestGetSetDeadlock(t *testing.T) {
14881488
require.NoError(t, txn.SetEntry(NewEntry(key, val)))
14891489
require.NoError(t, txn.SetEntry(NewEntry([]byte("key2"), val)))
14901490
return nil
1491-
})
1491+
}))
14921492
done <- true
14931493
}()
14941494

@@ -1818,9 +1818,9 @@ func TestMinReadTs(t *testing.T) {
18181818
db.orc.readMark.Done(uint64(20)) // Because we called readTs.
18191819

18201820
for i := 0; i < 10; i++ {
1821-
db.View(func(txn *Txn) error {
1821+
require.NoError(t, db.View(func(txn *Txn) error {
18221822
return nil
1823-
})
1823+
}))
18241824
}
18251825
time.Sleep(time.Millisecond)
18261826
require.Equal(t, uint64(20), db.orc.readMark.DoneUntil())
@@ -2089,7 +2089,7 @@ func TestVerifyChecksum(t *testing.T) {
20892089
st := 0
20902090

20912091
buf := z.NewBuffer(10<<20, "test")
2092-
defer buf.Release()
2092+
defer func() { require.NoError(t, buf.Release()) }()
20932093
for i := 0; i < 1000; i++ {
20942094
key := make([]byte, 8)
20952095
binary.BigEndian.PutUint64(key, uint64(i))
@@ -2153,12 +2153,12 @@ func TestWriteInemory(t *testing.T) {
21532153
item, err := txn.Get([]byte(fmt.Sprintf("key%d", j)))
21542154
require.NoError(t, err)
21552155
expected := []byte(fmt.Sprintf("val%d", j))
2156-
item.Value(func(val []byte) error {
2156+
require.NoError(t, item.Value(func(val []byte) error {
21572157
require.Equal(t, expected, val,
21582158
"Invalid value for key %q. expected: %q, actual: %q",
21592159
item.Key(), expected, val)
21602160
return nil
2161-
})
2161+
}))
21622162
}
21632163
return nil
21642164
})
@@ -2242,7 +2242,7 @@ func TestOpenDBReadOnly(t *testing.T) {
22422242
var count int
22432243
read := func() {
22442244
count = 0
2245-
db.View(func(txn *Txn) error {
2245+
require.NoError(t, db.View(func(txn *Txn) error {
22462246
it := txn.NewIterator(DefaultIteratorOptions)
22472247
defer it.Close()
22482248
for it.Rewind(); it.Valid(); it.Next() {
@@ -2254,7 +2254,7 @@ func TestOpenDBReadOnly(t *testing.T) {
22542254
count++
22552255
}
22562256
return nil
2257-
})
2257+
}))
22582258
}
22592259
read()
22602260
require.Equal(t, 10, count)

iterator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func TestIterateSinceTs(t *testing.T) {
147147
iopt := DefaultIteratorOptions
148148
iopt.SinceTs = sinceTs
149149

150-
db.View(func(txn *Txn) error {
150+
require.NoError(t, db.View(func(txn *Txn) error {
151151
it := txn.NewIterator(iopt)
152152
defer it.Close()
153153

@@ -156,7 +156,7 @@ func TestIterateSinceTs(t *testing.T) {
156156
require.GreaterOrEqual(t, i.Version(), sinceTs)
157157
}
158158
return nil
159-
})
159+
}))
160160

161161
})
162162
}

0 commit comments

Comments
 (0)