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

chore: enable early-return and superfluous-else from revive #19152

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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 @@
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 @@
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 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
6 changes: 3 additions & 3 deletions pkg/flags/selective_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ 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 {
_, ok := ss.valids[vs[i]]
if !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 @@

// 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 @@ -1054,9 +1054,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
47 changes: 19 additions & 28 deletions tools/.golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,58 +44,49 @@ linters-settings: # please keep this alphabetized
# Align with https://github.com/alexkohler/nakedret/blob/v1.0.2/cmd/nakedret/main.go#L10
max-func-lines: 5
revive:
ignore-generated-header: false
severity: error
confidence: 0.8
enable-all-rules: false
rules:
- name: blank-imports
severity: error
disabled: false
- name: context-as-argument
severity: error
disabled: false
- name: context-keys-type
disabled: false
- name: dot-imports
severity: error
disabled: false
- name: error-return
severity: error
- name: early-return
disabled: false
arguments:
- "preserveScope"
- name: error-naming
disabled: false
- name: error-naming
severity: error
disabled: false
- name: error-return
disabled: false
- name: error-strings
disabled: false
- name: errorf
disabled: false
- name: if-return
severity: error
disabled: false
- name: increment-decrement
severity: error
disabled: false
- name: var-declaration
severity: error
- name: indent-error-flow
disabled: false
- name: package-comments
severity: error
disabled: false
- name: range
severity: error
disabled: false
- name: receiver-naming
severity: error
disabled: false
- name: time-naming
severity: error
disabled: false
- name: indent-error-flow
severity: error
disabled: false
- name: errorf
severity: error
- name: superfluous-else
disabled: false
- name: context-keys-type
severity: error
arguments:
- "preserveScope"
- name: time-naming
disabled: false
- name: error-strings
severity: error
- name: var-declaration
disabled: false
- name: var-naming
disabled: false
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
Loading