Skip to content

Commit a907d7e

Browse files
authored
all: more linters (ethereum#24783)
This enables the following linters - typecheck - unused - staticcheck - bidichk - durationcheck - exportloopref - gosec WIth a few exceptions. - We use a deprecated protobuf in trezor. I didn't want to mess with that, since I cannot meaningfully test any changes there. - The deprecated TypeMux is used in a few places still, so the warning for it is silenced for now. - Using string type in context.WithValue is apparently wrong, one should use a custom type, to prevent collisions between different places in the hierarchy of callers. That should be fixed at some point, but may require some attention. - The warnings for using weak random generator are squashed, since we use a lot of random without need for cryptographic guarantees.
1 parent eb94896 commit a907d7e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+156
-423
lines changed

.golangci.yml

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,53 @@ linters:
1919
- govet
2020
- ineffassign
2121
- misspell
22-
# - staticcheck
2322
- unconvert
24-
# - unused
2523
- varcheck
24+
- typecheck
25+
- unused
26+
- staticcheck
27+
- bidichk
28+
- durationcheck
29+
- exportloopref
30+
- gosec
31+
32+
#- structcheck # lots of false positives
33+
#- errcheck #lot of false positives
34+
# - contextcheck
35+
# - errchkjson # lots of false positives
36+
# - errorlint # this check crashes
37+
# - exhaustive # silly check
38+
# - makezero # false positives
39+
# - nilerr # several intentional
2640

2741
linters-settings:
2842
gofmt:
2943
simplify: true
3044
goconst:
3145
min-len: 3 # minimum length of string constant
3246
min-occurrences: 6 # minimum number of occurrences
47+
gosec:
48+
excludes:
49+
- G404 # Use of weak random number generator - lots of FP
50+
- G107 # Potential http request -- those are intentional
51+
- G306 # G306: Expect WriteFile permissions to be 0600 or less
3352

3453
issues:
3554
exclude-rules:
36-
- path: crypto/blake2b/
37-
linters:
38-
- deadcode
39-
- path: crypto/bn256/cloudflare
40-
linters:
41-
- deadcode
42-
- path: p2p/discv5/
43-
linters:
44-
- deadcode
45-
- path: core/vm/instructions_test.go
46-
linters:
47-
- goconst
48-
- path: cmd/faucet/
55+
- path: crypto/bn256/cloudflare/optate.go
4956
linters:
5057
- deadcode
58+
- staticcheck
59+
- path: internal/build/pgp.go
60+
text: 'SA1019: package golang.org/x/crypto/openpgp is deprecated'
61+
- path: core/vm/contracts.go
62+
text: 'SA1019: package golang.org/x/crypto/ripemd160 is deprecated'
63+
- path: accounts/usbwallet/trezor.go
64+
text: 'SA1019: package github.com/golang/protobuf/proto is deprecated'
65+
- path: accounts/usbwallet/trezor/
66+
text: 'SA1019: package github.com/golang/protobuf/proto is deprecated'
67+
exclude:
68+
- 'SA1019: event.TypeMux is deprecated: use Feed'
69+
- 'SA1019: strings.Title is deprecated'
70+
- 'SA1029: should not use built-in type string as key for value'
71+
- 'G306: Expect WriteFile permissions to be 0600 or less'

accounts/abi/abi_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,9 +1038,7 @@ func TestABI_EventById(t *testing.T) {
10381038
}
10391039
if event == nil {
10401040
t.Errorf("We should find a event for topic %s, test #%d", topicID.Hex(), testnum)
1041-
}
1042-
1043-
if event.ID != topicID {
1041+
} else if event.ID != topicID {
10441042
t.Errorf("Event id %s does not match topic %s, test #%d", event.ID.Hex(), topicID.Hex(), testnum)
10451043
}
10461044

accounts/abi/bind/backends/simulated_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,7 @@ func TestHeaderByNumber(t *testing.T) {
655655
}
656656
if latestBlockHeader == nil {
657657
t.Errorf("received a nil block header")
658-
}
659-
if latestBlockHeader.Number.Uint64() != uint64(0) {
658+
} else if latestBlockHeader.Number.Uint64() != uint64(0) {
660659
t.Errorf("expected block header number 0, instead got %v", latestBlockHeader.Number.Uint64())
661660
}
662661

accounts/external/backend.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,6 @@ func (api *ExternalSigner) SelfDerive(bases []accounts.DerivationPath, chain eth
152152
log.Error("operation SelfDerive not supported on external signers")
153153
}
154154

155-
func (api *ExternalSigner) signHash(account accounts.Account, hash []byte) ([]byte, error) {
156-
return []byte{}, fmt.Errorf("operation not supported on external signers")
157-
}
158-
159155
// SignData signs keccak256(data). The mimetype parameter describes the type of data being signed
160156
func (api *ExternalSigner) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) {
161157
var res hexutil.Bytes

accounts/keystore/account_cache_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func TestUpdatedKeyfileContents(t *testing.T) {
383383
time.Sleep(1000 * time.Millisecond)
384384

385385
// Now replace file contents with crap
386-
if err := os.WriteFile(file, []byte("foo"), 0644); err != nil {
386+
if err := os.WriteFile(file, []byte("foo"), 0600); err != nil {
387387
t.Fatal(err)
388388
return
389389
}

accounts/keystore/passphrase_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestKeyEncryptDecrypt(t *testing.T) {
5252
t.Errorf("test %d: key address mismatch: have %x, want %x", i, key.Address, address)
5353
}
5454
// Recrypt with a new password and start over
55-
password += "new data appended"
55+
password += "new data appended" // nolint: gosec
5656
if keyjson, err = EncryptKey(key, password, veryLightScryptN, veryLightScryptP); err != nil {
5757
t.Errorf("test %d: failed to recrypt key %v", i, err)
5858
}

cmd/devp2p/internal/ethtest/snap.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func (s *Suite) TestSnapGetAccountRange(t *utesting.T) {
104104
// Max bytes: 0. Expect to deliver one account.
105105
{0, root, zero, ffHash, 1, firstKey, firstKey},
106106
} {
107+
tc := tc
107108
if err := s.snapGetAccountRange(t, &tc); err != nil {
108109
t.Errorf("test %d \n root: %x\n range: %#x - %#x\n bytes: %d\nfailed: %v", i, tc.root, tc.origin, tc.limit, tc.nBytes, err)
109110
}
@@ -194,6 +195,7 @@ func (s *Suite) TestSnapGetStorageRanges(t *utesting.T) {
194195
expSlots: 2,
195196
},
196197
} {
198+
tc := tc
197199
if err := s.snapGetStorageRanges(t, &tc); err != nil {
198200
t.Errorf("test %d \n root: %x\n range: %#x - %#x\n bytes: %d\n #accounts: %d\nfailed: %v",
199201
i, tc.root, tc.origin, tc.limit, tc.nBytes, len(tc.accounts), err)
@@ -291,6 +293,7 @@ func (s *Suite) TestSnapGetByteCodes(t *utesting.T) {
291293
expHashes: 4,
292294
},
293295
} {
296+
tc := tc
294297
if err := s.snapGetByteCodes(t, &tc); err != nil {
295298
t.Errorf("test %d \n bytes: %d\n #hashes: %d\nfailed: %v", i, tc.nBytes, len(tc.hashes), err)
296299
}
@@ -436,6 +439,7 @@ func (s *Suite) TestSnapTrieNodes(t *utesting.T) {
436439
},
437440
},
438441
} {
442+
tc := tc
439443
if err := s.snapGetTrieNodes(t, &tc); err != nil {
440444
t.Errorf("test %d \n #hashes %x\n root: %#x\n bytes: %d\nfailed: %v", i, len(tc.expHashes), tc.root, tc.nBytes, err)
441445
}

cmd/devp2p/internal/v5test/framework.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,9 @@ type conn struct {
6060
remoteAddr *net.UDPAddr
6161
listeners []net.PacketConn
6262

63-
log logger
64-
codec *v5wire.Codec
65-
lastRequest v5wire.Packet
66-
lastChallenge *v5wire.Whoareyou
67-
idCounter uint32
63+
log logger
64+
codec *v5wire.Codec
65+
idCounter uint32
6866
}
6967

7068
type logger interface {

cmd/geth/accountcmd.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,15 @@ func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrErr
239239
}
240240
fmt.Println("Testing your password against all of them...")
241241
var match *accounts.Account
242-
for _, a := range err.Matches {
243-
if err := ks.Unlock(a, auth); err == nil {
244-
match = &a
242+
for i, a := range err.Matches {
243+
if e := ks.Unlock(a, auth); e == nil {
244+
match = &err.Matches[i]
245245
break
246246
}
247247
}
248248
if match == nil {
249249
utils.Fatalf("None of the listed files could be unlocked.")
250+
return accounts.Account{}
250251
}
251252
fmt.Printf("Your password unlocked %s\n", match.URL)
252253
fmt.Println("In order to avoid this warning, you need to remove the following duplicate key files:")

cmd/geth/les_test.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -81,41 +81,6 @@ func (g *gethrpc) getNodeInfo() *p2p.NodeInfo {
8181
return g.nodeInfo
8282
}
8383

84-
func (g *gethrpc) waitSynced() {
85-
// Check if it's synced now
86-
var result interface{}
87-
g.callRPC(&result, "eth_syncing")
88-
syncing, ok := result.(bool)
89-
if ok && !syncing {
90-
g.geth.Logf("%v already synced", g.name)
91-
return
92-
}
93-
94-
// Actually wait, subscribe to the event
95-
ch := make(chan interface{})
96-
sub, err := g.rpc.Subscribe(context.Background(), "eth", ch, "syncing")
97-
if err != nil {
98-
g.geth.Fatalf("%v syncing: %v", g.name, err)
99-
}
100-
defer sub.Unsubscribe()
101-
timeout := time.After(4 * time.Second)
102-
select {
103-
case ev := <-ch:
104-
g.geth.Log("'syncing' event", ev)
105-
syncing, ok := ev.(bool)
106-
if ok && !syncing {
107-
break
108-
}
109-
g.geth.Log("Other 'syncing' event", ev)
110-
case err := <-sub.Err():
111-
g.geth.Fatalf("%v notification: %v", g.name, err)
112-
break
113-
case <-timeout:
114-
g.geth.Fatalf("%v timeout syncing", g.name)
115-
break
116-
}
117-
}
118-
11984
// ipcEndpoint resolves an IPC endpoint based on a configured value, taking into
12085
// account the set data folders as well as the designated platform we're currently
12186
// running on.

0 commit comments

Comments
 (0)