From fc207de99ac2aa7a95175f37331cee415c4ca75a Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Sat, 30 Nov 2024 15:55:12 +0100 Subject: [PATCH] fix: enable gosimple linter Signed-off-by: Matthieu MOREL --- etcdctl/ctlv3/command/defrag_command.go | 2 +- etcdctl/main.go | 1 - server/etcdserver/api/v3discovery/discovery.go | 2 +- server/etcdserver/cluster_util.go | 3 +-- server/etcdserver/corrupt.go | 1 - server/etcdserver/server.go | 2 +- server/storage/mvcc/hash.go | 4 +--- server/storage/wal/version.go | 5 +---- tests/e2e/etcd_grpcproxy_test.go | 5 +---- tests/e2e/v2store_deprecation_test.go | 4 ++-- tests/e2e/v3_curl_cluster_test.go | 5 +++-- tests/e2e/v3_curl_maintenance_test.go | 5 ++--- tests/integration/clientv3/concurrency/example_stm_test.go | 1 - tests/integration/revision_test.go | 2 +- tests/robustness/model/describe.go | 6 +++--- tests/robustness/model/replay.go | 4 +--- tests/robustness/traffic/limiter.go | 2 +- tools/.golangci.yaml | 4 ++++ 18 files changed, 24 insertions(+), 34 deletions(-) diff --git a/etcdctl/ctlv3/command/defrag_command.go b/etcdctl/ctlv3/command/defrag_command.go index 253847746a8b..36116d4888c7 100644 --- a/etcdctl/ctlv3/command/defrag_command.go +++ b/etcdctl/ctlv3/command/defrag_command.go @@ -44,7 +44,7 @@ func defragCommandFunc(cmd *cobra.Command, args []string) { ctx, cancel := commandCtx(cmd) start := time.Now() _, err := c.Defragment(ctx, ep) - d := time.Now().Sub(start) + d := time.Since(start) cancel() if err != nil { fmt.Fprintf(os.Stderr, "Failed to defragment etcd member[%s]. took %s. (%v)\n", ep, d.String(), err) diff --git a/etcdctl/main.go b/etcdctl/main.go index e53059e241d9..f4a17434eb25 100644 --- a/etcdctl/main.go +++ b/etcdctl/main.go @@ -21,5 +21,4 @@ import ( func main() { ctlv3.MustStart() - return } diff --git a/server/etcdserver/api/v3discovery/discovery.go b/server/etcdserver/api/v3discovery/discovery.go index 3bfd4c9cbd04..7fe231cdb559 100644 --- a/server/etcdserver/api/v3discovery/discovery.go +++ b/server/etcdserver/api/v3discovery/discovery.go @@ -450,7 +450,7 @@ func (cls *clusterInfo) add(memberKey, memberValue string, rev int64) error { return errors.New("invalid peer registry key") } - if strings.IndexRune(memberValue, '=') == -1 { + if !strings.ContainsRune(memberValue, '=') { // It must be in the format "member1=http://127.0.0.1:2380". return errors.New("invalid peer info returned from discovery service") } diff --git a/server/etcdserver/cluster_util.go b/server/etcdserver/cluster_util.go index 12e5e9c2d247..089d2acf71bc 100644 --- a/server/etcdserver/cluster_util.go +++ b/server/etcdserver/cluster_util.go @@ -433,8 +433,7 @@ func convertToClusterVersion(v string) (*semver.Version, error) { } func GetMembershipInfoInV2Format(lg *zap.Logger, cl *membership.RaftCluster) []byte { - var st v2store.Store - st = v2store.New(StoreClusterPrefix, StoreKeysPrefix) + st := v2store.New(StoreClusterPrefix, StoreKeysPrefix) cl.Store(st) d, err := st.SaveNoCopy() if err != nil { diff --git a/server/etcdserver/corrupt.go b/server/etcdserver/corrupt.go index 5c16ffb7c9ca..5ec111bef43f 100644 --- a/server/etcdserver/corrupt.go +++ b/server/etcdserver/corrupt.go @@ -292,7 +292,6 @@ func (cm *corruptionChecker) CompactHashCheck() { } } cm.lg.Info("finished compaction hash check", zap.Int("number-of-hashes-checked", len(hashes))) - return } // check peers hash and raise alarms if detected corruption. diff --git a/server/etcdserver/server.go b/server/etcdserver/server.go index 446606431a43..2677e929f982 100644 --- a/server/etcdserver/server.go +++ b/server/etcdserver/server.go @@ -1197,7 +1197,7 @@ func (s *EtcdServer) ForceSnapshot() { } func (s *EtcdServer) snapshotIfNeededAndCompactRaftLog(ep *etcdProgress) { - //TODO: Remove disk snapshot in v3.7 + // TODO: Remove disk snapshot in v3.7 shouldSnapshotToDisk := s.shouldSnapshotToDisk(ep) shouldSnapshotToMemory := s.shouldSnapshotToMemory(ep) if !shouldSnapshotToDisk && !shouldSnapshotToMemory { diff --git a/server/storage/mvcc/hash.go b/server/storage/mvcc/hash.go index 31846f6116ee..21618535da40 100644 --- a/server/storage/mvcc/hash.go +++ b/server/storage/mvcc/hash.go @@ -174,9 +174,7 @@ func (s *hashStorage) Hashes() []KeyValueHash { s.hashMu.RLock() // Copy out hashes under lock just to be safe hashes := make([]KeyValueHash, 0, len(s.hashes)) - for _, hash := range s.hashes { - hashes = append(hashes, hash) - } + hashes = append(hashes, s.hashes...) s.hashMu.RUnlock() return hashes } diff --git a/server/storage/wal/version.go b/server/storage/wal/version.go index 8ed47c783d85..985926503037 100644 --- a/server/storage/wal/version.go +++ b/server/storage/wal/version.go @@ -187,10 +187,7 @@ func visitMessage(m protoreflect.Message, visitor Visitor) error { case protoreflect.EnumNumber: err = visitEnumNumber(fd.Enum(), m, visitor) } - if err != nil { - return false - } - return true + return err == nil }) return err } diff --git a/tests/e2e/etcd_grpcproxy_test.go b/tests/e2e/etcd_grpcproxy_test.go index a45613c2812f..02174e89f626 100644 --- a/tests/e2e/etcd_grpcproxy_test.go +++ b/tests/e2e/etcd_grpcproxy_test.go @@ -137,10 +137,7 @@ func waitForEndpointInLog(ctx context.Context, proxyProc *expect.ExpectProcess, defer cancel() _, err := proxyProc.ExpectFunc(ctx, func(s string) bool { - if strings.Contains(s, endpoint) { - return true - } - return false + return strings.Contains(s, endpoint) }) return err diff --git a/tests/e2e/v2store_deprecation_test.go b/tests/e2e/v2store_deprecation_test.go index 77b81a7c6e83..caaf7b2c66d3 100644 --- a/tests/e2e/v2store_deprecation_test.go +++ b/tests/e2e/v2store_deprecation_test.go @@ -271,11 +271,11 @@ func assertSnapshotsMatch(t testing.TB, firstDataDir, secondDataDir string, patc func assertMembershipEqual(t testing.TB, firstStore v2store.Store, secondStore v2store.Store) { rc1 := membership.NewCluster(zaptest.NewLogger(t)) rc1.SetStore(firstStore) - rc1.Recover(func(lg *zap.Logger, v *semver.Version) { return }) + rc1.Recover(func(lg *zap.Logger, v *semver.Version) {}) rc2 := membership.NewCluster(zaptest.NewLogger(t)) rc2.SetStore(secondStore) - rc2.Recover(func(lg *zap.Logger, v *semver.Version) { return }) + rc2.Recover(func(lg *zap.Logger, v *semver.Version) {}) // membership should match if !reflect.DeepEqual(rc1.Members(), rc2.Members()) { diff --git a/tests/e2e/v3_curl_cluster_test.go b/tests/e2e/v3_curl_cluster_test.go index 688f8dcaca00..4d8ae946f616 100644 --- a/tests/e2e/v3_curl_cluster_test.go +++ b/tests/e2e/v3_curl_cluster_test.go @@ -59,9 +59,10 @@ func testCurlV3ClusterOperations(cx ctlCtx) { var newMemberIDStr string for _, m := range members { mObj := m.(map[string]any) - pURLs, _ := mObj["peerURLs"] - pURL := pURLs.([]any)[0].(string) + require.Contains(cx.t, mObj, "peerURLs") + pURL := mObj["peerURLs"].([]any)[0].(string) if pURL == peerURL { + require.Contains(cx.t, mObj, "ID") newMemberIDStr = mObj["ID"].(string) break } diff --git a/tests/e2e/v3_curl_maintenance_test.go b/tests/e2e/v3_curl_maintenance_test.go index de8b837f60ea..85e68546a0cf 100644 --- a/tests/e2e/v3_curl_maintenance_test.go +++ b/tests/e2e/v3_curl_maintenance_test.go @@ -57,9 +57,8 @@ func testCurlV3MaintenanceStatus(cx ctlCtx) { cx.t.Fatalf("Field %q not found in (%v)", field, resp) } } - - actualVersion, _ := resp["version"] - require.Equal(cx.t, version.Version, actualVersion) + require.Contains(cx.t, resp, "version") + require.Equal(cx.t, version.Version, resp["version"]) } func TestCurlV3MaintenanceDefragment(t *testing.T) { diff --git a/tests/integration/clientv3/concurrency/example_stm_test.go b/tests/integration/clientv3/concurrency/example_stm_test.go index bff2914f96b4..074976b94964 100644 --- a/tests/integration/clientv3/concurrency/example_stm_test.go +++ b/tests/integration/clientv3/concurrency/example_stm_test.go @@ -70,7 +70,6 @@ func ExampleSTM_apply() { // write back stm.Put(fromK, fmt.Sprintf("%d", fromInt)) stm.Put(toK, fmt.Sprintf("%d", toInt)) - return } // concurrently exchange values between accounts diff --git a/tests/integration/revision_test.go b/tests/integration/revision_test.go index 74792546820d..e5c1d270fac0 100644 --- a/tests/integration/revision_test.go +++ b/tests/integration/revision_test.go @@ -149,7 +149,7 @@ func silenceConnectionErrors(err error) error { } s := status.Convert(err) for _, msg := range connectionErrorMessages { - if strings.Index(s.Message(), msg) != -1 { + if strings.Contains(s.Message(), msg) { return nil } } diff --git a/tests/robustness/model/describe.go b/tests/robustness/model/describe.go index a8c3bc1556d7..71f2b15f26b3 100644 --- a/tests/robustness/model/describe.go +++ b/tests/robustness/model/describe.go @@ -32,7 +32,7 @@ func describeEtcdResponse(request EtcdRequest, response MaybeEtcdResponse) strin if response.PersistedRevision != 0 { return fmt.Sprintf("unknown, rev: %d", response.PersistedRevision) } - return fmt.Sprintf("unknown") + return "unknown" } switch request.Type { case Range: @@ -74,7 +74,7 @@ func describeEtcdRequest(request EtcdRequest) string { case LeaseRevoke: return fmt.Sprintf("leaseRevoke(%d)", request.LeaseRevoke.LeaseID) case Defragment: - return fmt.Sprintf("defragment()") + return "defragment()" case Compact: return fmt.Sprintf("compact(%d)", request.Compact.Revision) default: @@ -182,7 +182,7 @@ func describeEtcdOperationResponse(op EtcdOperation, resp EtcdOperationResult) s case RangeOperation: return describeRangeResponse(op.Range, resp.RangeResponse) case PutOperation: - return fmt.Sprintf("ok") + return "ok" case DeleteOperation: return fmt.Sprintf("deleted: %d", resp.Deleted) default: diff --git a/tests/robustness/model/replay.go b/tests/robustness/model/replay.go index b8ea784683a3..7f2be26e8ed2 100644 --- a/tests/robustness/model/replay.go +++ b/tests/robustness/model/replay.go @@ -30,9 +30,7 @@ func NewReplay(persistedRequests []EtcdRequest) *EtcdReplay { if state.Revision != newState.Revision { revisionToEtcdState = append(revisionToEtcdState, newState) } - for _, e := range toWatchEvents(&state, request, response) { - events = append(events, e) - } + events = append(events, toWatchEvents(&state, request, response)...) state = newState } return &EtcdReplay{ diff --git a/tests/robustness/traffic/limiter.go b/tests/robustness/traffic/limiter.go index d5db965d5b7c..f2561f4168cd 100644 --- a/tests/robustness/traffic/limiter.go +++ b/tests/robustness/traffic/limiter.go @@ -40,7 +40,7 @@ func (c *concurrencyLimiter) Take() bool { func (c *concurrencyLimiter) Return() { select { - case _ = <-c.ch: + case <-c.ch: default: panic("Call to Return() without a successful Take") } diff --git a/tools/.golangci.yaml b/tools/.golangci.yaml index ecb8a43d10aa..b7320f6a3b45 100644 --- a/tools/.golangci.yaml +++ b/tools/.golangci.yaml @@ -8,6 +8,9 @@ issues: # exclude ineffassing linter for generated files for conversion - path: conversion\.go linters: [ineffassign] + - text: "S1000" # TODO: Fix me + linters: + - gosimple exclude-files: - ^zz_generated.* linters: @@ -21,6 +24,7 @@ linters: - errorlint - gofumpt - goimports + - gosimple - ineffassign - nakedret - revive