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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-11-20 16:18:19 +01:00
parent 8bbdb93cf9
commit 391668f57a
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
10 changed files with 23 additions and 17 deletions

View File

@ -16,6 +16,7 @@ linters:
- misspell - misspell
- nakedret - nakedret
- nilerr # Detects code that returns nil even if it checks that the error is not nil. - 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 - predeclared
- revive - revive
- staticcheck - staticcheck

View File

@ -1,8 +1,8 @@
package formatter package formatter
import ( import (
"fmt"
"sort" "sort"
"strconv"
"strings" "strings"
"time" "time"
@ -171,13 +171,13 @@ func (c *buildCacheContext) LastUsedSince() string {
} }
func (c *buildCacheContext) UsageCount() string { func (c *buildCacheContext) UsageCount() string {
return fmt.Sprintf("%d", c.v.UsageCount) return strconv.Itoa(c.v.UsageCount)
} }
func (c *buildCacheContext) InUse() string { func (c *buildCacheContext) InUse() string {
return fmt.Sprintf("%t", c.v.InUse) return strconv.FormatBool(c.v.InUse)
} }
func (c *buildCacheContext) Shared() string { func (c *buildCacheContext) Shared() string {
return fmt.Sprintf("%t", c.v.Shared) return strconv.FormatBool(c.v.Shared)
} }

View File

@ -1,7 +1,7 @@
package formatter package formatter
import ( import (
"fmt" "strconv"
"time" "time"
"github.com/distribution/reference" "github.com/distribution/reference"
@ -256,7 +256,7 @@ func (c *imageContext) Containers() string {
if c.i.Containers == -1 { if c.i.Containers == -1 {
return "N/A" 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 // VirtualSize shows the virtual size of the image and all of its parent

View File

@ -8,6 +8,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"strconv"
"testing" "testing"
) )
@ -695,7 +696,7 @@ func BenchmarkPyramid(b *testing.B) {
for _, x := range [...]int{10, 100, 1000} { for _, x := range [...]int{10, 100, 1000} {
// Build a line with x cells. // Build a line with x cells.
line := bytes.Repeat([]byte("a\t"), x) 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() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
w := NewWriter(io.Discard, 4, 4, 1, ' ', 0) // no particular reason for these settings 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) lines[i] = bytes.Repeat([]byte("a\t"), w)
} }
for _, h := range [...]int{10, 100, 1000} { 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() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
w := NewWriter(io.Discard, 4, 4, 1, ' ', 0) // no particular reason for these settings w := NewWriter(io.Discard, 4, 4, 1, ' ', 0) // no particular reason for these settings

View File

@ -3,6 +3,7 @@ package image
import ( import (
"context" "context"
"fmt" "fmt"
"strconv"
"strings" "strings"
"github.com/docker/cli/cli" "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) { func runPrune(dockerCli command.Cli, options pruneOptions) (spaceReclaimed uint64, output string, err error) {
pruneFilters := options.filter.Value().Clone() 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) pruneFilters = command.PruneFilters(dockerCli, pruneFilters)
warning := danglingWarning warning := danglingWarning

View File

@ -1,7 +1,7 @@
package network package network
import ( import (
"fmt" "strconv"
"strings" "strings"
"github.com/docker/cli/cli/command/formatter" "github.com/docker/cli/cli/command/formatter"
@ -89,11 +89,11 @@ func (c *networkContext) Scope() string {
} }
func (c *networkContext) IPv6() string { func (c *networkContext) IPv6() string {
return fmt.Sprintf("%v", c.n.EnableIPv6) return strconv.FormatBool(c.n.EnableIPv6)
} }
func (c *networkContext) Internal() string { func (c *networkContext) Internal() string {
return fmt.Sprintf("%v", c.n.Internal) return strconv.FormatBool(c.n.Internal)
} }
func (c *networkContext) Labels() string { func (c *networkContext) Labels() string {

View File

@ -3,6 +3,7 @@ package service
import ( import (
"fmt" "fmt"
"sort" "sort"
"strconv"
"strings" "strings"
"time" "time"
@ -742,12 +743,12 @@ func (pr portRange) String() string {
if pr.pEnd > pr.pStart { if pr.pEnd > pr.pStart {
pub = fmt.Sprintf("%d-%d", pr.pStart, pr.pEnd) pub = fmt.Sprintf("%d-%d", pr.pStart, pr.pEnd)
} else { } else {
pub = fmt.Sprintf("%d", pr.pStart) pub = strconv.FormatUint(uint64(pr.pStart), 10)
} }
if pr.tEnd > pr.tStart { if pr.tEnd > pr.tStart {
tgt = fmt.Sprintf("%d-%d", pr.tStart, pr.tEnd) tgt = fmt.Sprintf("%d-%d", pr.tStart, pr.tEnd)
} else { } else {
tgt = fmt.Sprintf("%d", pr.tStart) tgt = strconv.FormatUint(uint64(pr.tStart), 10)
} }
return fmt.Sprintf("*:%s->%s/%s", pub, tgt, pr.protocol) return fmt.Sprintf("*:%s->%s/%s", pub, tgt, pr.protocol)
} }

View File

@ -46,7 +46,7 @@ func (i *Uint64Opt) Type() string {
// String returns a string repr of this option // String returns a string repr of this option
func (i *Uint64Opt) String() string { func (i *Uint64Opt) String() string {
if i.value != nil { if i.value != nil {
return fmt.Sprintf("%v", *i.value) return strconv.FormatUint(*i.value, 10)
} }
return "" return ""
} }

View File

@ -6,6 +6,7 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"sort" "sort"
"strconv"
"strings" "strings"
"time" "time"
@ -717,7 +718,7 @@ var transformServicePort TransformerFunc = func(data interface{}) (interface{},
for _, entry := range entries { for _, entry := range entries {
switch value := entry.(type) { switch value := entry.(type) {
case int: case int:
v, err := toServicePortConfigs(fmt.Sprint(value)) v, err := toServicePortConfigs(strconv.Itoa(value))
if err != nil { if err != nil {
return data, err return data, err
} }

View File

@ -3,6 +3,7 @@ package types
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"strconv"
"time" "time"
) )
@ -565,7 +566,7 @@ func (e External) MarshalYAML() (interface{}, error) {
// MarshalJSON makes External implement json.Marshaller // MarshalJSON makes External implement json.Marshaller
func (e External) MarshalJSON() ([]byte, error) { func (e External) MarshalJSON() ([]byte, error) {
if e.Name == "" { 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 return []byte(fmt.Sprintf(`{"name": %q}`, e.Name)), nil
} }