Skip to content

Commit

Permalink
Enable perfsprint linter (#2229)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu authored Oct 27, 2023
1 parent b83af9b commit 42d4e3e
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 21 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ linters:
- nakedret
- noctx
- nolintlint
- perfsprint
- prealloc
- revive
- staticcheck
Expand Down
4 changes: 2 additions & 2 deletions api/keystore/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package keystore

import (
"fmt"
"encoding/hex"
"math/rand"
"testing"

Expand Down Expand Up @@ -58,7 +58,7 @@ func TestServiceCreateUser(t *testing.T) {
func genStr(n int) string {
b := make([]byte, n)
rand.Read(b) // #nosec G404
return fmt.Sprintf("%x", b)[:n]
return hex.EncodeToString(b)[:n]
}

// TestServiceCreateUserArgsCheck generates excessively long usernames or
Expand Down
5 changes: 3 additions & 2 deletions genesis/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package genesis

import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -244,7 +245,7 @@ func TestGenesisFromFile(t *testing.T) {
genesisBytes, _, err := FromFile(test.networkID, customFile, genesisStakingCfg)
require.ErrorIs(err, test.expectedErr)
if test.expectedErr == nil {
genesisHash := fmt.Sprintf("%x", hashing.ComputeHash256(genesisBytes))
genesisHash := hex.EncodeToString(hashing.ComputeHash256(genesisBytes))
require.Equal(test.expectedHash, genesisHash, "genesis hash mismatch")

_, err = genesis.Parse(genesisBytes)
Expand Down Expand Up @@ -330,7 +331,7 @@ func TestGenesisFromFlag(t *testing.T) {
genesisBytes, _, err := FromFlag(test.networkID, content, genesisStakingCfg)
require.ErrorIs(err, test.expectedErr)
if test.expectedErr == nil {
genesisHash := fmt.Sprintf("%x", hashing.ComputeHash256(genesisBytes))
genesisHash := hex.EncodeToString(hashing.ComputeHash256(genesisBytes))
require.Equal(test.expectedHash, genesisHash, "genesis hash mismatch")

_, err = genesis.Parse(genesisBytes)
Expand Down
5 changes: 3 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net"
"os"
"path/filepath"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -244,7 +245,7 @@ func (n *Node) initNetworking() error {
//
// 1: https://apple.stackexchange.com/questions/393715/do-you-want-the-application-main-to-accept-incoming-network-connections-pop
// 2: https://github.com/golang/go/issues/56998
listenAddress := net.JoinHostPort(n.Config.ListenHost, fmt.Sprintf("%d", currentIPPort.Port))
listenAddress := net.JoinHostPort(n.Config.ListenHost, strconv.FormatUint(uint64(currentIPPort.Port), 10))

listener, err := net.Listen(constants.NetworkType, listenAddress)
if err != nil {
Expand Down Expand Up @@ -678,7 +679,7 @@ func (n *Node) initMetrics() {
func (n *Node) initAPIServer() error {
n.Log.Info("initializing API server")

listenAddress := net.JoinHostPort(n.Config.HTTPHost, fmt.Sprintf("%d", n.Config.HTTPPort))
listenAddress := net.JoinHostPort(n.Config.HTTPHost, strconv.FormatUint(uint64(n.Config.HTTPPort), 10))
listener, err := net.Listen("tcp", listenAddress)
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions tests/fixture/testnet/local/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io/fs"
"os"
"path/filepath"
"strconv"
"time"

"github.com/ava-labs/avalanchego/config"
Expand Down Expand Up @@ -65,7 +66,7 @@ func FindNextNetworkID(rootDir string) (uint32, string, error) {
continue
}

dirPath = filepath.Join(rootDir, fmt.Sprint(networkID))
dirPath = filepath.Join(rootDir, strconv.FormatUint(uint64(networkID), 10))
err := os.Mkdir(dirPath, perms.ReadWriteExecute)
if err == nil {
return networkID, dirPath, nil
Expand Down Expand Up @@ -304,7 +305,7 @@ func (ln *LocalNetwork) PopulateNodeConfig(node *LocalNode, nodeParentDir string
})

// Convert the network id to a string to ensure consistency in JSON round-tripping.
flags[config.NetworkNameKey] = fmt.Sprintf("%d", ln.Genesis.NetworkID)
flags[config.NetworkNameKey] = strconv.FormatUint(uint64(ln.Genesis.NetworkID), 10)

// Ensure keys are added if necessary
if err := node.EnsureKeys(); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions utils/crypto/bls/bls_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package bls

import (
"fmt"
"strconv"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -31,7 +31,7 @@ func BenchmarkSign(b *testing.B) {
privateKey, err := NewSecretKey()
require.NoError(b, err)
for _, messageSize := range sizes {
b.Run(fmt.Sprintf("%d", messageSize), func(b *testing.B) {
b.Run(strconv.Itoa(messageSize), func(b *testing.B) {
message := utils.RandomBytes(messageSize)

b.ResetTimer()
Expand All @@ -49,7 +49,7 @@ func BenchmarkVerify(b *testing.B) {
publicKey := PublicFromSecretKey(privateKey)

for _, messageSize := range sizes {
b.Run(fmt.Sprintf("%d", messageSize), func(b *testing.B) {
b.Run(strconv.Itoa(messageSize), func(b *testing.B) {
message := utils.RandomBytes(messageSize)
signature := Sign(privateKey, message)

Expand All @@ -72,7 +72,7 @@ func BenchmarkAggregatePublicKeys(b *testing.B) {
}

for _, size := range sizes {
b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
b.Run(strconv.Itoa(size), func(b *testing.B) {
for n := 0; n < b.N; n++ {
_, err := AggregatePublicKeys(keys[:size])
require.NoError(b, err)
Expand Down
2 changes: 1 addition & 1 deletion utils/ips/ip_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (ipPort IPPort) Equal(other IPPort) bool {
}

func (ipPort IPPort) String() string {
return net.JoinHostPort(ipPort.IP.String(), fmt.Sprintf("%d", ipPort.Port))
return net.JoinHostPort(ipPort.IP.String(), strconv.FormatUint(uint64(ipPort.Port), 10))
}

// IsZero returns if the IP or port is zeroed out
Expand Down
3 changes: 1 addition & 2 deletions utils/ips/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package ips

import (
"encoding/json"
"fmt"
"net"
"strconv"
"testing"
Expand Down Expand Up @@ -61,7 +60,7 @@ func TestIPPortEqual(t *testing.T) {
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
t.Run(strconv.Itoa(i), func(t *testing.T) {
require := require.New(t)

ipPort := IPDesc{}
Expand Down
4 changes: 2 additions & 2 deletions utils/sampler/rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
package sampler

import (
"fmt"
"math"
"math/rand"
"strconv"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestRNG(t *testing.T) {
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
t.Run(strconv.Itoa(i), func(t *testing.T) {
require := require.New(t)

source := &testSource{
Expand Down
4 changes: 2 additions & 2 deletions utils/set/bits.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package set

import (
"fmt"
"encoding/hex"
"math/big"
"math/bits"
)
Expand Down Expand Up @@ -98,5 +98,5 @@ func BitsFromBytes(bytes []byte) Bits {

// String returns the hex representation of this bitset
func (b Bits) String() string {
return fmt.Sprintf("%x", b.bits.Bytes())
return hex.EncodeToString(b.bits.Bytes())
}
4 changes: 2 additions & 2 deletions vms/platformvm/warp/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package warp

import (
"context"
"fmt"
"math"
"strconv"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -336,7 +336,7 @@ func BenchmarkGetCanonicalValidatorSet(b *testing.B) {
},
}

b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
b.Run(strconv.Itoa(size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _, err := GetCanonicalValidatorSet(context.Background(), validatorState, pChainHeight, subnetID)
require.NoError(b, err)
Expand Down

0 comments on commit 42d4e3e

Please sign in to comment.