Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: align golangci-lint with etcd #918

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,62 @@ linters:
disable-all: true
enable: # please keep this alphabetized
- errcheck
- gofmt
# - errorlint
- gofumpt
- goimports
- gosimple
- govet
- ineffassign
- nakedret
- revive
- staticcheck
# - stylecheck
# - testifylint
# - unconvert # Remove unnecessary type conversions
# - unparam
- unused
- usestdlibvars
- whitespace
linters-settings: # please keep this alphabetized
goimports:
local-prefixes: go.etcd.io # Put imports beginning with prefix after 3rd-party packages.
revive:
rules:
- name: blank-imports
- name: context-as-argument
- name: context-keys-type
- name: dot-imports
- name: early-return
disabled: true
arguments:
- "preserveScope"
- name: error-return
- name: error-naming
disabled: true
- name: error-strings
disabled: true
- name: errorf
- name: exported
- name: if-return
disabled: true
- name: increment-decrement
disabled: true
- name: indent-error-flow
disabled: true
- name: package-comments
- name: range
- name: receiver-naming
disabled: true
- name: superfluous-else
disabled: true
arguments:
- "preserveScope"
- name: time-naming
- name: unexported-return
disabled: true
- name: use-any
disabled: true
- name: var-declaration
disabled: true
- name: var-naming
disabled: true
2 changes: 1 addition & 1 deletion bolt_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func mmap(db *DB, sz int) error {
err = unix.Madvise(b, syscall.MADV_RANDOM)
if err != nil && err != syscall.ENOSYS {
// Ignore not implemented error in kernel because it still works.
return fmt.Errorf("madvise: %s", err)
return fmt.Errorf("madvise: %w", err)
}

// Save the original byte slice and convert to a byte array pointer.
Expand Down
42 changes: 21 additions & 21 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Bucket struct {

// newBucket returns a new bucket associated with a transaction.
func newBucket(tx *Tx) Bucket {
var b = Bucket{tx: tx, FillPercent: DefaultFillPercent}
b := Bucket{tx: tx, FillPercent: DefaultFillPercent}
if tx.writable {
b.buckets = make(map[string]*Bucket)
b.nodes = make(map[common.Pgid]*node)
Expand Down Expand Up @@ -102,7 +102,7 @@ func (b *Bucket) Bucket(name []byte) *Bucket {
}

// Otherwise create a bucket and cache it.
var child = b.openBucket(v)
child := b.openBucket(v)
if b.buckets != nil {
b.buckets[string(name)] = child
}
Expand All @@ -113,7 +113,7 @@ func (b *Bucket) Bucket(name []byte) *Bucket {
// Helper method that re-interprets a sub-bucket value
// from a parent into a Bucket
func (b *Bucket) openBucket(value []byte) *Bucket {
var child = newBucket(b.tx)
child := newBucket(b.tx)

// Unaligned access requires a copy to be made.
const unalignedMask = unsafe.Alignof(struct {
Expand Down Expand Up @@ -182,12 +182,12 @@ func (b *Bucket) CreateBucket(key []byte) (rb *Bucket, err error) {
}

// Create empty, inline bucket.
var bucket = Bucket{
bucket := Bucket{
InBucket: &common.InBucket{},
rootNode: &node{isLeaf: true},
FillPercent: DefaultFillPercent,
}
var value = bucket.write()
value := bucket.write()

c.node().put(newKey, newKey, value, 0, common.BucketLeafFlag)

Expand Down Expand Up @@ -240,7 +240,7 @@ func (b *Bucket) CreateBucketIfNotExists(key []byte) (rb *Bucket, err error) {
// Return an error if there is an existing non-bucket key.
if bytes.Equal(newKey, k) {
if (flags & common.BucketLeafFlag) != 0 {
var child = b.openBucket(v)
child := b.openBucket(v)
if b.buckets != nil {
b.buckets[string(newKey)] = child
}
Expand All @@ -251,12 +251,12 @@ func (b *Bucket) CreateBucketIfNotExists(key []byte) (rb *Bucket, err error) {
}

// Create empty, inline bucket.
var bucket = Bucket{
bucket := Bucket{
InBucket: &common.InBucket{},
rootNode: &node{isLeaf: true},
FillPercent: DefaultFillPercent,
}
var value = bucket.write()
value := bucket.write()

c.node().put(newKey, newKey, value, 0, common.BucketLeafFlag)

Expand Down Expand Up @@ -305,7 +305,7 @@ func (b *Bucket) DeleteBucket(key []byte) (err error) {
child := b.Bucket(newKey)
err = child.ForEachBucket(func(k []byte) error {
if err := child.DeleteBucket(k); err != nil {
return fmt.Errorf("delete bucket: %s", err)
return fmt.Errorf("delete bucket: %w", err)
}
return nil
})
Expand Down Expand Up @@ -716,7 +716,7 @@ func (b *Bucket) forEachPageNode(fn func(*common.Page, *node, int)) {
}

func (b *Bucket) _forEachPageNode(pgId common.Pgid, depth int, fn func(*common.Page, *node, int)) {
var p, n = b.pageNode(pgId)
p, n := b.pageNode(pgId)

// Execute function.
fn(p, n, depth)
Expand Down Expand Up @@ -756,7 +756,7 @@ func (b *Bucket) spill() error {

// Update the child bucket header in this bucket.
value = make([]byte, unsafe.Sizeof(common.InBucket{}))
var bucket = (*common.InBucket)(unsafe.Pointer(&value[0]))
bucket := (*common.InBucket)(unsafe.Pointer(&value[0]))
*bucket = *child.InBucket
}

Expand All @@ -766,7 +766,7 @@ func (b *Bucket) spill() error {
}

// Update parent node.
var c = b.Cursor()
c := b.Cursor()
k, _, flags := c.seek([]byte(name))
if !bytes.Equal([]byte(name), k) {
panic(fmt.Sprintf("misplaced bucket header: %x -> %x", []byte(name), k))
Expand Down Expand Up @@ -800,7 +800,7 @@ func (b *Bucket) spill() error {
// inlineable returns true if a bucket is small enough to be written inline
// and if it contains no subbuckets. Otherwise, returns false.
func (b *Bucket) inlineable() bool {
var n = b.rootNode
n := b.rootNode

// Bucket must only contain a single leaf node.
if n == nil || !n.isLeaf {
Expand All @@ -809,7 +809,7 @@ func (b *Bucket) inlineable() bool {

// Bucket is not inlineable if it contains subbuckets or if it goes beyond
// our threshold for inline bucket size.
var size = common.PageHeaderSize
size := common.PageHeaderSize
for _, inode := range n.inodes {
size += common.LeafPageElementSize + uintptr(len(inode.Key())) + uintptr(len(inode.Value()))

Expand All @@ -831,15 +831,15 @@ func (b *Bucket) maxInlineBucketSize() uintptr {
// write allocates and writes a bucket to a byte slice.
func (b *Bucket) write() []byte {
// Allocate the appropriate size.
var n = b.rootNode
var value = make([]byte, common.BucketHeaderSize+n.size())
n := b.rootNode
value := make([]byte, common.BucketHeaderSize+n.size())

// Write a bucket header.
var bucket = (*common.InBucket)(unsafe.Pointer(&value[0]))
bucket := (*common.InBucket)(unsafe.Pointer(&value[0]))
*bucket = *b.InBucket

// Convert byte slice to a fake page and write the root node.
var p = (*common.Page)(unsafe.Pointer(&value[common.BucketHeaderSize]))
p := (*common.Page)(unsafe.Pointer(&value[common.BucketHeaderSize]))
n.write(p)

return value
Expand Down Expand Up @@ -873,7 +873,7 @@ func (b *Bucket) node(pgId common.Pgid, parent *node) *node {
}

// Use the inline page if this is an inline bucket.
var p = b.page
p := b.page
if p == nil {
p = b.tx.page(pgId)
} else {
Expand All @@ -900,7 +900,7 @@ func (b *Bucket) free() {
return
}

var tx = b.tx
tx := b.tx
b.forEachPageNode(func(p *common.Page, n *node, _ int) {
if p != nil {
tx.db.freelist.Free(tx.meta.Txid(), p)
Expand Down Expand Up @@ -993,7 +993,7 @@ func (s *BucketStats) Add(other BucketStats) {

// cloneBytes returns a copy of a given slice.
func cloneBytes(v []byte) []byte {
var clone = make([]byte, len(v))
clone := make([]byte, len(v))
copy(clone, v)
return clone
}
Expand Down
30 changes: 18 additions & 12 deletions bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ func TestBucket_Nested(t *testing.T) {

// Cause a split.
if err := db.Update(func(tx *bolt.Tx) error {
var b = tx.Bucket([]byte("widgets"))
b := tx.Bucket([]byte("widgets"))
for i := 0; i < 10000; i++ {
if err := b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {
t.Fatal(err)
Expand All @@ -523,7 +523,7 @@ func TestBucket_Nested(t *testing.T) {

// Insert into widgets/foo/baz.
if err := db.Update(func(tx *bolt.Tx) error {
var b = tx.Bucket([]byte("widgets"))
b := tx.Bucket([]byte("widgets"))
if err := b.Bucket([]byte("foo")).Put([]byte("baz"), []byte("yyyy")); err != nil {
t.Fatal(err)
}
Expand All @@ -535,7 +535,7 @@ func TestBucket_Nested(t *testing.T) {

// Verify.
if err := db.View(func(tx *bolt.Tx) error {
var b = tx.Bucket([]byte("widgets"))
b := tx.Bucket([]byte("widgets"))
if v := b.Bucket([]byte("foo")).Get([]byte("baz")); !bytes.Equal(v, []byte("yyyy")) {
t.Fatalf("unexpected value: %v", v)
}
Expand Down Expand Up @@ -1283,7 +1283,8 @@ func TestBucket_Stats(t *testing.T) {
1*10 + 2*90 + 3*400 + longKeyLength, // leaf values: 10 * 1digit, 90*2digits, ...
BucketN: 1,
InlineBucketN: 0,
InlineBucketInuse: 0},
InlineBucketInuse: 0,
},
16384: {
BranchPageN: 1,
BranchOverflowN: 0,
Expand All @@ -1301,7 +1302,8 @@ func TestBucket_Stats(t *testing.T) {
1*10 + 2*90 + 3*400 + longKeyLength, // leaf values: 10 * 1digit, 90*2digits, ...
BucketN: 1,
InlineBucketN: 0,
InlineBucketInuse: 0},
InlineBucketInuse: 0,
},
65536: {
BranchPageN: 1,
BranchOverflowN: 0,
Expand All @@ -1319,7 +1321,8 @@ func TestBucket_Stats(t *testing.T) {
1*10 + 2*90 + 3*400 + longKeyLength, // leaf values: 10 * 1digit, 90*2digits, ...
BucketN: 1,
InlineBucketN: 0,
InlineBucketInuse: 0},
InlineBucketInuse: 0,
},
}

if err := db.View(func(tx *bolt.Tx) error {
Expand Down Expand Up @@ -1775,7 +1778,8 @@ func TestBucket_Stats_Large(t *testing.T) {
LeafInuse: 2596916,
BucketN: 1,
InlineBucketN: 0,
InlineBucketInuse: 0},
InlineBucketInuse: 0,
},
16384: {
BranchPageN: 1,
BranchOverflowN: 0,
Expand All @@ -1789,7 +1793,8 @@ func TestBucket_Stats_Large(t *testing.T) {
LeafInuse: 2582452,
BucketN: 1,
InlineBucketN: 0,
InlineBucketInuse: 0},
InlineBucketInuse: 0,
},
65536: {
BranchPageN: 1,
BranchOverflowN: 0,
Expand All @@ -1803,7 +1808,8 @@ func TestBucket_Stats_Large(t *testing.T) {
LeafInuse: 2578948,
BucketN: 1,
InlineBucketN: 0,
InlineBucketInuse: 0},
InlineBucketInuse: 0,
},
}

if err := db.View(func(tx *bolt.Tx) error {
Expand Down Expand Up @@ -2021,7 +2027,7 @@ func BenchmarkBucket_CreateBucketIfNotExists(b *testing.B) {

func ExampleBucket_Put() {
// Open the database.
db, err := bolt.Open(tempfile(), 0600, nil)
db, err := bolt.Open(tempfile(), 0o600, nil)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -2064,7 +2070,7 @@ func ExampleBucket_Put() {

func ExampleBucket_Delete() {
// Open the database.
db, err := bolt.Open(tempfile(), 0600, nil)
db, err := bolt.Open(tempfile(), 0o600, nil)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -2122,7 +2128,7 @@ func ExampleBucket_Delete() {

func ExampleBucket_ForEach() {
// Open the database.
db, err := bolt.Open(tempfile(), 0600, nil)
db, err := bolt.Open(tempfile(), 0o600, nil)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/bbolt/command_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func checkFunc(cmd *cobra.Command, dbPath string, cfg checkOptions) error {
}

// Open database.
db, err := bolt.Open(dbPath, 0600, &bolt.Options{
db, err := bolt.Open(dbPath, 0o600, &bolt.Options{
ReadOnly: true,
PreLoadFreelist: true,
})
Expand Down
1 change: 0 additions & 1 deletion cmd/bbolt/command_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func TestCheckCommand_Run(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

t.Log("Creating sample DB")
db := btesting.MustCreateDB(t)
db.Close()
Expand Down
2 changes: 1 addition & 1 deletion cmd/bbolt/command_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func inspectFunc(srcDBPath string) error {
return err
}

db, err := bolt.Open(srcDBPath, 0600, &bolt.Options{ReadOnly: true})
db, err := bolt.Open(srcDBPath, 0o600, &bolt.Options{ReadOnly: true})
if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/bbolt/command_surgery.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (
"go.etcd.io/bbolt/internal/surgeon"
)

var (
ErrSurgeryFreelistAlreadyExist = errors.New("the file already has freelist, please consider to abandon the freelist to forcibly rebuild it")
)
var ErrSurgeryFreelistAlreadyExist = errors.New("the file already has freelist, please consider to abandon the freelist to forcibly rebuild it")

func newSurgeryCommand() *cobra.Command {
surgeryCmd := &cobra.Command{
Expand Down
Loading