Skip to content

Commit

Permalink
Fix some gosimple issues
Browse files Browse the repository at this point in the history
See,
$ golangci-lint run --disable-all --enable=gosimple
govc/cluster/rule/ls.go:80:23: S1025: the argument is already a string, there's no need to use fmt.Sprintf (gosimple)
                                res = append(res, fmt.Sprintf("%s", ruleName))
                                                  ^
govc/dvs/portgroup/info.go:105:10: S1034: assigning the result of this type assertion to a variable (switch port := port.(type)) could eliminate the following type assertions:

        govc/dvs/portgroup/info.go:109:78 (gosimple)
                switch port.(type) {
                       ^
govc/dvs/portgroup/info.go:119:11: S1025: the argument is already a string, there's no need to use fmt.Sprintf (gosimple)
                        return fmt.Sprintf("%s", address.(*types.SingleIp).Address)
                               ^
govc/vm/info.go:105:3: S1020: when ok is true, err can't be nil (gosimple)
                if _, ok := err.(*find.NotFoundError); ok {
                ^
govc/vm/network/add.go:77:11: S1028: should use fmt.Errorf(...) instead of errors.New(fmt.Sprintf(...)) (gosimple)
                        return errors.New(fmt.Sprintf("couldn't set specified network %v",
                               ^
govc/vm/network/change.go:90:11: S1028: should use fmt.Errorf(...) instead of errors.New(fmt.Sprintf(...)) (gosimple)
                        return errors.New(fmt.Sprintf("couldn't set specified network %v",
                               ^
object/diagnostic_manager.go:74:2: S1031: unnecessary nil check around range (gosimple)
        if host != nil {
        ^
simulator/host_datastore_browser.go:131:5: S1008: should use 'return <expr>' instead of 'if <expr> { return <bool> }; return <bool>' (gosimple)
                                if strings.HasSuffix(name, "-flat.vmdk") {
                                ^
test/functional/helper.go:118:30: S1002: should omit comparison to bool constant, can be simplified to `*i.Vmfs.Local` (gosimple)
                        if i.Vmfs.Local != nil && *i.Vmfs.Local == true {
                                                  ^
toolbox/process_test.go:229:2: S1021: should merge variable declaration with assignment on next line (gosimple)
        var err error
        ^
toolbox/vix/protocol.go:819:46: S1030: should use buf.String() instead of string(buf.Bytes()) (gosimple)
        str, err := base64.StdEncoding.DecodeString(string(buf.Bytes()))
                                                    ^
units/size_test.go:35:12: S1025: should use String() instead of fmt.Sprintf (gosimple)
        actual := fmt.Sprintf("%s", b)
                  ^
vapi/library/finder/finder.go:74:2: S1017: should replace this `if` statement with an unconditional `strings.TrimPrefix` (gosimple)
        if strings.HasPrefix(ipath, "/") {
        ^
  • Loading branch information
mjtrangoni committed Feb 18, 2019
1 parent 2458627 commit d45b5f3
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 30 deletions.
2 changes: 1 addition & 1 deletion govc/cluster/rule/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
ruleTypeInfo := GetExtendedClusterRuleInfo(g).ruleType
res = append(res, fmt.Sprintf("%s (%s)", ruleName, ruleTypeInfo))
} else {
res = append(res, fmt.Sprintf("%s", ruleName))
res = append(res, ruleName)
}
}
} else {
Expand Down
9 changes: 5 additions & 4 deletions govc/dvs/portgroup/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ type infoResult struct {

func printPort(port types.BaseDvsIpPort) string {
if port != nil {
switch port.(type) {
switch portType := port.(type) {
case *types.DvsSingleIpPort:
return fmt.Sprintf("%d", port.(*types.DvsSingleIpPort).PortNumber)
return fmt.Sprintf("%d", portType.PortNumber)
case *types.DvsIpPortRange:
return fmt.Sprintf("%d-%d", port.(*types.DvsIpPortRange).StartPortNumber, port.(*types.DvsIpPortRange).EndPortNumber)
return fmt.Sprintf("%d-%d", portType.StartPortNumber,
portType.EndPortNumber)
}
}
return "Any"
Expand All @@ -116,7 +117,7 @@ func printAddress(address types.BaseIpAddress) string {
if address != nil {
switch (address).(type) {
case *types.SingleIp:
return fmt.Sprintf("%s", address.(*types.SingleIp).Address)
return address.(*types.SingleIp).Address
case *types.IpRange:
return fmt.Sprintf("%s/%d", address.(*types.IpRange).AddressPrefix, address.(*types.IpRange).PrefixLength)
}
Expand Down
1 change: 0 additions & 1 deletion govc/object/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,6 @@ func (cmd *collect) Run(ctx context.Context, f *flag.FlagSet) error {
if matches > 0 {
return true
}

return false
}

Expand Down
4 changes: 2 additions & 2 deletions govc/vm/network/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() > 0 {
err = cmd.NetworkFlag.Set(f.Arg(0))
if err != nil {
return errors.New(fmt.Sprintf("couldn't set specified network %v",
err))
return fmt.Errorf("couldn't set specified network %v",
err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions govc/vm/network/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func (cmd *change) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() > 1 {
err = cmd.NetworkFlag.Set(f.Arg(1))
if err != nil {
return errors.New(fmt.Sprintf("couldn't set specified network %v",
err))
return fmt.Errorf("couldn't set specified network %v",
err)
}
}

Expand Down
6 changes: 2 additions & 4 deletions object/diagnostic_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,8 @@ func (m DiagnosticManager) GenerateLogBundles(ctx context.Context, includeDefaul
IncludeDefault: includeDefault,
}

if host != nil {
for _, h := range host {
req.Host = append(req.Host, h.Reference())
}
for _, h := range host {
req.Host = append(req.Host, h.Reference())
}

res, err := methods.GenerateLogBundles_Task(ctx, m.c, &req)
Expand Down
11 changes: 6 additions & 5 deletions simulator/host_datastore_browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,13 @@ func (s *searchDatastore) queryMatch(file os.FileInfo) bool {
}
case *types.VmDiskFileQuery:
if ext == ".vmdk" {
if strings.HasSuffix(name, "-flat.vmdk") {
// only matches the descriptor, not the backing file(s)
return false
}
// if strings.HasSuffix(name, "-flat.vmdk") {
// // only matches the descriptor, not the backing file(s)
// return false
//}
// TODO: check Filter and Details fields
return true
//return true
return !strings.HasSuffix(name, "-flat.vmdk")
}
case *types.VmLogFileQuery:
if ext == ".log" {
Expand Down
2 changes: 1 addition & 1 deletion test/functional/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (h *Helper) LocalDatastores(ctx context.Context, cr *object.ComputeResource

switch i := mds.Info.(type) {
case *types.VmfsDatastoreInfo:
if i.Vmfs.Local != nil && *i.Vmfs.Local == true {
if i.Vmfs.Local != nil && *i.Vmfs.Local {
break
}
default:
Expand Down
4 changes: 1 addition & 3 deletions toolbox/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,7 @@ func TestEscapeXML(t *testing.T) {

func TestProcessError(t *testing.T) {
fault := errors.New("fail")
var err error

err = &ProcessError{Err: fault}
var err error = &ProcessError{Err: fault}

if err.Error() != fault.Error() {
t.Fatal()
Expand Down
2 changes: 1 addition & 1 deletion toolbox/vix/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ func (c *UserCredentialNamePassword) UnmarshalBinary(data []byte) error {
return err
}

str, err := base64.StdEncoding.DecodeString(string(buf.Bytes()))
str, err := base64.StdEncoding.DecodeString(buf.String())
if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions units/size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package units

import (
"fmt"
"math"
"testing"
)
Expand All @@ -31,8 +30,7 @@ func TestMB(t *testing.T) {
}

func TestTenMB(t *testing.T) {
b := ByteSize(10 * 1024 * 1024)
actual := fmt.Sprintf("%s", b)
actual := ByteSize(10 * 1024 * 1024).String()
expected := "10.0MB"
if actual != expected {
t.Errorf("Expected '%v' but got '%v'", expected, actual)
Expand Down
4 changes: 1 addition & 3 deletions vapi/library/finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ func (f *Finder) find(ctx context.Context, ipath string) ([]FindResult, error) {
}

// Get the argument and remove any leading separator characters.
if strings.HasPrefix(ipath, "/") {
ipath = ipath[1:]
}
ipath = strings.TrimPrefix(ipath, "/")

// Tokenize the path into its distinct parts.
parts := strings.Split(ipath, "/")
Expand Down

0 comments on commit d45b5f3

Please sign in to comment.