From 391668f57ab792c13555a10be6cfdae4a8e9b15c Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 20 Nov 2023 16:18:19 +0100 Subject: [PATCH] golangci-lint: enable perfsprint linter cli/compose/types/types.go:568:17: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint) return []byte(fmt.Sprintf("%v", e.External)), nil ^ cli/command/formatter/buildcache.go:174:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint) return fmt.Sprintf("%d", c.v.UsageCount) ^ cli/command/formatter/buildcache.go:178:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint) return fmt.Sprintf("%t", c.v.InUse) ^ cli/command/formatter/buildcache.go:182:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint) return fmt.Sprintf("%t", c.v.Shared) ^ cli/command/formatter/image.go:259:9: fmt.Sprintf can be replaced with faster strconv.FormatInt (perfsprint) return fmt.Sprintf("%d", c.i.Containers) ^ cli/command/formatter/tabwriter/tabwriter_test.go:698:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint) b.Run(fmt.Sprintf("%d", x), func(b *testing.B) { ^ cli/command/formatter/tabwriter/tabwriter_test.go:720:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint) b.Run(fmt.Sprintf("%d", h), func(b *testing.B) { ^ cli/command/image/prune.go:62:31: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint) pruneFilters.Add("dangling", fmt.Sprintf("%v", !options.all)) ^ cli/command/network/formatter.go:92:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint) return fmt.Sprintf("%v", c.n.EnableIPv6) ^ cli/command/network/formatter.go:96:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint) return fmt.Sprintf("%v", c.n.Internal) ^ cli/command/service/formatter.go:745:9: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint) pub = fmt.Sprintf("%d", pr.pStart) ^ cli/command/service/formatter.go:750:9: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint) tgt = fmt.Sprintf("%d", pr.tStart) ^ cli/command/service/opts.go:49:10: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint) return fmt.Sprintf("%v", *i.value) ^ cli/compose/loader/loader.go:720:36: fmt.Sprint can be replaced with faster strconv.Itoa (perfsprint) v, err := toServicePortConfigs(fmt.Sprint(value)) ^ Signed-off-by: Sebastiaan van Stijn --- .golangci.yml | 1 + cli/command/formatter/buildcache.go | 8 ++++---- cli/command/formatter/image.go | 4 ++-- cli/command/formatter/tabwriter/tabwriter_test.go | 5 +++-- cli/command/image/prune.go | 3 ++- cli/command/network/formatter.go | 6 +++--- cli/command/service/formatter.go | 5 +++-- cli/command/service/opts.go | 2 +- cli/compose/loader/loader.go | 3 ++- cli/compose/types/types.go | 3 ++- 10 files changed, 23 insertions(+), 17 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index b7b597804a..c5b9e4696c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -16,6 +16,7 @@ linters: - misspell - nakedret - nilerr # Detects code that returns nil even if it checks that the error is not nil. + - perfsprint # Detects fmt.Sprintf uses that can be replaced with a faster alternative. - predeclared - revive - staticcheck diff --git a/cli/command/formatter/buildcache.go b/cli/command/formatter/buildcache.go index f6fbe72d3e..71f80c0c17 100644 --- a/cli/command/formatter/buildcache.go +++ b/cli/command/formatter/buildcache.go @@ -1,8 +1,8 @@ package formatter import ( - "fmt" "sort" + "strconv" "strings" "time" @@ -171,13 +171,13 @@ func (c *buildCacheContext) LastUsedSince() string { } func (c *buildCacheContext) UsageCount() string { - return fmt.Sprintf("%d", c.v.UsageCount) + return strconv.Itoa(c.v.UsageCount) } func (c *buildCacheContext) InUse() string { - return fmt.Sprintf("%t", c.v.InUse) + return strconv.FormatBool(c.v.InUse) } func (c *buildCacheContext) Shared() string { - return fmt.Sprintf("%t", c.v.Shared) + return strconv.FormatBool(c.v.Shared) } diff --git a/cli/command/formatter/image.go b/cli/command/formatter/image.go index c7eeb1e39c..b2075674ba 100644 --- a/cli/command/formatter/image.go +++ b/cli/command/formatter/image.go @@ -1,7 +1,7 @@ package formatter import ( - "fmt" + "strconv" "time" "github.com/distribution/reference" @@ -256,7 +256,7 @@ func (c *imageContext) Containers() string { if c.i.Containers == -1 { return "N/A" } - return fmt.Sprintf("%d", c.i.Containers) + return strconv.FormatInt(c.i.Containers, 10) } // VirtualSize shows the virtual size of the image and all of its parent diff --git a/cli/command/formatter/tabwriter/tabwriter_test.go b/cli/command/formatter/tabwriter/tabwriter_test.go index 45669b36d8..5f61d87aae 100644 --- a/cli/command/formatter/tabwriter/tabwriter_test.go +++ b/cli/command/formatter/tabwriter/tabwriter_test.go @@ -8,6 +8,7 @@ import ( "bytes" "fmt" "io" + "strconv" "testing" ) @@ -695,7 +696,7 @@ func BenchmarkPyramid(b *testing.B) { for _, x := range [...]int{10, 100, 1000} { // Build a line with x cells. line := bytes.Repeat([]byte("a\t"), x) - b.Run(fmt.Sprintf("%d", x), func(b *testing.B) { + b.Run(strconv.Itoa(x), func(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { w := NewWriter(io.Discard, 4, 4, 1, ' ', 0) // no particular reason for these settings @@ -717,7 +718,7 @@ func BenchmarkRagged(b *testing.B) { lines[i] = bytes.Repeat([]byte("a\t"), w) } for _, h := range [...]int{10, 100, 1000} { - b.Run(fmt.Sprintf("%d", h), func(b *testing.B) { + b.Run(strconv.Itoa(h), func(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { w := NewWriter(io.Discard, 4, 4, 1, ' ', 0) // no particular reason for these settings diff --git a/cli/command/image/prune.go b/cli/command/image/prune.go index c94c1be596..e3bf349910 100644 --- a/cli/command/image/prune.go +++ b/cli/command/image/prune.go @@ -3,6 +3,7 @@ package image import ( "context" "fmt" + "strconv" "strings" "github.com/docker/cli/cli" @@ -59,7 +60,7 @@ Are you sure you want to continue?` func runPrune(dockerCli command.Cli, options pruneOptions) (spaceReclaimed uint64, output string, err error) { pruneFilters := options.filter.Value().Clone() - pruneFilters.Add("dangling", fmt.Sprintf("%v", !options.all)) + pruneFilters.Add("dangling", strconv.FormatBool(!options.all)) pruneFilters = command.PruneFilters(dockerCli, pruneFilters) warning := danglingWarning diff --git a/cli/command/network/formatter.go b/cli/command/network/formatter.go index 604095a9d4..5e8765df6f 100644 --- a/cli/command/network/formatter.go +++ b/cli/command/network/formatter.go @@ -1,7 +1,7 @@ package network import ( - "fmt" + "strconv" "strings" "github.com/docker/cli/cli/command/formatter" @@ -89,11 +89,11 @@ func (c *networkContext) Scope() string { } func (c *networkContext) IPv6() string { - return fmt.Sprintf("%v", c.n.EnableIPv6) + return strconv.FormatBool(c.n.EnableIPv6) } func (c *networkContext) Internal() string { - return fmt.Sprintf("%v", c.n.Internal) + return strconv.FormatBool(c.n.Internal) } func (c *networkContext) Labels() string { diff --git a/cli/command/service/formatter.go b/cli/command/service/formatter.go index 91b6a3f830..0dc02d3522 100644 --- a/cli/command/service/formatter.go +++ b/cli/command/service/formatter.go @@ -3,6 +3,7 @@ package service import ( "fmt" "sort" + "strconv" "strings" "time" @@ -742,12 +743,12 @@ func (pr portRange) String() string { if pr.pEnd > pr.pStart { pub = fmt.Sprintf("%d-%d", pr.pStart, pr.pEnd) } else { - pub = fmt.Sprintf("%d", pr.pStart) + pub = strconv.FormatUint(uint64(pr.pStart), 10) } if pr.tEnd > pr.tStart { tgt = fmt.Sprintf("%d-%d", pr.tStart, pr.tEnd) } else { - tgt = fmt.Sprintf("%d", pr.tStart) + tgt = strconv.FormatUint(uint64(pr.tStart), 10) } return fmt.Sprintf("*:%s->%s/%s", pub, tgt, pr.protocol) } diff --git a/cli/command/service/opts.go b/cli/command/service/opts.go index 67c19fefd1..d36afa5ef1 100644 --- a/cli/command/service/opts.go +++ b/cli/command/service/opts.go @@ -46,7 +46,7 @@ func (i *Uint64Opt) Type() string { // String returns a string repr of this option func (i *Uint64Opt) String() string { if i.value != nil { - return fmt.Sprintf("%v", *i.value) + return strconv.FormatUint(*i.value, 10) } return "" } diff --git a/cli/compose/loader/loader.go b/cli/compose/loader/loader.go index c5edd5c51f..a507181960 100644 --- a/cli/compose/loader/loader.go +++ b/cli/compose/loader/loader.go @@ -6,6 +6,7 @@ import ( "path/filepath" "reflect" "sort" + "strconv" "strings" "time" @@ -717,7 +718,7 @@ var transformServicePort TransformerFunc = func(data interface{}) (interface{}, for _, entry := range entries { switch value := entry.(type) { case int: - v, err := toServicePortConfigs(fmt.Sprint(value)) + v, err := toServicePortConfigs(strconv.Itoa(value)) if err != nil { return data, err } diff --git a/cli/compose/types/types.go b/cli/compose/types/types.go index 964e5f5e33..f606f9ea8a 100644 --- a/cli/compose/types/types.go +++ b/cli/compose/types/types.go @@ -3,6 +3,7 @@ package types import ( "encoding/json" "fmt" + "strconv" "time" ) @@ -565,7 +566,7 @@ func (e External) MarshalYAML() (interface{}, error) { // MarshalJSON makes External implement json.Marshaller func (e External) MarshalJSON() ([]byte, error) { if e.Name == "" { - return []byte(fmt.Sprintf("%v", e.External)), nil + return []byte(strconv.FormatBool(e.External)), nil } return []byte(fmt.Sprintf(`{"name": %q}`, e.Name)), nil }