Skip to content

Commit 778ff94

Browse files
quasilytefjlholiman
authored
all: fix some go-critic linter warnings (#23709)
This doesn't fix all go-critic warnings, just the most serious ones. Co-authored-by: Felix Lange <[email protected]> Co-authored-by: Martin Holst Swende <[email protected]>
1 parent e4f570f commit 778ff94

File tree

8 files changed

+35
-38
lines changed

8 files changed

+35
-38
lines changed

accounts/abi/reflect.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,8 @@ func set(dst, src reflect.Value) error {
123123
func setSlice(dst, src reflect.Value) error {
124124
slice := reflect.MakeSlice(dst.Type(), src.Len(), src.Len())
125125
for i := 0; i < src.Len(); i++ {
126-
if src.Index(i).Kind() == reflect.Struct {
127-
if err := set(slice.Index(i), src.Index(i)); err != nil {
128-
return err
129-
}
130-
} else {
131-
// e.g. [][32]uint8 to []common.Hash
132-
if err := set(slice.Index(i), src.Index(i)); err != nil {
133-
return err
134-
}
126+
if err := set(slice.Index(i), src.Index(i)); err != nil {
127+
return err
135128
}
136129
}
137130
if dst.CanSet() {

cmd/evm/internal/t8ntool/transition.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,15 +419,15 @@ func dispatchOutput(ctx *cli.Context, baseDir string, result *ExecutionResult, a
419419
return NewError(ErrorJson, fmt.Errorf("failed marshalling output: %v", err))
420420
}
421421
os.Stdout.Write(b)
422-
os.Stdout.Write([]byte("\n"))
422+
os.Stdout.WriteString("\n")
423423
}
424424
if len(stdErrObject) > 0 {
425425
b, err := json.MarshalIndent(stdErrObject, "", " ")
426426
if err != nil {
427427
return NewError(ErrorJson, fmt.Errorf("failed marshalling output: %v", err))
428428
}
429429
os.Stderr.Write(b)
430-
os.Stderr.Write([]byte("\n"))
430+
os.Stderr.WriteString("\n")
431431
}
432432
return nil
433433
}

cmd/faucet/faucet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ func authTwitter(url string, tokenV1, tokenV2 string) (string, string, string, c
741741
return "", "", "", common.Address{}, errors.New("No Ethereum address found to fund")
742742
}
743743
var avatar string
744-
if parts = regexp.MustCompile("src=\"([^\"]+twimg.com/profile_images[^\"]+)\"").FindStringSubmatch(string(body)); len(parts) == 2 {
744+
if parts = regexp.MustCompile(`src="([^"]+twimg\.com/profile_images[^"]+)"`).FindStringSubmatch(string(body)); len(parts) == 2 {
745745
avatar = parts[1]
746746
}
747747
return username + "@twitter", username, avatar, address, nil
@@ -867,7 +867,7 @@ func authFacebook(url string) (string, string, common.Address, error) {
867867
return "", "", common.Address{}, errors.New("No Ethereum address found to fund")
868868
}
869869
var avatar string
870-
if parts = regexp.MustCompile("src=\"([^\"]+fbcdn.net[^\"]+)\"").FindStringSubmatch(string(body)); len(parts) == 2 {
870+
if parts = regexp.MustCompile(`src="([^"]+fbcdn\.net[^"]+)"`).FindStringSubmatch(string(body)); len(parts) == 2 {
871871
avatar = parts[1]
872872
}
873873
return username + "@facebook", avatar, address, nil

core/bloombits/generator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func BenchmarkGenerator(b *testing.B) {
7070
if err != nil {
7171
b.Fatalf("failed to create bloombit generator: %v", err)
7272
}
73-
for j, bloom := range input {
73+
for j, bloom := range &input {
7474
if err := gen.AddBloom(uint(j), bloom); err != nil {
7575
b.Fatalf("bloom %d: failed to add: %v", i, err)
7676
}
@@ -89,7 +89,7 @@ func BenchmarkGenerator(b *testing.B) {
8989
if err != nil {
9090
b.Fatalf("failed to create bloombit generator: %v", err)
9191
}
92-
for j, bloom := range input {
92+
for j, bloom := range &input {
9393
if err := gen.AddBloom(uint(j), bloom); err != nil {
9494
b.Fatalf("bloom %d: failed to add: %v", i, err)
9595
}

eth/protocols/eth/handler_test.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ func testGetNodeData(t *testing.T, protocol uint) {
385385
acc2Addr := crypto.PubkeyToAddress(acc2Key.PublicKey)
386386

387387
signer := types.HomesteadSigner{}
388-
// Create a chain generator with some simple transactions (blatantly stolen from @fjl/chain_markets_test)
388+
// Create a chain generator with some simple transactions (blatantly stolen from @fjl/chain_makers_test)
389389
generator := func(i int, block *core.BlockGen) {
390390
switch i {
391391
case 0:
@@ -420,9 +420,8 @@ func testGetNodeData(t *testing.T, protocol uint) {
420420
peer, _ := newTestPeer("peer", protocol, backend)
421421
defer peer.close()
422422

423-
// Fetch for now the entire chain db
423+
// Collect all state tree hashes.
424424
var hashes []common.Hash
425-
426425
it := backend.db.NewIterator(nil, nil)
427426
for it.Next() {
428427
if key := it.Key(); len(key) == common.HashLength {
@@ -431,6 +430,7 @@ func testGetNodeData(t *testing.T, protocol uint) {
431430
}
432431
it.Release()
433432

433+
// Request all hashes.
434434
p2p.Send(peer.app, GetNodeDataMsg, GetNodeDataPacket66{
435435
RequestId: 123,
436436
GetNodeDataPacket: hashes,
@@ -442,38 +442,40 @@ func testGetNodeData(t *testing.T, protocol uint) {
442442
if msg.Code != NodeDataMsg {
443443
t.Fatalf("response packet code mismatch: have %x, want %x", msg.Code, NodeDataMsg)
444444
}
445-
var (
446-
data [][]byte
447-
res NodeDataPacket66
448-
)
445+
var res NodeDataPacket66
449446
if err := msg.Decode(&res); err != nil {
450447
t.Fatalf("failed to decode response node data: %v", err)
451448
}
452-
data = res.NodeDataPacket
453-
// Verify that all hashes correspond to the requested data, and reconstruct a state tree
449+
450+
// Verify that all hashes correspond to the requested data.
451+
data := res.NodeDataPacket
454452
for i, want := range hashes {
455453
if hash := crypto.Keccak256Hash(data[i]); hash != want {
456454
t.Errorf("data hash mismatch: have %x, want %x", hash, want)
457455
}
458456
}
459-
statedb := rawdb.NewMemoryDatabase()
457+
458+
// Reconstruct state tree from the received data.
459+
reconstructDB := rawdb.NewMemoryDatabase()
460460
for i := 0; i < len(data); i++ {
461-
statedb.Put(hashes[i].Bytes(), data[i])
461+
rawdb.WriteTrieNode(reconstructDB, hashes[i], data[i])
462462
}
463+
464+
// Sanity check whether all state matches.
463465
accounts := []common.Address{testAddr, acc1Addr, acc2Addr}
464466
for i := uint64(0); i <= backend.chain.CurrentBlock().NumberU64(); i++ {
465-
trie, _ := state.New(backend.chain.GetBlockByNumber(i).Root(), state.NewDatabase(statedb), nil)
466-
467+
root := backend.chain.GetBlockByNumber(i).Root()
468+
reconstructed, _ := state.New(root, state.NewDatabase(reconstructDB), nil)
467469
for j, acc := range accounts {
468-
state, _ := backend.chain.State()
470+
state, _ := backend.chain.StateAt(root)
469471
bw := state.GetBalance(acc)
470-
bh := trie.GetBalance(acc)
472+
bh := reconstructed.GetBalance(acc)
471473

472-
if (bw != nil && bh == nil) || (bw == nil && bh != nil) {
473-
t.Errorf("test %d, account %d: balance mismatch: have %v, want %v", i, j, bh, bw)
474+
if (bw == nil) != (bh == nil) {
475+
t.Errorf("block %d, account %d: balance mismatch: have %v, want %v", i, j, bh, bw)
474476
}
475-
if bw != nil && bh != nil && bw.Cmp(bw) != 0 {
476-
t.Errorf("test %d, account %d: balance mismatch: have %v, want %v", i, j, bh, bw)
477+
if bw != nil && bh != nil && bw.Cmp(bh) != 0 {
478+
t.Errorf("block %d, account %d: balance mismatch: have %v, want %v", i, j, bh, bw)
477479
}
478480
}
479481
}

node/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (api *privateAdminAPI) StartHTTP(host *string, port *int, cors *string, api
218218
}
219219

220220
// StartRPC starts the HTTP RPC API server.
221-
// This method is deprecated. Use StartHTTP instead.
221+
// Deprecated: use StartHTTP instead.
222222
func (api *privateAdminAPI) StartRPC(host *string, port *int, cors *string, apis *string, vhosts *string) (bool, error) {
223223
log.Warn("Deprecation warning", "method", "admin.StartRPC", "use-instead", "admin.StartHTTP")
224224
return api.StartHTTP(host, port, cors, apis, vhosts)
@@ -231,7 +231,7 @@ func (api *privateAdminAPI) StopHTTP() (bool, error) {
231231
}
232232

233233
// StopRPC shuts down the HTTP server.
234-
// This method is deprecated. Use StopHTTP instead.
234+
// Deprecated: use StopHTTP instead.
235235
func (api *privateAdminAPI) StopRPC() (bool, error) {
236236
log.Warn("Deprecation warning", "method", "admin.StopRPC", "use-instead", "admin.StopHTTP")
237237
return api.StopHTTP()

signer/core/signed_data.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"strconv"
3030
"strings"
3131
"unicode"
32+
"unicode/utf8"
3233

3334
"github.com/ethereum/go-ethereum/accounts"
3435
"github.com/ethereum/go-ethereum/common"
@@ -100,7 +101,8 @@ func (t *Type) isReferenceType() bool {
100101
return false
101102
}
102103
// Reference types must have a leading uppercase character
103-
return unicode.IsUpper([]rune(t.Type)[0])
104+
r, _ := utf8.DecodeRuneInString(t.Type)
105+
return unicode.IsUpper(r)
104106
}
105107

106108
type Types map[string][]Type

signer/core/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"regexp"
2222
)
2323

24-
var printable7BitAscii = regexp.MustCompile("^[A-Za-z0-9!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ ]+$")
24+
var printable7BitAscii = regexp.MustCompile("^[A-Za-z0-9!\"#$%&'()*+,\\-./:;<=>?@[\\]^_`{|}~ ]+$")
2525

2626
// ValidatePasswordFormat returns an error if the password is too short, or consists of characters
2727
// outside the range of the printable 7bit ascii set

0 commit comments

Comments
 (0)