Skip to content

Commit 10d7cea

Browse files
mmorel-35ivanvc
andcommitted
chore: enable early-return and superfluous-else from revive
Signed-off-by: Matthieu MOREL <[email protected]> Co-authored-by: Iván Valdés Castillo <[email protected]>
1 parent ecd7cfc commit 10d7cea

File tree

15 files changed

+47
-56
lines changed

15 files changed

+47
-56
lines changed

contrib/lock/client/client.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,10 @@ func main() {
135135

136136
err = write("key0", fmt.Sprintf("value from client %x", mode), int64(version))
137137
if err != nil {
138-
if mode == 1 {
139-
log.Printf("expected fail to write to storage with old lease version: %s\n", err) // client 1 should show this message
140-
} else {
138+
if mode != 1 {
141139
log.Fatalf("unexpected fail to write to storage: %s\n", err)
142140
}
141+
log.Printf("expected fail to write to storage with old lease version: %s\n", err) // client 1 should show this message
143142
} else {
144143
log.Printf("successfully write a key to storage using lease %x\n", int64(version))
145144
}

etcdctl/ctlv3/command/check.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,11 @@ func newCheckPerfCommand(cmd *cobra.Command, args []string) {
257257
fmt.Printf("PASS: Stddev is %fs\n", s.Stddev)
258258
}
259259

260-
if ok {
261-
fmt.Println("PASS")
262-
} else {
260+
if !ok {
263261
fmt.Println("FAIL")
264262
os.Exit(cobrautl.ExitError)
265263
}
264+
fmt.Println("PASS")
266265
}
267266

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

etcdctl/ctlv3/ctl.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,8 @@ func MustStart() {
116116
if err := Start(); err != nil {
117117
if rootCmd.SilenceErrors {
118118
cobrautl.ExitWithError(cobrautl.ExitError, err)
119-
} else {
120-
os.Exit(cobrautl.ExitError)
121119
}
120+
os.Exit(cobrautl.ExitError)
122121
}
123122
}
124123

pkg/flags/selective_string.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,10 @@ type SelectiveStringsValue struct {
7777
func (ss *SelectiveStringsValue) Set(s string) error {
7878
vs := strings.Split(s, ",")
7979
for i := range vs {
80-
if _, ok := ss.valids[vs[i]]; ok {
81-
ss.vs = append(ss.vs, vs[i])
82-
} else {
80+
if _, ok := ss.valids[vs[i]]; !ok {
8381
return fmt.Errorf("invalid value %q", vs[i])
8482
}
83+
ss.vs = append(ss.vs, vs[i])
8584
}
8685
sort.Strings(ss.vs)
8786
return nil

server/etcdserver/api/v2store/store.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -603,18 +603,17 @@ func (s *store) internalCreate(nodePath string, dir bool, value string, unique,
603603

604604
// force will try to replace an existing file
605605
if n != nil {
606-
if replace {
607-
if n.IsDir() {
608-
return nil, v2error.NewError(v2error.EcodeNotFile, nodePath, currIndex)
609-
}
610-
e.PrevNode = n.Repr(false, false, s.clock)
611-
612-
if err := n.Remove(false, false, nil); err != nil {
613-
return nil, err
614-
}
615-
} else {
606+
if !replace {
616607
return nil, v2error.NewError(v2error.EcodeNodeExist, nodePath, currIndex)
617608
}
609+
if n.IsDir() {
610+
return nil, v2error.NewError(v2error.EcodeNotFile, nodePath, currIndex)
611+
}
612+
e.PrevNode = n.Repr(false, false, s.clock)
613+
614+
if err := n.Remove(false, false, nil); err != nil {
615+
return nil, err
616+
}
618617
}
619618

620619
if !dir { // create file

server/etcdserver/cluster_util.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,12 @@ func getDowngradeEnabledFromRemotePeers(lg *zap.Logger, cl *membership.RaftClust
353353
continue
354354
}
355355
enable, err := getDowngradeEnabled(lg, m, rt, timeout)
356-
if err != nil {
357-
lg.Warn("failed to get downgrade enabled status", zap.String("remote-member-id", m.ID.String()), zap.Error(err))
358-
} else {
356+
if err == nil {
359357
// Since the "/downgrade/enabled" serves linearized data,
360358
// this function can return once it gets a non-error response from the endpoint.
361359
return enable
362360
}
361+
lg.Warn("failed to get downgrade enabled status", zap.String("remote-member-id", m.ID.String()), zap.Error(err))
363362
}
364363
return false
365364
}

server/proxy/grpcproxy/watcher.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,11 @@ func (w *watcher) send(wr clientv3.WatchResponse) {
7171
ev := (*mvccpb.Event)(wr.Events[i])
7272
if ev.Kv.ModRevision < w.nextrev {
7373
continue
74-
} else {
75-
// We cannot update w.rev here.
76-
// txn can have multiple events with the same rev.
77-
// If w.nextrev updates here, it would skip events in the same txn.
78-
lastRev = ev.Kv.ModRevision
7974
}
75+
// We cannot update w.rev here.
76+
// txn can have multiple events with the same rev.
77+
// If w.nextrev updates here, it would skip events in the same txn.
78+
lastRev = ev.Kv.ModRevision
8079

8180
filtered := false
8281
for _, filter := range w.filters {

server/storage/mvcc/watchable_store.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,14 @@ func (s *watchableStore) moveVictims() (moved int) {
290290
for w, eb := range wb {
291291
// watcher has observed the store up to, but not including, w.minRev
292292
rev := w.minRev - 1
293-
if w.send(WatchResponse{WatchID: w.id, Events: eb.evs, Revision: rev}) {
294-
pendingEventsGauge.Add(float64(len(eb.evs)))
295-
} else {
293+
if !w.send(WatchResponse{WatchID: w.id, Events: eb.evs, Revision: rev}) {
296294
if newVictim == nil {
297295
newVictim = make(watcherBatch)
298296
}
299297
newVictim[w] = eb
300298
continue
301299
}
300+
pendingEventsGauge.Add(float64(len(eb.evs)))
302301
moved++
303302
}
304303

tests/framework/e2e/cluster.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1062,9 +1062,8 @@ func (epc *EtcdProcessCluster) WaitMembersForLeader(ctx context.Context, t testi
10621062
if strings.Contains(err.Error(), "connection refused") {
10631063
// if member[i] has stopped
10641064
continue
1065-
} else {
1066-
t.Fatal(err)
10671065
}
1066+
t.Fatal(err)
10681067
}
10691068
members[resp[0].Header.MemberId] = i
10701069
leaders[resp[0].Leader] = struct{}{}

tests/integration/clientv3/metrics_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,8 @@ func getHTTPBodyAsLines(t *testing.T, url string) []string {
168168
if err != nil {
169169
if errors.Is(err, io.EOF) {
170170
break
171-
} else {
172-
t.Fatalf("error reading: %v", err)
173171
}
172+
t.Fatalf("error reading: %v", err)
174173
}
175174
lines = append(lines, line)
176175
}

tests/integration/v3_alarm_test.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,10 @@ func TestV3CorruptAlarm(t *testing.T) {
247247
for i := 0; i < 5; i++ {
248248
presp, perr := clus.Client(0).Put(context.TODO(), "abc", "aaa")
249249
if perr != nil {
250-
if !eqErrGRPC(perr, rpctypes.ErrCorrupt) {
251-
t.Fatalf("expected %v, got %+v (%v)", rpctypes.ErrCorrupt, presp, perr)
252-
} else {
250+
if eqErrGRPC(perr, rpctypes.ErrCorrupt) {
253251
return
254252
}
253+
t.Fatalf("expected %v, got %+v (%v)", rpctypes.ErrCorrupt, presp, perr)
255254
}
256255
time.Sleep(time.Second)
257256
}
@@ -347,10 +346,9 @@ func TestV3CorruptAlarmWithLeaseCorrupted(t *testing.T) {
347346
time.Sleep(time.Second)
348347
presp, perr := clus.Client(0).Put(context.TODO(), "abc", "aaa")
349348
if perr != nil {
350-
if !eqErrGRPC(perr, rpctypes.ErrCorrupt) {
351-
t.Fatalf("expected %v, got %+v (%v)", rpctypes.ErrCorrupt, presp, perr)
352-
} else {
349+
if eqErrGRPC(perr, rpctypes.ErrCorrupt) {
353350
return
354351
}
352+
t.Fatalf("expected %v, got %+v (%v)", rpctypes.ErrCorrupt, presp, perr)
355353
}
356354
}

tests/integration/v3_failover_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,8 @@ func putWithRetries(t *testing.T, cli *clientv3.Client, key, val string, retryCo
124124
retryCount--
125125
if shouldRetry(err) {
126126
continue
127-
} else {
128-
t.Fatal(err)
129127
}
128+
t.Fatal(err)
130129
}
131130
break
132131
}
@@ -156,9 +155,8 @@ func getWithRetries(t *testing.T, cli *clientv3.Client, key, val string, retryCo
156155
retryCount--
157156
if shouldRetry(err) {
158157
continue
159-
} else {
160-
t.Fatal(err)
161158
}
159+
t.Fatal(err)
162160
}
163161
break
164162
}

tests/robustness/validate/patch_history.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,11 @@ func patchOperations(operations []porcupine.Operation, watchRevision, putReturnT
108108
if !persisted {
109109
// Remove non persisted operations
110110
continue
111+
}
112+
if txnRevision != 0 {
113+
op.Output = model.MaybeEtcdResponse{Persisted: true, PersistedRevision: txnRevision}
111114
} else {
112-
if txnRevision != 0 {
113-
op.Output = model.MaybeEtcdResponse{Persisted: true, PersistedRevision: txnRevision}
114-
} else {
115-
op.Output = model.MaybeEtcdResponse{Persisted: true}
116-
}
115+
op.Output = model.MaybeEtcdResponse{Persisted: true}
117116
}
118117
}
119118
// Leave operation as it is as we cannot discard it.

tools/.golangci.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ linters-settings: # please keep this alphabetized
5454
disabled: false
5555
- name: dot-imports
5656
disabled: false
57+
- name: early-return
58+
disabled: false
59+
arguments:
60+
- "preserveScope"
5761
- name: error-return
5862
disabled: false
5963
- name: error-naming
@@ -74,6 +78,10 @@ linters-settings: # please keep this alphabetized
7478
disabled: false
7579
- name: receiver-naming
7680
disabled: false
81+
- name: superfluous-else
82+
disabled: false
83+
arguments:
84+
- "preserveScope"
7785
- name: time-naming
7886
disabled: false
7987
- name: use-any

tools/etcd-dump-logs/raw.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ func readRaw(fromIndex *uint64, waldir string, out io.Writer) {
7575
} else if errors.Is(err, io.ErrUnexpectedEOF) {
7676
fmt.Fprintf(out, "ErrUnexpectedEOF: The last record might be corrupted, error: %v.\n", err)
7777
break
78-
} else {
79-
log.Printf("Error: Reading failed: %v", err)
80-
break
8178
}
79+
log.Printf("Error: Reading failed: %v", err)
80+
break
8281
}
8382
}
8483

0 commit comments

Comments
 (0)