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

etcd: modify declaring empty slices #14479

Merged
merged 2 commits into from
Sep 19, 2022
Merged
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
2 changes: 1 addition & 1 deletion client/pkg/srv/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func GetCluster(serviceScheme, service, name, dns string, apurls types.URLs) ([]
tcp2ap[tcpAddr.String()] = url
}

stringParts := []string{}
var stringParts []string
updateNodeMap := func(service, scheme string) error {
_, addrs, err := lookupSRV(service, "tcp", dns)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion client/pkg/srv/srv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestSRVGetCluster(t *testing.T) {
{Target: "2.example.com.", Port: 2480},
{Target: "3.example.com.", Port: 2480},
}
srvNone := []*net.SRV{}
var srvNone []*net.SRV

tests := []struct {
service string
Expand Down
3 changes: 2 additions & 1 deletion client/pkg/transport/listener_tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ func checkCertSAN(ctx context.Context, cert *x509.Certificate, remoteAddr string

func isHostInDNS(ctx context.Context, host string, dnsNames []string) (ok bool, err error) {
// reverse lookup
wildcards, names := []string{}, []string{}
var names []string
var wildcards []string
for _, dns := range dnsNames {
if strings.HasPrefix(dns, "*.") {
wildcards = append(wildcards, dns[1:])
Expand Down
2 changes: 1 addition & 1 deletion client/pkg/types/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (us *unsafeSet) Length() int {

// Values returns the values of the Set in an unspecified order.
func (us *unsafeSet) Values() (values []string) {
values = make([]string, 0)
values = make([]string, 0, len(us.d))
for val := range us.d {
values = append(values, val)
}
Expand Down
7 changes: 3 additions & 4 deletions client/pkg/types/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ func equal(a, b []string) bool {

func driveSetTests(t *testing.T, s Set) {
// Verify operations on an empty set
eValues := []string{}
values := s.Values()
if !reflect.DeepEqual(values, eValues) {
t.Fatalf("Expect values=%v got %v", eValues, values)
if len(values) != 0 {
t.Fatalf("Expect values=%v got %v", []string{}, values)
}
if l := s.Length(); l != 0 {
t.Fatalf("Expected length=0, got %d", l)
Expand All @@ -58,7 +57,7 @@ func driveSetTests(t *testing.T, s Set) {
s.Add("bar")
s.Add("baz")

eValues = []string{"foo", "bar", "baz"}
eValues := []string{"foo", "bar", "baz"}
values = s.Values()
if !equal(values, eValues) {
t.Fatalf("Expect values=%v got %v", eValues, values)
Expand Down
4 changes: 2 additions & 2 deletions etcdctl/ctlv3/command/del_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"time"

"github.com/spf13/cobra"
"go.etcd.io/etcd/client/v3"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/pkg/v3/cobrautl"
)

Expand Down Expand Up @@ -67,7 +67,7 @@ func getDelOp(args []string) (string, []clientv3.OpOption) {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("`--prefix` and `--from-key` cannot be set at the same time, choose one"))
}

opts := []clientv3.OpOption{}
var opts []clientv3.OpOption
key := args[0]
if len(args) > 1 {
if delPrefix || delFromKey {
Expand Down
12 changes: 6 additions & 6 deletions etcdctl/ctlv3/command/ep_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
"go.etcd.io/etcd/client/pkg/v3/logutil"
"go.etcd.io/etcd/client/v3"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/pkg/v3/cobrautl"
"go.etcd.io/etcd/pkg/v3/flags"

Expand Down Expand Up @@ -101,7 +101,7 @@ func epHealthCommandFunc(cmd *cobra.Command, args []string) {
ka := keepAliveTimeFromCmd(cmd)
kat := keepAliveTimeoutFromCmd(cmd)
auth := authCfgFromCmd(cmd)
cfgs := []*clientv3.Config{}
var cfgs []*clientv3.Config
for _, ep := range endpointsFromCluster(cmd) {
cfg, err := clientv3.NewClientConfig(&clientv3.ConfigSpec{
Endpoints: []string{ep},
Expand Down Expand Up @@ -172,7 +172,7 @@ func epHealthCommandFunc(cmd *cobra.Command, args []string) {
close(hch)

errs := false
healthList := []epHealth{}
var healthList []epHealth
for h := range hch {
healthList = append(healthList, h)
if h.Error != "" {
Expand All @@ -193,7 +193,7 @@ type epStatus struct {
func epStatusCommandFunc(cmd *cobra.Command, args []string) {
c := mustClientFromCmd(cmd)

statusList := []epStatus{}
var statusList []epStatus
var err error
for _, ep := range endpointsFromCluster(cmd) {
ctx, cancel := commandCtx(cmd)
Expand Down Expand Up @@ -222,7 +222,7 @@ type epHashKV struct {
func epHashKVCommandFunc(cmd *cobra.Command, args []string) {
c := mustClientFromCmd(cmd)

hashList := []epHashKV{}
var hashList []epHashKV
var err error
for _, ep := range endpointsFromCluster(cmd) {
ctx, cancel := commandCtx(cmd)
Expand Down Expand Up @@ -288,7 +288,7 @@ func endpointsFromCluster(cmd *cobra.Command) []string {
cobrautl.ExitWithError(cobrautl.ExitError, err)
}

ret := []string{}
var ret []string
for _, m := range membs.Members {
ret = append(ret, m.ClientURLs...)
}
Expand Down
4 changes: 2 additions & 2 deletions etcdctl/ctlv3/command/get_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"strings"

"github.com/spf13/cobra"
"go.etcd.io/etcd/client/v3"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/pkg/v3/cobrautl"
)

Expand Down Expand Up @@ -107,7 +107,7 @@ func getGetOp(args []string) (string, []clientv3.OpOption) {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("`--keys-only` and `--count-only` cannot be set at the same time, choose one"))
}

opts := []clientv3.OpOption{}
var opts []clientv3.OpOption
switch getConsistency {
case "s":
opts = append(opts, clientv3.WithSerializable())
Expand Down
4 changes: 2 additions & 2 deletions etcdctl/ctlv3/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"go.etcd.io/etcd/client/pkg/v3/logutil"
"go.etcd.io/etcd/client/pkg/v3/srv"
"go.etcd.io/etcd/client/pkg/v3/transport"
"go.etcd.io/etcd/client/v3"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/pkg/v3/cobrautl"
"go.etcd.io/etcd/pkg/v3/flags"

Expand Down Expand Up @@ -363,7 +363,7 @@ func endpointsFromFlagValue(cmd *cobra.Command) ([]string, error) {
return eps, err
}
// strip insecure connections
ret := []string{}
var ret []string
for _, ep := range eps {
if strings.HasPrefix(ep, "http://") {
fmt.Fprintf(os.Stderr, "ignoring discovered insecure endpoint %q\n", ep)
Expand Down
4 changes: 2 additions & 2 deletions etcdctl/ctlv3/command/make_mirror_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

"go.etcd.io/etcd/api/v3/mvccpb"
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
"go.etcd.io/etcd/client/v3"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/client/v3/mirror"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -191,7 +191,7 @@ func makeMirror(ctx context.Context, c *clientv3.Client, dc *clientv3.Client) er
}

var lastRev int64
ops := []clientv3.Op{}
var ops []clientv3.Op

for _, ev := range wr.Events {
nextRev := ev.Kv.ModRevision
Expand Down
4 changes: 2 additions & 2 deletions etcdctl/ctlv3/command/member_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"strings"

"github.com/spf13/cobra"
"go.etcd.io/etcd/client/v3"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/pkg/v3/cobrautl"
)

Expand Down Expand Up @@ -157,7 +157,7 @@ func memberAddCommandFunc(cmd *cobra.Command, args []string) {
display.MemberAdd(*resp)

if _, ok := (display).(*simplePrinter); ok {
conf := []string{}
var conf []string
for _, memb := range resp.Members {
for _, u := range memb.PeerURLs {
n := memb.Name
Expand Down
2 changes: 1 addition & 1 deletion etcdctl/ctlv3/command/put_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func getPutOp(args []string) (string, string, []clientv3.OpOption) {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("bad lease ID (%v), expecting ID in Hex", err))
}

opts := []clientv3.OpOption{}
var opts []clientv3.OpOption
if id != 0 {
opts = append(opts, clientv3.WithLease(clientv3.LeaseID(id)))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/osutil/interrupt_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var (
interruptRegisterMu, interruptExitMu sync.Mutex
// interruptHandlers holds all registered InterruptHandlers in order
// they will be executed.
interruptHandlers = []InterruptHandler{}
interruptHandlers []InterruptHandler
)

// RegisterInterruptHandler registers a new InterruptHandler. Handlers registered
Expand Down
2 changes: 1 addition & 1 deletion pkg/report/timeseries.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (t TimeSeries) String() string {
if err := wr.Write([]string{"UNIX-SECOND", "MIN-LATENCY-MS", "AVG-LATENCY-MS", "MAX-LATENCY-MS", "AVG-THROUGHPUT"}); err != nil {
log.Fatal(err)
}
rows := [][]string{}
var rows [][]string
for i := range t {
row := []string{
fmt.Sprintf("%d", t[i].Timestamp),
Expand Down
8 changes: 4 additions & 4 deletions raft/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestNodeStepUnblock(t *testing.T) {

// TestNodePropose ensures that node.Propose sends the given proposal to the underlying raft.
func TestNodePropose(t *testing.T) {
msgs := []raftpb.Message{}
var msgs []raftpb.Message
appendStep := func(r *raft, m raftpb.Message) error {
msgs = append(msgs, m)
return nil
Expand Down Expand Up @@ -166,7 +166,7 @@ func TestNodePropose(t *testing.T) {
// TestNodeReadIndex ensures that node.ReadIndex sends the MsgReadIndex message to the underlying raft.
// It also ensures that ReadState can be read out through ready chan.
func TestNodeReadIndex(t *testing.T) {
msgs := []raftpb.Message{}
var msgs []raftpb.Message
appendStep := func(r *raft, m raftpb.Message) error {
msgs = append(msgs, m)
return nil
Expand Down Expand Up @@ -306,7 +306,7 @@ func TestNodeReadIndexToOldLeader(t *testing.T) {
// TestNodeProposeConfig ensures that node.ProposeConfChange sends the given configuration proposal
// to the underlying raft.
func TestNodeProposeConfig(t *testing.T) {
msgs := []raftpb.Message{}
var msgs []raftpb.Message
appendStep := func(r *raft, m raftpb.Message) error {
msgs = append(msgs, m)
return nil
Expand Down Expand Up @@ -456,7 +456,7 @@ func TestBlockProposal(t *testing.T) {
}

func TestNodeProposeWaitDropped(t *testing.T) {
msgs := []raftpb.Message{}
var msgs []raftpb.Message
droppingMsg := []byte("test_dropping")
dropStep := func(r *raft, m raftpb.Message) error {
if m.Type == raftpb.MsgProp && strings.Contains(m.String(), string(droppingMsg)) {
Expand Down
6 changes: 3 additions & 3 deletions raft/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,13 +640,13 @@ func TestLogReplication(t *testing.T) {
t.Errorf("#%d.%d: committed = %d, want %d", i, j, sm.raftLog.committed, tt.wcommitted)
}

ents := []pb.Entry{}
var ents []pb.Entry
for _, e := range nextEnts(sm, tt.network.storage[j]) {
if e.Data != nil {
ents = append(ents, e)
}
}
props := []pb.Message{}
var props []pb.Message
for _, m := range tt.msgs {
if m.Type == pb.MsgProp {
props = append(props, m)
Expand Down Expand Up @@ -4747,7 +4747,7 @@ func (nw *network) recover() {
}

func (nw *network) filter(msgs []pb.Message) []pb.Message {
mm := []pb.Message{}
var mm []pb.Message
for _, m := range msgs {
if nw.ignorem[m.Type] {
continue
Expand Down
2 changes: 1 addition & 1 deletion raft/rawnode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ func TestRawNodeProposeAddDuplicateNode(t *testing.T) {
// TestRawNodeReadIndex ensures that Rawnode.ReadIndex sends the MsgReadIndex message
// to the underlying raft. It also ensures that ReadState can be read out.
func TestRawNodeReadIndex(t *testing.T) {
msgs := []pb.Message{}
var msgs []pb.Message
appendStep := func(r *raft, m pb.Message) error {
msgs = append(msgs, m)
return nil
Expand Down
2 changes: 1 addition & 1 deletion raft/read_only.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (ro *readOnly) advance(m pb.Message) []*readIndexStatus {
)

ctx := string(m.Context)
rss := []*readIndexStatus{}
var rss []*readIndexStatus

for _, okctx := range ro.readIndexQueue {
i++
Expand Down
4 changes: 2 additions & 2 deletions server/auth/store_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ func (t txMock) UnsafeGetRole(s string) *authpb.Role {
}

func (t txMock) UnsafeGetAllUsers() []*authpb.User {
users := []*authpb.User{}
var users []*authpb.User
for _, u := range t.be.users {
users = append(users, u)
}
return users
}

func (t txMock) UnsafeGetAllRoles() []*authpb.Role {
roles := []*authpb.Role{}
var roles []*authpb.Role
for _, r := range t.be.roles {
roles = append(roles, r)
}
Expand Down
2 changes: 1 addition & 1 deletion server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (c *ServerConfig) advertiseMatchesCluster() error {
initMap[url.String()] = struct{}{}
}

missing := []string{}
var missing []string
for url := range initMap {
if _, ok := apMap[url]; !ok {
missing = append(missing, url)
Expand Down
2 changes: 1 addition & 1 deletion server/embed/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ func (e *Etcd) serveClients() (err error) {
etcdhttp.HandleMetrics(mux)
etcdhttp.HandleHealth(e.cfg.logger, mux, e.Server)

gopts := []grpc.ServerOption{}
var gopts []grpc.ServerOption
if e.cfg.GRPCKeepAliveMinTime > time.Duration(0) {
gopts = append(gopts, grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
MinTime: e.cfg.GRPCKeepAliveMinTime,
Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/api/snap/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (s *Snapshotter) snapNames() ([]string, error) {
}

func (s *Snapshotter) checkSuffix(names []string) []string {
snaps := []string{}
var snaps []string
for i := range names {
if strings.HasSuffix(names[i], snapSuffix) {
snaps = append(snaps, names[i])
Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ func TestApplyMultiConfChangeShouldStop(t *testing.T) {
consistIndex: ci,
beHooks: serverstorage.NewBackendHooks(lg, ci),
}
ents := []raftpb.Entry{}
var ents []raftpb.Entry
for i := 1; i <= 4; i++ {
ent := raftpb.Entry{
Term: 1,
Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/txn/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ func compareKV(c *pb.Compare, ckv mvccpb.KeyValue) bool {
rev := int64(0)
switch c.Target {
case pb.Compare_VALUE:
v := []byte{}
var v []byte
if tv, _ := c.TargetUnion.(*pb.Compare_Value); tv != nil {
v = tv.Value
}
Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func newCluster(lg *zap.Logger, memberCount int, ver semver.Version) *clusterMoc

func (c *clusterMock) StepMonitors() {
// Execute monitor functions in random order as it is not guaranteed
fs := []func(){}
var fs []func()
for _, m := range c.members {
fs = append(fs, m.monitor.UpdateStorageVersionIfNeeded)
if m.isLeader {
Expand Down
2 changes: 1 addition & 1 deletion server/lease/lessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ func (le *lessor) findDueScheduledCheckpoints(checkpointLimit int) []*pb.LeaseCh
}

now := time.Now()
cps := []*pb.LeaseCheckpoint{}
var cps []*pb.LeaseCheckpoint
for le.leaseCheckpointHeap.Len() > 0 && len(cps) < checkpointLimit {
lt := le.leaseCheckpointHeap[0]
if lt.time.After(now) /* lt.time: next checkpoint time */ {
Expand Down
Loading