Skip to content

Commit c92faee

Browse files
authored
all: simplify nested complexity and if blocks ending with a return statement (#21854)
Changes: Simplify nested complexity If an if blocks ends with a return statement then remove the else nesting. Most of the changes has also been reported in golint https://goreportcard.com/report/github.com/ethereum/go-ethereum#golint
1 parent 29efe1f commit c92faee

19 files changed

Lines changed: 56 additions & 83 deletions

File tree

‎cmd/clef/main.go‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -747,12 +747,10 @@ func DefaultConfigDir() string {
747747
appdata := os.Getenv("APPDATA")
748748
if appdata != "" {
749749
return filepath.Join(appdata, "Signer")
750-
} else {
751-
return filepath.Join(home, "AppData", "Roaming", "Signer")
752750
}
753-
} else {
754-
return filepath.Join(home, ".clef")
751+
return filepath.Join(home, "AppData", "Roaming", "Signer")
755752
}
753+
return filepath.Join(home, ".clef")
756754
}
757755
// As we cannot guess a stable location, return empty and handle later
758756
return ""

‎cmd/utils/flags.go‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,19 +1695,18 @@ func RegisterEthService(stack *node.Node, cfg *eth.Config) ethapi.Backend {
16951695
Fatalf("Failed to register the Ethereum service: %v", err)
16961696
}
16971697
return backend.ApiBackend
1698-
} else {
1699-
backend, err := eth.New(stack, cfg)
1698+
}
1699+
backend, err := eth.New(stack, cfg)
1700+
if err != nil {
1701+
Fatalf("Failed to register the Ethereum service: %v", err)
1702+
}
1703+
if cfg.LightServ > 0 {
1704+
_, err := les.NewLesServer(stack, backend, cfg)
17001705
if err != nil {
1701-
Fatalf("Failed to register the Ethereum service: %v", err)
1702-
}
1703-
if cfg.LightServ > 0 {
1704-
_, err := les.NewLesServer(stack, backend, cfg)
1705-
if err != nil {
1706-
Fatalf("Failed to create the LES server: %v", err)
1707-
}
1706+
Fatalf("Failed to create the LES server: %v", err)
17081707
}
1709-
return backend.APIBackend
17101708
}
1709+
return backend.APIBackend
17111710
}
17121711

17131712
// RegisterEthStatsService configures the Ethereum Stats daemon and adds it to

‎console/bridge.go‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,14 @@ func (b *bridge) OpenWallet(call jsre.Call) (goja.Value, error) {
144144
if val, err = openWallet(goja.Null(), wallet, passwd); err != nil {
145145
if !strings.HasSuffix(err.Error(), scwallet.ErrPINNeeded.Error()) {
146146
return nil, err
147-
} else {
148-
// PIN input requested, fetch from the user and call open again
149-
input, err := b.prompter.PromptPassword("Please enter current PIN: ")
150-
if err != nil {
151-
return nil, err
152-
}
153-
if val, err = openWallet(goja.Null(), wallet, call.VM.ToValue(input)); err != nil {
154-
return nil, err
155-
}
147+
}
148+
// PIN input requested, fetch from the user and call open again
149+
input, err := b.prompter.PromptPassword("Please enter current PIN: ")
150+
if err != nil {
151+
return nil, err
152+
}
153+
if val, err = openWallet(goja.Null(), wallet, call.VM.ToValue(input)); err != nil {
154+
return nil, err
156155
}
157156
}
158157

‎core/headerchain.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,8 @@ func (hc *HeaderChain) GetAncestor(hash common.Hash, number, ancestor uint64, ma
369369
// in this case it is cheaper to just read the header
370370
if header := hc.GetHeader(hash, number); header != nil {
371371
return header.ParentHash, number - 1
372-
} else {
373-
return common.Hash{}, 0
374372
}
373+
return common.Hash{}, 0
375374
}
376375
for ancestor != 0 {
377376
if rawdb.ReadCanonicalHash(hc.chainDb, number) == hash {

‎crypto/bls12381/g1.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,8 @@ func (g *G1) Add(r, p1, p2 *PointG1) *PointG1 {
266266
if t[1].equal(t[3]) {
267267
if t[0].equal(t[2]) {
268268
return g.Double(r, p1)
269-
} else {
270-
return r.Zero()
271269
}
270+
return r.Zero()
272271
}
273272
sub(t[1], t[1], t[3])
274273
double(t[4], t[1])

‎crypto/bls12381/g2.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,8 @@ func (g *G2) Add(r, p1, p2 *PointG2) *PointG2 {
287287
if t[1].equal(t[3]) {
288288
if t[0].equal(t[2]) {
289289
return g.Double(r, p1)
290-
} else {
291-
return r.Zero()
292290
}
291+
return r.Zero()
293292
}
294293
g.f.sub(t[1], t[1], t[3])
295294
g.f.double(t[4], t[1])

‎les/benchmark.go‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ func (b *benchmarkBlockHeaders) init(h *serverHandler, count int) error {
7575
func (b *benchmarkBlockHeaders) request(peer *serverPeer, index int) error {
7676
if b.byHash {
7777
return peer.requestHeadersByHash(0, b.hashes[index], b.amount, b.skip, b.reverse)
78-
} else {
79-
return peer.requestHeadersByNumber(0, uint64(b.offset+rand.Int63n(b.randMax)), b.amount, b.skip, b.reverse)
8078
}
79+
return peer.requestHeadersByNumber(0, uint64(b.offset+rand.Int63n(b.randMax)), b.amount, b.skip, b.reverse)
8180
}
8281

8382
// benchmarkBodiesOrReceipts implements requestBenchmark
@@ -98,9 +97,8 @@ func (b *benchmarkBodiesOrReceipts) init(h *serverHandler, count int) error {
9897
func (b *benchmarkBodiesOrReceipts) request(peer *serverPeer, index int) error {
9998
if b.receipts {
10099
return peer.requestReceipts(0, []common.Hash{b.hashes[index]})
101-
} else {
102-
return peer.requestBodies(0, []common.Hash{b.hashes[index]})
103100
}
101+
return peer.requestBodies(0, []common.Hash{b.hashes[index]})
104102
}
105103

106104
// benchmarkProofsOrCode implements requestBenchmark
@@ -119,9 +117,8 @@ func (b *benchmarkProofsOrCode) request(peer *serverPeer, index int) error {
119117
rand.Read(key)
120118
if b.code {
121119
return peer.requestCode(0, []CodeReq{{BHash: b.headHash, AccKey: key}})
122-
} else {
123-
return peer.requestProofs(0, []ProofReq{{BHash: b.headHash, Key: key}})
124120
}
121+
return peer.requestProofs(0, []ProofReq{{BHash: b.headHash, Key: key}})
125122
}
126123

127124
// benchmarkHelperTrie implements requestBenchmark

‎les/lespay/server/balance.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,8 @@ func (n *NodeBalance) timeUntil(priority int64) (time.Duration, bool) {
588588
}
589589
dt = float64(posBalance-newBalance) / timePrice
590590
return time.Duration(dt), true
591-
} else {
592-
dt = float64(posBalance) / timePrice
593591
}
592+
dt = float64(posBalance) / timePrice
594593
} else {
595594
if priority > 0 {
596595
return 0, false

‎les/lespay/server/balance_tracker.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,8 @@ func (bt *BalanceTracker) storeBalance(id []byte, neg bool, value utils.ExpiredV
261261
func (bt *BalanceTracker) canDropBalance(now mclock.AbsTime, neg bool, b utils.ExpiredValue) bool {
262262
if neg {
263263
return b.Value(bt.negExp.LogOffset(now)) <= negThreshold
264-
} else {
265-
return b.Value(bt.posExp.LogOffset(now)) <= posThreshold
266264
}
265+
return b.Value(bt.posExp.LogOffset(now)) <= posThreshold
267266
}
268267

269268
// updateTotalBalance adjusts the total balance after executing given callback.

‎les/lespay/server/prioritypool.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,8 @@ func activePriority(a interface{}, now mclock.AbsTime) int64 {
288288
}
289289
if c.bias == 0 {
290290
return invertPriority(c.nodePriority.Priority(now, c.capacity))
291-
} else {
292-
return invertPriority(c.nodePriority.EstMinPriority(now+mclock.AbsTime(c.bias), c.capacity, true))
293291
}
292+
return invertPriority(c.nodePriority.EstMinPriority(now+mclock.AbsTime(c.bias), c.capacity, true))
294293
}
295294

296295
// activeMaxPriority callback returns estimated maximum priority of ppNodeInfo item in activeQueue

0 commit comments

Comments
 (0)