From 67a67131f66c0cd16032dbbae3a2b3b021394b08 Mon Sep 17 00:00:00 2001 From: Zach Ramsay Date: Sun, 10 Dec 2017 15:46:33 +0000 Subject: [PATCH] all: address new gosimple lints --- accounts/abi/bind/bind.go | 2 +- accounts/abi/unpack_test.go | 4 ++-- bmt/bmt.go | 3 +-- consensus/clique/clique.go | 2 +- console/console_test.go | 14 +++++++------- core/asm/asm.go | 5 +---- core/asm/compiler.go | 5 +---- ethstats/ethstats.go | 1 - p2p/simulations/network.go | 7 +++---- rpc/http_test.go | 2 +- swarm/fuse/swarmfs_test.go | 10 ++++------ swarm/storage/chunker_test.go | 4 ++-- swarm/storage/pyramid.go | 16 ++++++++-------- whisper/whisperv6/whisper_test.go | 3 +-- 14 files changed, 33 insertions(+), 45 deletions(-) diff --git a/accounts/abi/bind/bind.go b/accounts/abi/bind/bind.go index f587580884..4dce79b779 100644 --- a/accounts/abi/bind/bind.go +++ b/accounts/abi/bind/bind.go @@ -129,7 +129,7 @@ func Bind(types []string, abis []string, bytecodes []string, pkg string, lang La return string(code), nil } // For all others just return as is for now - return string(buffer.Bytes()), nil + return buffer.String(), nil } // bindType is a set of type binders that convert Solidity types to some supported diff --git a/accounts/abi/unpack_test.go b/accounts/abi/unpack_test.go index 2949083788..1e21aafc05 100644 --- a/accounts/abi/unpack_test.go +++ b/accounts/abi/unpack_test.go @@ -368,11 +368,11 @@ func TestUnmarshal(t *testing.T) { if err != nil { t.Error(err) } else { - if bytes.Compare(p0, p0Exp) != 0 { + if !bytes.Equal(p0, p0Exp) { t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0) } - if bytes.Compare(p1[:], p1Exp) != 0 { + if !bytes.Equal(p1[:], p1Exp) { t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1) } } diff --git a/bmt/bmt.go b/bmt/bmt.go index d62365bb1f..4b65b1d94a 100644 --- a/bmt/bmt.go +++ b/bmt/bmt.go @@ -260,8 +260,7 @@ func NewTree(hasher BaseHasher, segmentSize, segmentCount int) *Tree { for d := 1; d <= depth(segmentCount); d++ { nodes := make([]*Node, count) for i := 0; i < len(nodes); i++ { - var parent *Node - parent = prevlevel[i/2] + parent := prevlevel[i/2] t := NewNode(level, i, parent) nodes[i] = t } diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index 6892d83906..be8b43e140 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -630,7 +630,7 @@ func (c *Clique) Seal(chain consensus.ChainReader, block *types.Block, stop <-ch } } // Sweet, the protocol permits us to sign the block, wait for our time - delay := time.Unix(header.Time.Int64(), 0).Sub(time.Now()) + delay := time.Until(time.Unix(header.Time.Int64(), 0)) if header.Difficulty.Cmp(diffNoTurn) == 0 { // It's not our turn explicitly to sign, delay it a bit wiggle := time.Duration(len(snap.Signers)/2+1) * wiggleTime diff --git a/console/console_test.go b/console/console_test.go index 05aec2cc0c..7b1629c032 100644 --- a/console/console_test.go +++ b/console/console_test.go @@ -164,7 +164,7 @@ func TestWelcome(t *testing.T) { tester.console.Welcome() - output := string(tester.output.Bytes()) + output := tester.output.String() if want := "Welcome"; !strings.Contains(output, want) { t.Fatalf("console output missing welcome message: have\n%s\nwant also %s", output, want) } @@ -188,7 +188,7 @@ func TestEvaluate(t *testing.T) { defer tester.Close(t) tester.console.Evaluate("2 + 2") - if output := string(tester.output.Bytes()); !strings.Contains(output, "4") { + if output := tester.output.String(); !strings.Contains(output, "4") { t.Fatalf("statement evaluation failed: have %s, want %s", output, "4") } } @@ -218,7 +218,7 @@ func TestInteractive(t *testing.T) { case <-time.After(time.Second): t.Fatalf("secondary prompt timeout") } - if output := string(tester.output.Bytes()); !strings.Contains(output, "4") { + if output := tester.output.String(); !strings.Contains(output, "4") { t.Fatalf("statement evaluation failed: have %s, want %s", output, "4") } } @@ -230,7 +230,7 @@ func TestPreload(t *testing.T) { defer tester.Close(t) tester.console.Evaluate("preloaded") - if output := string(tester.output.Bytes()); !strings.Contains(output, "some-preloaded-string") { + if output := tester.output.String(); !strings.Contains(output, "some-preloaded-string") { t.Fatalf("preloaded variable missing: have %s, want %s", output, "some-preloaded-string") } } @@ -243,7 +243,7 @@ func TestExecute(t *testing.T) { tester.console.Execute("exec.js") tester.console.Evaluate("execed") - if output := string(tester.output.Bytes()); !strings.Contains(output, "some-executed-string") { + if output := tester.output.String(); !strings.Contains(output, "some-executed-string") { t.Fatalf("execed variable missing: have %s, want %s", output, "some-executed-string") } } @@ -275,7 +275,7 @@ func TestPrettyPrint(t *testing.T) { string: ` + two + ` } ` - if output := string(tester.output.Bytes()); output != want { + if output := tester.output.String(); output != want { t.Fatalf("pretty print mismatch: have %s, want %s", output, want) } } @@ -287,7 +287,7 @@ func TestPrettyError(t *testing.T) { tester.console.Evaluate("throw 'hello'") want := jsre.ErrorColor("hello") + "\n" - if output := string(tester.output.Bytes()); output != want { + if output := tester.output.String(); output != want { t.Fatalf("pretty error mismatch: have %s, want %s", output, want) } } diff --git a/core/asm/asm.go b/core/asm/asm.go index 5fe827e7c1..ce22f93f92 100644 --- a/core/asm/asm.go +++ b/core/asm/asm.go @@ -114,10 +114,7 @@ func PrintDisassembled(code string) error { fmt.Printf("%06v: %v\n", it.PC(), it.Op()) } } - if err := it.Error(); err != nil { - return err - } - return nil + return it.Error() } // Return all disassembled EVM instructions in human-readable format. diff --git a/core/asm/compiler.go b/core/asm/compiler.go index b2c85375cc..318c4e4d80 100644 --- a/core/asm/compiler.go +++ b/core/asm/compiler.go @@ -237,10 +237,7 @@ func (c *Compiler) pushBin(v interface{}) { // isPush returns whether the string op is either any of // push(N). func isPush(op string) bool { - if op == "push" { - return true - } - return false + return op == "push" } // isJump returns whether the string op is jump(i) diff --git a/ethstats/ethstats.go b/ethstats/ethstats.go index 77784ff4ac..7065d71629 100644 --- a/ethstats/ethstats.go +++ b/ethstats/ethstats.go @@ -193,7 +193,6 @@ func (s *Service) loop() { } } close(quitCh) - return }() // Loop reporting until termination for { diff --git a/p2p/simulations/network.go b/p2p/simulations/network.go index fd8777673e..f3dda2e441 100644 --- a/p2p/simulations/network.go +++ b/p2p/simulations/network.go @@ -403,9 +403,8 @@ func (self *Network) getNodeByName(name string) *Node { func (self *Network) GetNodes() (nodes []*Node) { self.lock.Lock() defer self.lock.Unlock() - for _, node := range self.Nodes { - nodes = append(nodes, node) - } + + nodes = append(nodes, self.Nodes...) return nodes } @@ -477,7 +476,7 @@ func (self *Network) InitConn(oneID, otherID discover.NodeID) (*Conn, error) { if err != nil { return nil, err } - if time.Now().Sub(conn.initiated) < dialBanTimeout { + if time.Since(conn.initiated) < dialBanTimeout { return nil, fmt.Errorf("connection between %v and %v recently attempted", oneID, otherID) } if conn.Up { diff --git a/rpc/http_test.go b/rpc/http_test.go index 1cb7a7acbf..38196b48b4 100644 --- a/rpc/http_test.go +++ b/rpc/http_test.go @@ -32,7 +32,7 @@ func TestHTTPErrorResponseWithPut(t *testing.T) { } func TestHTTPErrorResponseWithMaxContentLength(t *testing.T) { - body := make([]rune, maxHTTPRequestContentLength+1, maxHTTPRequestContentLength+1) + body := make([]rune, maxHTTPRequestContentLength+1) testHTTPErrorResponse(t, "POST", contentType, string(body), http.StatusRequestEntityTooLarge) } diff --git a/swarm/fuse/swarmfs_test.go b/swarm/fuse/swarmfs_test.go index 93f1d4c2fd..42af36345f 100644 --- a/swarm/fuse/swarmfs_test.go +++ b/swarm/fuse/swarmfs_test.go @@ -95,7 +95,7 @@ func mountDir(t *testing.T, api *api.Api, files map[string]fileInfo, bzzHash str } // Test listMounts - if found == false { + if !found { t.Fatalf("Error getting mounts information for %v: %v", mountDir, err) } @@ -185,10 +185,8 @@ func isDirEmpty(name string) bool { defer f.Close() _, err = f.Readdirnames(1) - if err == io.EOF { - return true - } - return false + + return err == io.EOF } type testAPI struct { @@ -388,7 +386,7 @@ func (ta *testAPI) seekInMultiChunkFile(t *testing.T) { d.Read(contents) finfo := files["1.txt"] - if bytes.Compare(finfo.contents[:6024][5000:], contents) != 0 { + if !bytes.Equal(finfo.contents[:6024][5000:], contents) { t.Fatalf("File seek contents mismatch") } d.Close() diff --git a/swarm/storage/chunker_test.go b/swarm/storage/chunker_test.go index b41d7dd333..6178a7bb1c 100644 --- a/swarm/storage/chunker_test.go +++ b/swarm/storage/chunker_test.go @@ -77,7 +77,7 @@ func (self *chunkerTester) Split(chunker Splitter, data io.Reader, size int64, c key, err = chunker.Split(data, size, chunkC, swg, nil) if err != nil && expectedError == nil { - err = errors.New(fmt.Sprintf("Split error: %v", err)) + err = fmt.Errorf("Split error: %v", err) } if chunkC != nil { @@ -123,7 +123,7 @@ func (self *chunkerTester) Append(chunker Splitter, rootKey Key, data io.Reader, key, err = chunker.Append(rootKey, data, chunkC, swg, nil) if err != nil && expectedError == nil { - err = errors.New(fmt.Sprintf("Append error: %v", err)) + err = fmt.Errorf("Append error: %v", err) } if chunkC != nil { diff --git a/swarm/storage/pyramid.go b/swarm/storage/pyramid.go index 5de385dcbe..f2e85cb5b0 100644 --- a/swarm/storage/pyramid.go +++ b/swarm/storage/pyramid.go @@ -391,7 +391,7 @@ func (self *PyramidChunker) prepareChunks(isAppend bool, chunkLevel [][]*TreeEnt parent := NewTreeEntry(self) var unFinishedChunk *Chunk - if isAppend == true && len(chunkLevel[0]) != 0 { + if isAppend && len(chunkLevel[0]) != 0 { lastIndex := len(chunkLevel[0]) - 1 ent := chunkLevel[0][lastIndex] @@ -512,7 +512,7 @@ func (self *PyramidChunker) buildTree(isAppend bool, chunkLevel [][]*TreeEntry, } } - if compress == false && last == false { + if !compress && !last { return } @@ -522,7 +522,7 @@ func (self *PyramidChunker) buildTree(isAppend bool, chunkLevel [][]*TreeEntry, for lvl := int64(ent.level); lvl < endLvl; lvl++ { lvlCount := int64(len(chunkLevel[lvl])) - if lvlCount == 1 && last == true { + if lvlCount == 1 && last { copy(rootKey, chunkLevel[lvl][0].key) return } @@ -540,7 +540,7 @@ func (self *PyramidChunker) buildTree(isAppend bool, chunkLevel [][]*TreeEntry, nextLvlCount = int64(len(chunkLevel[lvl+1]) - 1) tempEntry = chunkLevel[lvl+1][nextLvlCount] } - if isAppend == true && tempEntry != nil && tempEntry.updatePending == true { + if isAppend && tempEntry != nil && tempEntry.updatePending { updateEntry := &TreeEntry{ level: int(lvl + 1), branchCount: 0, @@ -585,9 +585,9 @@ func (self *PyramidChunker) buildTree(isAppend bool, chunkLevel [][]*TreeEntry, } - if isAppend == false { + if !isAppend { chunkWG.Wait() - if compress == true { + if compress { chunkLevel[lvl] = nil } } @@ -599,7 +599,7 @@ func (self *PyramidChunker) enqueueTreeChunk(chunkLevel [][]*TreeEntry, ent *Tre if ent != nil { // wait for data chunks to get over before processing the tree chunk - if last == true { + if last { chunkWG.Wait() } @@ -612,7 +612,7 @@ func (self *PyramidChunker) enqueueTreeChunk(chunkLevel [][]*TreeEntry, ent *Tre } // Update or append based on weather it is a new entry or being reused - if ent.updatePending == true { + if ent.updatePending { chunkWG.Wait() chunkLevel[ent.level][ent.index] = ent } else { diff --git a/whisper/whisperv6/whisper_test.go b/whisper/whisperv6/whisper_test.go index aeb54b3ab8..c8f3a9ed74 100644 --- a/whisper/whisperv6/whisper_test.go +++ b/whisper/whisperv6/whisper_test.go @@ -80,8 +80,7 @@ func TestWhisperBasic(t *testing.T) { t.Fatalf("failed w.Messages.") } - var derived []byte - derived = pbkdf2.Key([]byte(peerID), nil, 65356, aesKeyLength, sha256.New) + derived := pbkdf2.Key([]byte(peerID), nil, 65356, aesKeyLength, sha256.New) if !validateSymmetricKey(derived) { t.Fatalf("failed validateSymmetricKey with param = %v.", derived) }