Fix deepsource findings (#7457)

* remove unused code

* Incomplete condition fix

* club append to single call

* anti-pattern fix 'should use for range instead of for { select {} }'

* use strings.ReplaceAll

* replace `len(parts[0]) == 0` with `parts[0] == ""`

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Preston Van Loon 2020-10-08 01:36:47 -07:00 committed by GitHub
parent 390a589afb
commit d4e6ce6998
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 19 additions and 79 deletions

View File

@ -210,19 +210,14 @@ func (h *stateRootHasher) validatorRoot(hasher htrutils.HashFn, validator *ethpb
} else {
slashBuf[0] = uint8(0)
}
fieldRoots = append(fieldRoots, slashBuf)
// Activation eligibility epoch.
fieldRoots = append(fieldRoots, activationEligibilityBuf)
// Activation epoch.
fieldRoots = append(fieldRoots, activationBuf)
// Exit epoch.
fieldRoots = append(fieldRoots, exitBuf)
// Withdrawable epoch.
fieldRoots = append(fieldRoots, withdrawalBuf)
fieldRoots = append(
fieldRoots,
slashBuf,
activationEligibilityBuf,
activationBuf,
exitBuf,
withdrawalBuf,
)
}
valRoot, err := htrutils.BitwiseMerkleizeArrays(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))

View File

@ -146,7 +146,7 @@ func TestStore_SaveProposerSlashing(t *testing.T) {
}
t.Log(diff)
if proposerSlashings == nil || !reflect.DeepEqual(proposerSlashings[0], tt.ps) {
if len(proposerSlashings) == 0 || !reflect.DeepEqual(proposerSlashings[0], tt.ps) {
t.Fatalf("Proposer slashing: %v should be part of proposer slashings response: %v", tt.ps, proposerSlashings)
}
}

View File

@ -1,61 +1,10 @@
package ineffassign
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"sort"
"strings"
)
// The core of this code originates here: https://github.com/gordonklaus/ineffassign and was
// adapted for our workflow (the original source code is under the MIT license).
// If you need a standalone CLI ineffassign runner, please, use the one above.
func walkPath(root string) bool {
lintFailed := false
err := filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
if err != nil {
fmt.Printf("Error during filesystem walk: %v\n", err)
return nil
}
if !strings.HasSuffix(path, ".go") {
return nil
}
fset, _, ineff := checkPath(path)
for _, id := range ineff {
fmt.Printf("%s: ineffectual assignment to %s\n", fset.Position(id.Pos()), id.Name)
lintFailed = true
}
return nil
})
if err != nil {
return false
}
return lintFailed
}
func checkPath(path string) (*token.FileSet, []*ast.CommentGroup, []*ast.Ident) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
return nil, nil, nil
}
bld := &builder{vars: map[*ast.Object]*variable{}}
bld.walk(f)
chk := &checker{vars: bld.vars, seen: map[*block]bool{}}
for _, b := range bld.roots {
chk.check(b)
}
sort.Sort(chk.ineff)
return fset, f.Comments, chk.ineff
}
type builder struct {
roots []*block
block *block

View File

@ -54,14 +54,11 @@ func main() {
ticker := time.NewTicker(time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Second)
go func() {
for {
select {
case <-ticker.C:
if *compare {
compareHeads(clients)
} else {
displayHeads(clients)
}
for range ticker.C {
if *compare {
compareHeads(clients)
} else {
displayHeads(clients)
}
}
}()

View File

@ -217,10 +217,10 @@ func HandleEmptyKeystoreFlags(cliCtx *cli.Context, confirmPassword bool) (string
if err != nil {
return path, passphrase, errors.Wrap(err, "could not read input path")
}
if text = strings.Replace(text, "\n", "", -1); text != "" {
if text = strings.ReplaceAll(text, "\n", ""); text != "" {
path = text
}
if text = strings.Replace(text, "\r", "", -1); text != "" {
if text = strings.ReplaceAll(text, "\r", ""); text != "" {
path = text
}
}

View File

@ -61,7 +61,7 @@ func NewKeystore(input string) (KeyManager, string, error) {
return nil, keystoreOptsHelp, err
}
text := string(bytePassword)
opts.Passphrase = strings.Replace(text, "\n", "", -1)
opts.Passphrase = strings.ReplaceAll(text, "\n", "")
}
if err := v1.VerifyAccountNotExists(opts.Path, opts.Passphrase); err == nil {

View File

@ -302,7 +302,7 @@ func pathsToVerificationRegexes(paths []string) []*regexp.Regexp {
for _, path := range paths {
log := log.WithField("path", path)
parts := strings.Split(path, "/")
if len(parts) == 0 || len(parts[0]) == 0 {
if len(parts) == 0 || parts[0] == "" {
log.Debug("Invalid path")
continue
}

View File

@ -85,7 +85,7 @@ func NewWallet(input string) (KeyManager, string, error) {
ctx := context.Background()
for _, path := range opts.Accounts {
parts := strings.Split(path, "/")
if len(parts[0]) == 0 {
if parts[0] == "" {
return nil, walletOptsHelp, fmt.Errorf("did not understand account specifier %q", path)
}
wallet, err := e2wallet.OpenWallet(parts[0], e2wallet.WithStore(store))

View File

@ -16,7 +16,6 @@ var log = logrus.WithField("prefix", "gateway")
// Gateway is the gRPC gateway to serve HTTP JSON traffic as a
// proxy and forward it to the gRPC server.
type Gateway struct {
conn *grpc.ClientConn
ctx context.Context
cancel context.CancelFunc
gatewayAddr string