Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enable int-conversion from perfsprint #8194

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ linters-settings:
- simmilar
perfsprint:
# Optimizes even if it requires an int or uint type cast.
int-conversion: false
int-conversion: true
# Optimizes into `err.Error()` even if it is only equivalent for non-nil errors.
err-error: true
# Optimizes `fmt.Errorf`.
Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/id.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dependency

import (
"fmt"
"strconv"
"strings"

"github.com/mitchellh/hashstructure/v2"
Expand Down Expand Up @@ -52,5 +52,5 @@ func UID(filePath string, pkg types.Package) string {
if err != nil {
log.Warn("Failed to calculate the package hash", log.String("pkg", pkg.Name), log.Err(err))
}
return fmt.Sprintf("%x", hash)
return strconv.FormatUint(hash, 16)
}
4 changes: 2 additions & 2 deletions pkg/flag/db_flags.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package flag

import (
"fmt"
"strconv"

"github.com/google/go-containerregistry/pkg/name"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -188,7 +188,7 @@ func parseRepository(repo string, dbSchemaVersion int) (name.Reference, error) {
return dbRepository, nil
}

dbRepository = t.Tag(fmt.Sprint(dbSchemaVersion))
dbRepository = t.Tag(strconv.Itoa(dbSchemaVersion))
log.Info("Adding schema version to the DB repository for backward compatibility",
log.String("repository", dbRepository.String()))

Expand Down
5 changes: 3 additions & 2 deletions pkg/iac/scan/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/fs"
"path/filepath"
"reflect"
"strconv"
"strings"

"github.com/aquasecurity/trivy/pkg/iac/ignore"
Expand Down Expand Up @@ -311,9 +312,9 @@ func rawToString(raw any) string {
}
switch t := raw.(type) {
case int:
return fmt.Sprintf("%d", t)
return strconv.Itoa(t)
case bool:
return fmt.Sprintf("%t", t)
return strconv.FormatBool(t)
case float64:
return fmt.Sprintf("%f", t)
case string:
Expand Down
3 changes: 2 additions & 1 deletion pkg/iac/scanners/azure/functions/utc_now_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package functions

import (
"fmt"
"strconv"
"testing"
"time"

Expand All @@ -20,7 +21,7 @@ func Test_UTCNow(t *testing.T) {
args: []any{
"d",
},
expected: fmt.Sprintf("%d", time.Now().UTC().Day()),
expected: strconv.Itoa(time.Now().UTC().Day()),
},
{
name: "utc now date",
Expand Down
2 changes: 1 addition & 1 deletion pkg/iac/scanners/cloudformation/parser/fn_sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func resolveMapSub(refValue, original *Property) (*Property, bool) {
case cftypes.Int:
replacement = strconv.Itoa(v.AsInt())
case cftypes.Bool:
replacement = fmt.Sprintf("%v", v.AsBool())
replacement = strconv.FormatBool(v.AsBool())
case cftypes.List:
var parts []string
for _, p := range v.AsList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (p *Property) convertToString() *Property {
case cftypes.Int:
return p.deriveResolved(cftypes.String, strconv.Itoa(p.AsInt()))
case cftypes.Bool:
return p.deriveResolved(cftypes.String, fmt.Sprintf("%v", p.AsBool()))
return p.deriveResolved(cftypes.String, strconv.FormatBool(p.AsBool()))
case cftypes.List:
var parts []string
for _, property := range p.AsList() {
Expand Down
5 changes: 3 additions & 2 deletions pkg/iac/scanners/terraform/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"sort"
"strconv"

"github.com/zclconf/go-cty/cty"

Expand Down Expand Up @@ -123,13 +124,13 @@ func ignoreByParams(params map[string]string, modules terraform.Modules, m *type
case cty.Number:
bf := val.AsBigFloat()
f64, _ := bf.Float64()
comparableInt := fmt.Sprintf("%d", int(f64))
comparableInt := strconv.Itoa(int(f64))
comparableFloat := fmt.Sprintf("%f", f64)
if param != comparableInt && param != comparableFloat {
return false
}
case cty.Bool:
if fmt.Sprintf("%t", val.True()) != param {
if strconv.FormatBool(val.True()) != param {
return false
}
default:
Expand Down
3 changes: 2 additions & 1 deletion pkg/iac/scanners/terraform/parser/funcs/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package funcs

import (
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -132,7 +133,7 @@ func parseTimestamp(ts string) (time.Time, error) {
case "Z07:00":
what = "UTC offset"
case "T":
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: missing required time introducer 'T'")
return time.Time{}, errors.New("not a valid RFC3339 timestamp: missing required time introducer 'T'")
Copy link
Contributor

@DmitriyLewen DmitriyLewen Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
return time.Time{}, errors.New("not a valid RFC3339 timestamp: missing required time introducer 'T'")
return time.Time{}, xerrors.New("not a valid RFC3339 timestamp: missing required time introducer 'T'")

case ":", "-":
if err.ValueElem == "" {
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: end of string where %q is expected", err.LayoutElem)
Expand Down
5 changes: 3 additions & 2 deletions pkg/report/table/misconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package table
import (
"bytes"
"fmt"
"strconv"
"strings"

"github.com/fatih/color"
Expand Down Expand Up @@ -168,7 +169,7 @@ func (r *misconfigRenderer) renderCode(misconf types.DetectedMisconfiguration) {
for i, occ := range misconf.CauseMetadata.Occurrences {
lineInfo := fmt.Sprintf("%d-%d", occ.Location.StartLine, occ.Location.EndLine)
if occ.Location.StartLine >= occ.Location.EndLine {
lineInfo = fmt.Sprintf("%d", occ.Location.StartLine)
lineInfo = strconv.Itoa(occ.Location.StartLine)
}

r.printf(
Expand All @@ -184,7 +185,7 @@ func (r *misconfigRenderer) renderCode(misconf types.DetectedMisconfiguration) {
for i, line := range lines {
switch {
case line.Truncated:
r.printf("<dim>%4s ", strings.Repeat(".", len(fmt.Sprintf("%d", line.Number))))
r.printf("<dim>%4s ", strings.Repeat(".", len(strconv.Itoa(line.Number))))
case line.IsCause:
r.printf("<red>%4d ", line.Number)
switch {
Expand Down
3 changes: 2 additions & 1 deletion pkg/report/table/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package table
import (
"bytes"
"fmt"
"strconv"
"strings"

"golang.org/x/term"
Expand Down Expand Up @@ -141,7 +142,7 @@ func (r *secretRenderer) renderCode(secret types.DetectedSecret) {
for i, line := range lines {
switch {
case line.Truncated:
r.printf("<dim>%4s ", strings.Repeat(".", len(fmt.Sprintf("%d", line.Number))))
r.printf("<dim>%4s ", strings.Repeat(".", len(strconv.Itoa(line.Number))))
case line.IsCause:
r.printf("<red>%4d ", line.Number)
switch {
Expand Down
3 changes: 2 additions & 1 deletion pkg/sbom/spdx/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"slices"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -527,7 +528,7 @@ func calcPkgID(h Hash, v any) (string, error) {
return "", xerrors.Errorf("could not build package ID for %+v: %w", v, err)
}

return fmt.Sprintf("%x", f), nil
return strconv.FormatUint(f, 16), nil
}

func camelCase(inputUnderScoreStr string) (camelCase string) {
Expand Down
Loading