Skip to content

Commit

Permalink
chore: enable early-return and superfluous-else from revive
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Jan 17, 2025
1 parent ecd7cfc commit 8f37a26
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 57 deletions.
5 changes: 2 additions & 3 deletions contrib/lock/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,10 @@ func main() {

err = write("key0", fmt.Sprintf("value from client %x", mode), int64(version))
if err != nil {
if mode == 1 {
log.Printf("expected fail to write to storage with old lease version: %s\n", err) // client 1 should show this message
} else {
if mode != 1 {
log.Fatalf("unexpected fail to write to storage: %s\n", err)
}
log.Printf("expected fail to write to storage with old lease version: %s\n", err) // client 1 should show this message
} else {
log.Printf("successfully write a key to storage using lease %x\n", int64(version))
}
Expand Down
8 changes: 3 additions & 5 deletions etcdctl/ctlv3/command/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,11 @@ func newCheckPerfCommand(cmd *cobra.Command, args []string) {
fmt.Printf("PASS: Stddev is %fs\n", s.Stddev)
}

if ok {
fmt.Println("PASS")
} else {
if !ok {

Check warning on line 260 in etcdctl/ctlv3/command/check.go

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/check.go#L260

Added line #L260 was not covered by tests
fmt.Println("FAIL")
os.Exit(cobrautl.ExitError)
}
fmt.Println("PASS")

Check warning on line 264 in etcdctl/ctlv3/command/check.go

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/check.go#L264

Added line #L264 was not covered by tests
}

func attemptCleanup(client *v3.Client, autoCompact bool) {
Expand Down Expand Up @@ -434,7 +433,6 @@ func newCheckDatascaleCommand(cmd *cobra.Command, args []string) {
fmt.Printf("FAIL: ERROR(%v) -> %d\n", k, v)
}
os.Exit(cobrautl.ExitError)
} else {
fmt.Printf("PASS: Approximate system memory used : %v MB.\n", strconv.FormatFloat(mbUsed, 'f', 2, 64))
}
fmt.Printf("PASS: Approximate system memory used : %v MB.\n", strconv.FormatFloat(mbUsed, 'f', 2, 64))

Check warning on line 437 in etcdctl/ctlv3/command/check.go

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/command/check.go#L437

Added line #L437 was not covered by tests
}
5 changes: 2 additions & 3 deletions etcdctl/ctlv3/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,10 @@ func Start() error {

func MustStart() {
if err := Start(); err != nil {
if rootCmd.SilenceErrors {
cobrautl.ExitWithError(cobrautl.ExitError, err)
} else {
if !rootCmd.SilenceErrors {

Check warning on line 117 in etcdctl/ctlv3/ctl.go

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/ctl.go#L117

Added line #L117 was not covered by tests
os.Exit(cobrautl.ExitError)
}
cobrautl.ExitWithError(cobrautl.ExitError, err)

Check warning on line 120 in etcdctl/ctlv3/ctl.go

View check run for this annotation

Codecov / codecov/patch

etcdctl/ctlv3/ctl.go#L120

Added line #L120 was not covered by tests
}
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/flags/selective_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ type SelectiveStringsValue struct {
func (ss *SelectiveStringsValue) Set(s string) error {
vs := strings.Split(s, ",")
for i := range vs {
if _, ok := ss.valids[vs[i]]; ok {
ss.vs = append(ss.vs, vs[i])
} else {
if _, ok := ss.valids[vs[i]]; !ok {
return fmt.Errorf("invalid value %q", vs[i])
}
ss.vs = append(ss.vs, vs[i])
}
sort.Strings(ss.vs)
return nil
Expand Down
19 changes: 9 additions & 10 deletions server/etcdserver/api/v2store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,18 +603,17 @@ func (s *store) internalCreate(nodePath string, dir bool, value string, unique,

// force will try to replace an existing file
if n != nil {
if replace {
if n.IsDir() {
return nil, v2error.NewError(v2error.EcodeNotFile, nodePath, currIndex)
}
e.PrevNode = n.Repr(false, false, s.clock)

if err := n.Remove(false, false, nil); err != nil {
return nil, err
}
} else {
if !replace {
return nil, v2error.NewError(v2error.EcodeNodeExist, nodePath, currIndex)
}
if n.IsDir() {
return nil, v2error.NewError(v2error.EcodeNotFile, nodePath, currIndex)

Check warning on line 610 in server/etcdserver/api/v2store/store.go

View check run for this annotation

Codecov / codecov/patch

server/etcdserver/api/v2store/store.go#L610

Added line #L610 was not covered by tests
}
e.PrevNode = n.Repr(false, false, s.clock)

if err := n.Remove(false, false, nil); err != nil {
return nil, err

Check warning on line 615 in server/etcdserver/api/v2store/store.go

View check run for this annotation

Codecov / codecov/patch

server/etcdserver/api/v2store/store.go#L615

Added line #L615 was not covered by tests
}
}

if !dir { // create file
Expand Down
5 changes: 2 additions & 3 deletions server/etcdserver/cluster_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,12 @@ func getDowngradeEnabledFromRemotePeers(lg *zap.Logger, cl *membership.RaftClust
continue
}
enable, err := getDowngradeEnabled(lg, m, rt, timeout)
if err != nil {
lg.Warn("failed to get downgrade enabled status", zap.String("remote-member-id", m.ID.String()), zap.Error(err))
} else {
if err == nil {
// Since the "/downgrade/enabled" serves linearized data,
// this function can return once it gets a non-error response from the endpoint.
return enable
}
lg.Warn("failed to get downgrade enabled status", zap.String("remote-member-id", m.ID.String()), zap.Error(err))
}
return false
}
Expand Down
9 changes: 4 additions & 5 deletions server/proxy/grpcproxy/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ func (w *watcher) send(wr clientv3.WatchResponse) {
ev := (*mvccpb.Event)(wr.Events[i])
if ev.Kv.ModRevision < w.nextrev {
continue
} else {
// We cannot update w.rev here.
// txn can have multiple events with the same rev.
// If w.nextrev updates here, it would skip events in the same txn.
lastRev = ev.Kv.ModRevision
}
// We cannot update w.rev here.
// txn can have multiple events with the same rev.
// If w.nextrev updates here, it would skip events in the same txn.
lastRev = ev.Kv.ModRevision

filtered := false
for _, filter := range w.filters {
Expand Down
5 changes: 2 additions & 3 deletions server/storage/mvcc/watchable_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,14 @@ func (s *watchableStore) moveVictims() (moved int) {
for w, eb := range wb {
// watcher has observed the store up to, but not including, w.minRev
rev := w.minRev - 1
if w.send(WatchResponse{WatchID: w.id, Events: eb.evs, Revision: rev}) {
pendingEventsGauge.Add(float64(len(eb.evs)))
} else {
if !w.send(WatchResponse{WatchID: w.id, Events: eb.evs, Revision: rev}) {
if newVictim == nil {
newVictim = make(watcherBatch)
}
newVictim[w] = eb
continue
}
pendingEventsGauge.Add(float64(len(eb.evs)))
moved++
}

Expand Down
3 changes: 1 addition & 2 deletions tests/framework/e2e/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1062,9 +1062,8 @@ func (epc *EtcdProcessCluster) WaitMembersForLeader(ctx context.Context, t testi
if strings.Contains(err.Error(), "connection refused") {
// if member[i] has stopped
continue
} else {
t.Fatal(err)
}
t.Fatal(err)
}
members[resp[0].Header.MemberId] = i
leaders[resp[0].Leader] = struct{}{}
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/clientv3/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ func getHTTPBodyAsLines(t *testing.T, url string) []string {
if err != nil {
if errors.Is(err, io.EOF) {
break
} else {
t.Fatalf("error reading: %v", err)
}
t.Fatalf("error reading: %v", err)
}
lines = append(lines, line)
}
Expand Down
10 changes: 4 additions & 6 deletions tests/integration/v3_alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,10 @@ func TestV3CorruptAlarm(t *testing.T) {
for i := 0; i < 5; i++ {
presp, perr := clus.Client(0).Put(context.TODO(), "abc", "aaa")
if perr != nil {
if !eqErrGRPC(perr, rpctypes.ErrCorrupt) {
t.Fatalf("expected %v, got %+v (%v)", rpctypes.ErrCorrupt, presp, perr)
} else {
if eqErrGRPC(perr, rpctypes.ErrCorrupt) {
return
}
t.Fatalf("expected %v, got %+v (%v)", rpctypes.ErrCorrupt, presp, perr)
}
time.Sleep(time.Second)
}
Expand Down Expand Up @@ -347,10 +346,9 @@ func TestV3CorruptAlarmWithLeaseCorrupted(t *testing.T) {
time.Sleep(time.Second)
presp, perr := clus.Client(0).Put(context.TODO(), "abc", "aaa")
if perr != nil {
if !eqErrGRPC(perr, rpctypes.ErrCorrupt) {
t.Fatalf("expected %v, got %+v (%v)", rpctypes.ErrCorrupt, presp, perr)
} else {
if eqErrGRPC(perr, rpctypes.ErrCorrupt) {
return
}
t.Fatalf("expected %v, got %+v (%v)", rpctypes.ErrCorrupt, presp, perr)
}
}
6 changes: 2 additions & 4 deletions tests/integration/v3_failover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ func putWithRetries(t *testing.T, cli *clientv3.Client, key, val string, retryCo
retryCount--
if shouldRetry(err) {
continue
} else {
t.Fatal(err)
}
t.Fatal(err)
}
break
}
Expand Down Expand Up @@ -156,9 +155,8 @@ func getWithRetries(t *testing.T, cli *clientv3.Client, key, val string, retryCo
retryCount--
if shouldRetry(err) {
continue
} else {
t.Fatal(err)
}
t.Fatal(err)
}
break
}
Expand Down
9 changes: 4 additions & 5 deletions tests/robustness/validate/patch_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,11 @@ func patchOperations(operations []porcupine.Operation, watchRevision, putReturnT
if !persisted {
// Remove non persisted operations
continue
}
if txnRevision != 0 {
op.Output = model.MaybeEtcdResponse{Persisted: true, PersistedRevision: txnRevision}
} else {
if txnRevision != 0 {
op.Output = model.MaybeEtcdResponse{Persisted: true, PersistedRevision: txnRevision}
} else {
op.Output = model.MaybeEtcdResponse{Persisted: true}
}
op.Output = model.MaybeEtcdResponse{Persisted: true}
}
}
// Leave operation as it is as we cannot discard it.
Expand Down
8 changes: 8 additions & 0 deletions tools/.golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ linters-settings: # please keep this alphabetized
disabled: false
- name: dot-imports
disabled: false
- name: early-return
disabled: false
arguments:
- "preserveScope"
- name: error-return
disabled: false
- name: error-naming
Expand All @@ -74,6 +78,10 @@ linters-settings: # please keep this alphabetized
disabled: false
- name: receiver-naming
disabled: false
- name: superfluous-else
disabled: false
arguments:
- "preserveScope"
- name: time-naming
disabled: false
- name: use-any
Expand Down
5 changes: 2 additions & 3 deletions tools/etcd-dump-logs/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,9 @@ func readRaw(fromIndex *uint64, waldir string, out io.Writer) {
} else if errors.Is(err, io.ErrUnexpectedEOF) {
fmt.Fprintf(out, "ErrUnexpectedEOF: The last record might be corrupted, error: %v.\n", err)
break
} else {
log.Printf("Error: Reading failed: %v", err)
break
}
log.Printf("Error: Reading failed: %v", err)
break
}
}

Expand Down

0 comments on commit 8f37a26

Please sign in to comment.