diff --git a/.golangci.yaml b/.golangci.yaml index 9249f9308e91..1a033d51cb4a 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -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`. diff --git a/pkg/dependency/id.go b/pkg/dependency/id.go index 12ef6ef773b3..309abe946c5a 100644 --- a/pkg/dependency/id.go +++ b/pkg/dependency/id.go @@ -1,7 +1,7 @@ package dependency import ( - "fmt" + "strconv" "strings" "github.com/mitchellh/hashstructure/v2" @@ -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) } diff --git a/pkg/flag/db_flags.go b/pkg/flag/db_flags.go index cea4b82eaec6..c6887c98292e 100644 --- a/pkg/flag/db_flags.go +++ b/pkg/flag/db_flags.go @@ -1,7 +1,7 @@ package flag import ( - "fmt" + "strconv" "github.com/google/go-containerregistry/pkg/name" "golang.org/x/xerrors" @@ -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())) diff --git a/pkg/iac/scan/result.go b/pkg/iac/scan/result.go index 9c9e8c5e3274..f00e5691836e 100644 --- a/pkg/iac/scan/result.go +++ b/pkg/iac/scan/result.go @@ -5,6 +5,7 @@ import ( "io/fs" "path/filepath" "reflect" + "strconv" "strings" "github.com/aquasecurity/trivy/pkg/iac/ignore" @@ -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: diff --git a/pkg/iac/scanners/azure/functions/utc_now_test.go b/pkg/iac/scanners/azure/functions/utc_now_test.go index 5ee8a3c78021..94263af9f9aa 100644 --- a/pkg/iac/scanners/azure/functions/utc_now_test.go +++ b/pkg/iac/scanners/azure/functions/utc_now_test.go @@ -2,6 +2,7 @@ package functions import ( "fmt" + "strconv" "testing" "time" @@ -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", diff --git a/pkg/iac/scanners/cloudformation/parser/fn_sub.go b/pkg/iac/scanners/cloudformation/parser/fn_sub.go index 52db66cf9757..ad990bba3b32 100644 --- a/pkg/iac/scanners/cloudformation/parser/fn_sub.go +++ b/pkg/iac/scanners/cloudformation/parser/fn_sub.go @@ -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() { diff --git a/pkg/iac/scanners/cloudformation/parser/property_conversion.go b/pkg/iac/scanners/cloudformation/parser/property_conversion.go index 95f6a48bf380..1053afa1b3f7 100644 --- a/pkg/iac/scanners/cloudformation/parser/property_conversion.go +++ b/pkg/iac/scanners/cloudformation/parser/property_conversion.go @@ -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() { diff --git a/pkg/iac/scanners/terraform/executor/executor.go b/pkg/iac/scanners/terraform/executor/executor.go index 2714d50be6fe..339ea7e1aca6 100644 --- a/pkg/iac/scanners/terraform/executor/executor.go +++ b/pkg/iac/scanners/terraform/executor/executor.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "sort" + "strconv" "github.com/zclconf/go-cty/cty" @@ -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: diff --git a/pkg/iac/scanners/terraform/parser/funcs/datetime.go b/pkg/iac/scanners/terraform/parser/funcs/datetime.go index 11ed3c8a2214..b191ab2b1c38 100644 --- a/pkg/iac/scanners/terraform/parser/funcs/datetime.go +++ b/pkg/iac/scanners/terraform/parser/funcs/datetime.go @@ -2,6 +2,7 @@ package funcs import ( + "errors" "fmt" "time" @@ -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'") case ":", "-": if err.ValueElem == "" { return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: end of string where %q is expected", err.LayoutElem) diff --git a/pkg/report/table/misconfig.go b/pkg/report/table/misconfig.go index 41f111d883c9..604b7db0be67 100644 --- a/pkg/report/table/misconfig.go +++ b/pkg/report/table/misconfig.go @@ -3,6 +3,7 @@ package table import ( "bytes" "fmt" + "strconv" "strings" "github.com/fatih/color" @@ -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( @@ -184,7 +185,7 @@ func (r *misconfigRenderer) renderCode(misconf types.DetectedMisconfiguration) { for i, line := range lines { switch { case line.Truncated: - r.printf("%4s ", strings.Repeat(".", len(fmt.Sprintf("%d", line.Number)))) + r.printf("%4s ", strings.Repeat(".", len(strconv.Itoa(line.Number)))) case line.IsCause: r.printf("%4d ", line.Number) switch { diff --git a/pkg/report/table/secret.go b/pkg/report/table/secret.go index cff91a98d9ed..1b1cf78dda21 100644 --- a/pkg/report/table/secret.go +++ b/pkg/report/table/secret.go @@ -3,6 +3,7 @@ package table import ( "bytes" "fmt" + "strconv" "strings" "golang.org/x/term" @@ -141,7 +142,7 @@ func (r *secretRenderer) renderCode(secret types.DetectedSecret) { for i, line := range lines { switch { case line.Truncated: - r.printf("%4s ", strings.Repeat(".", len(fmt.Sprintf("%d", line.Number)))) + r.printf("%4s ", strings.Repeat(".", len(strconv.Itoa(line.Number)))) case line.IsCause: r.printf("%4d ", line.Number) switch { diff --git a/pkg/sbom/spdx/marshal.go b/pkg/sbom/spdx/marshal.go index 51f9144f682d..e17cd88c809c 100644 --- a/pkg/sbom/spdx/marshal.go +++ b/pkg/sbom/spdx/marshal.go @@ -5,6 +5,7 @@ import ( "fmt" "slices" "sort" + "strconv" "strings" "time" @@ -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) {