Skip to content

Commit 17951bc

Browse files
committed
fix: enable perfsprint linter
Signed-off-by: Matthieu MOREL <[email protected]>
1 parent efec326 commit 17951bc

File tree

37 files changed

+102
-69
lines changed

37 files changed

+102
-69
lines changed

.golangci.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ linters-settings:
8080
- licence
8181
- optimise
8282
- simmilar
83+
perfsprint:
84+
# Optimizes even if it requires an int or uint type cast.
85+
int-conversion: false
86+
# Optimizes into `err.Error()` even if it is only equivalent for non-nil errors.
87+
err-error: true
88+
# Optimizes `fmt.Errorf`.
89+
errorf: true
90+
# Optimizes `fmt.Sprintf` with only one argument.
91+
sprintf1: false
92+
# Optimizes into strings concatenation.
93+
strconcat: false
8394
revive:
8495
ignore-generated-header: true
8596
testifylint:
@@ -99,6 +110,7 @@ linters:
99110
- govet
100111
- ineffassign
101112
- misspell
113+
- perfsprint
102114
- revive
103115
- tenv
104116
- testifylint
@@ -139,5 +151,8 @@ issues:
139151
linters:
140152
- gocritic
141153
text: "importShadow:"
154+
- linters:
155+
- perfsprint
156+
text: "fmt.Sprint"
142157
exclude-use-default: false
143158
max-same-issues: 0

examples/module/spring4shell/spring4shell.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package main
55

66
import (
77
"bufio"
8+
"errors"
89
"fmt"
910
"io"
1011
"os"
@@ -112,7 +113,7 @@ func (Spring4Shell) parseTomcatReleaseNotes(f *os.File, filePath string) (*seria
112113

113114
m := tomcatVersionRegex.FindStringSubmatch(string(b))
114115
if len(m) != 2 {
115-
return nil, fmt.Errorf("unknown tomcat release notes format")
116+
return nil, errors.New("unknown tomcat release notes format")
116117
}
117118

118119
return &serialize.AnalysisResult{

magefiles/schema.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package main
55
import (
66
"bytes"
77
"encoding/json"
8-
"fmt"
8+
"errors"
99
"log"
1010
"os"
1111

@@ -66,7 +66,7 @@ func VerifySchema() error {
6666
return err
6767
}
6868
if !bytes.Equal(data, existing) {
69-
return fmt.Errorf("schema is out of date:\n\nplease run 'mage schema:generate' and commit the changes\n")
69+
return errors.New("schema is out of date:\n\nplease run 'mage schema:generate' and commit the changes\n")
7070
}
7171
return nil
7272
}

pkg/flag/kubernetes_flags.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package flag
22

33
import (
4+
"errors"
45
"fmt"
56
"strconv"
67
"strings"
@@ -192,10 +193,10 @@ func (f *K8sFlagGroup) ToOptions() (K8sOptions, error) {
192193
exludeNodeLabels[excludeNodeParts[0]] = excludeNodeParts[1]
193194
}
194195
if len(f.ExcludeNamespaces.Value()) > 0 && len(f.IncludeNamespaces.Value()) > 0 {
195-
return K8sOptions{}, fmt.Errorf("include-namespaces and exclude-namespaces flags cannot be used together")
196+
return K8sOptions{}, errors.New("include-namespaces and exclude-namespaces flags cannot be used together")
196197
}
197198
if len(f.ExcludeKinds.Value()) > 0 && len(f.IncludeKinds.Value()) > 0 {
198-
return K8sOptions{}, fmt.Errorf("include-kinds and exclude-kinds flags cannot be used together")
199+
return K8sOptions{}, errors.New("include-kinds and exclude-kinds flags cannot be used together")
199200
}
200201

201202
return K8sOptions{
@@ -222,12 +223,12 @@ func optionToTolerations(tolerationsOptions []string) ([]corev1.Toleration, erro
222223
for _, toleration := range tolerationsOptions {
223224
tolerationParts := strings.Split(toleration, ":")
224225
if len(tolerationParts) < 2 {
225-
return []corev1.Toleration{}, fmt.Errorf("toleration must include key and effect")
226+
return []corev1.Toleration{}, errors.New("toleration must include key and effect")
226227
}
227228
if corev1.TaintEffect(tolerationParts[1]) != corev1.TaintEffectNoSchedule &&
228229
corev1.TaintEffect(tolerationParts[1]) != corev1.TaintEffectPreferNoSchedule &&
229230
corev1.TaintEffect(tolerationParts[1]) != corev1.TaintEffectNoExecute {
230-
return []corev1.Toleration{}, fmt.Errorf("toleration effect must be a valid value")
231+
return []corev1.Toleration{}, errors.New("toleration effect must be a valid value")
231232
}
232233
keyValue := strings.Split(tolerationParts[0], "=")
233234
operator := corev1.TolerationOpEqual
@@ -245,7 +246,7 @@ func optionToTolerations(tolerationsOptions []string) ([]corev1.Toleration, erro
245246
if len(tolerationParts) == 3 {
246247
tolerationSec, err = strconv.Atoi(tolerationParts[2])
247248
if err != nil {
248-
return nil, fmt.Errorf("TolerationSeconds must must be a number")
249+
return nil, errors.New("TolerationSeconds must must be a number")
249250
}
250251
toleration.TolerationSeconds = lo.ToPtr(int64(tolerationSec))
251252
}

pkg/iac/adapters/cloudformation/aws/ecr/repository.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ecr
22

33
import (
4-
"fmt"
4+
"errors"
55

66
"github.com/liamg/iamgo"
77

@@ -60,7 +60,7 @@ func getRepositories(ctx parser.FileContext) (repositories []ecr.Repository) {
6060
func getPolicy(r *parser.Resource) (*iam.Policy, error) {
6161
policyProp := r.GetProperty("RepositoryPolicyText")
6262
if policyProp.IsNil() {
63-
return nil, fmt.Errorf("missing policy")
63+
return nil, errors.New("missing policy")
6464
}
6565

6666
parsed, err := iamgo.Parse(policyProp.GetJsonBytes())

pkg/iac/adapters/cloudformation/aws/sqs/queue.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package sqs
22

33
import (
4-
"fmt"
4+
"errors"
55

66
"github.com/liamg/iamgo"
77

@@ -59,5 +59,5 @@ func getPolicy(id string, ctx parser.FileContext) (*iam.Policy, error) {
5959
}
6060
}
6161
}
62-
return nil, fmt.Errorf("no matching policy found")
62+
return nil, errors.New("no matching policy found")
6363
}

pkg/iac/rego/metadata.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package rego
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"strings"
78

@@ -330,15 +331,15 @@ func (m *MetadataRetriever) RetrieveMetadata(ctx context.Context, module *ast.Mo
330331
}
331332

332333
if len(set) != 1 {
333-
return nil, fmt.Errorf("failed to parse metadata: unexpected set length")
334+
return nil, errors.New("failed to parse metadata: unexpected set length")
334335
}
335336
if len(set[0].Expressions) != 1 {
336-
return nil, fmt.Errorf("failed to parse metadata: unexpected expression length")
337+
return nil, errors.New("failed to parse metadata: unexpected expression length")
337338
}
338339
expression := set[0].Expressions[0]
339340
meta, ok := expression.Value.(map[string]any)
340341
if !ok {
341-
return nil, fmt.Errorf("failed to parse metadata: not an object")
342+
return nil, errors.New("failed to parse metadata: not an object")
342343
}
343344

344345
if err := metadata.update(meta); err != nil {

pkg/iac/rego/schemas/builder.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package schemas
22

33
import (
4+
"errors"
45
"fmt"
56
"reflect"
67
"strings"
@@ -56,7 +57,7 @@ func (b *builder) fromInput(inputValue reflect.Value) error {
5657
return err
5758
}
5859
if prop == nil {
59-
return fmt.Errorf("property is nil")
60+
return errors.New("property is nil")
6061
}
6162
b.schema.Properties = prop.Properties
6263
b.schema.Type = prop.Type

pkg/iac/scan/code.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scan
22

33
import (
44
"bufio"
5+
"errors"
56
"fmt"
67
"io/fs"
78
"path/filepath"
@@ -141,7 +142,7 @@ func (r *Result) GetCode(opts ...CodeOption) (*Code, error) {
141142

142143
fsys := r.Metadata().Range().GetFS()
143144
if fsys == nil {
144-
return nil, fmt.Errorf("code unavailable: result was not mapped to a known filesystem")
145+
return nil, errors.New("code unavailable: result was not mapped to a known filesystem")
145146
}
146147

147148
innerRange := r.metadata.Range()

pkg/iac/scanners/azure/arm/parser/armjson/decode.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package armjson
22

33
import (
4+
"errors"
45
"fmt"
56
"reflect"
67

@@ -40,7 +41,7 @@ func (n *node) decodeToValue(v reflect.Value) error {
4041
}
4142

4243
if !v.CanSet() {
43-
return fmt.Errorf("target is not settable")
44+
return errors.New("target is not settable")
4445
}
4546

4647
switch n.kind {
@@ -59,7 +60,7 @@ func (n *node) decodeToValue(v reflect.Value) error {
5960
case KindComment:
6061
return n.decodeString(v)
6162
case KindUnknown:
62-
return fmt.Errorf("cannot decode unknown kind")
63+
return errors.New("cannot decode unknown kind")
6364
default:
6465
return fmt.Errorf("decoding of kind 0x%x is not supported", n.kind)
6566
}

pkg/iac/scanners/azure/arm/parser/armjson/decode_array.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package armjson
22

33
import (
4-
"fmt"
4+
"errors"
55
"reflect"
66
)
77

@@ -14,7 +14,7 @@ func (n *node) decodeArray(v reflect.Value) error {
1414
switch v.Kind() {
1515
case reflect.Array:
1616
if v.Len() != length {
17-
return fmt.Errorf("invalid length")
17+
return errors.New("invalid length")
1818
}
1919
case reflect.Slice:
2020
v.Set(reflect.MakeSlice(v.Type(), length, length))
@@ -24,7 +24,7 @@ func (n *node) decodeArray(v reflect.Value) error {
2424
v = reflect.New(slice.Type()).Elem()
2525
v.Set(slice)
2626
default:
27-
return fmt.Errorf("invalid target type")
27+
return errors.New("invalid target type")
2828
}
2929

3030
elementType := v.Type().Elem()

pkg/iac/scanners/azure/arm/parser/armjson/decode_number.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package armjson
22

33
import (
4+
"errors"
45
"fmt"
56
"reflect"
67
)
@@ -42,5 +43,5 @@ func (n *node) decodeNumber(v reflect.Value) error {
4243
return fmt.Errorf("cannot decode number value to %s target", v.Kind())
4344
}
4445

45-
return fmt.Errorf("internal value is not numeric")
46+
return errors.New("internal value is not numeric")
4647
}

pkg/iac/scanners/azure/arm/parser/armjson/decode_object.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package armjson
22

33
import (
4+
"errors"
45
"fmt"
56
"reflect"
67
"strings"
@@ -54,19 +55,19 @@ func (n *node) decodeObjectToMap(v reflect.Value) error {
5455

5556
func (n *node) objectAsMap() (map[string]Node, error) {
5657
if n.kind != KindObject {
57-
return nil, fmt.Errorf("not an object")
58+
return nil, errors.New("not an object")
5859
}
5960
properties := make(map[string]Node)
6061
contents := n.content
6162
for i := 0; i < len(contents); i += 2 {
6263
key := contents[i]
6364
if key.Kind() != KindString {
64-
return nil, fmt.Errorf("invalid object key - please report this bug")
65+
return nil, errors.New("invalid object key - please report this bug")
6566
}
6667
keyStr := key.(*node).raw.(string)
6768

6869
if i+1 >= len(contents) {
69-
return nil, fmt.Errorf("missing object value - please report this bug")
70+
return nil, errors.New("missing object value - please report this bug")
7071
}
7172
properties[keyStr] = contents[i+1]
7273
}

pkg/iac/scanners/azure/arm/parser/armjson/parse_boolean.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package armjson
22

33
import (
4-
"fmt"
4+
"errors"
55

66
"github.com/aquasecurity/trivy/pkg/iac/types"
77
)
@@ -21,7 +21,7 @@ func (p *parser) parseBoolean(parentMetadata *types.Metadata) (Node, error) {
2121
if r == 't' {
2222
for _, expected := range trueRunes {
2323
if !p.swallowIfEqual(expected) {
24-
return nil, fmt.Errorf("unexpected character in boolean value")
24+
return nil, errors.New("unexpected character in boolean value")
2525
}
2626
}
2727
n.raw = true
@@ -31,7 +31,7 @@ func (p *parser) parseBoolean(parentMetadata *types.Metadata) (Node, error) {
3131

3232
for _, expected := range falseRunes {
3333
if !p.swallowIfEqual(expected) {
34-
return nil, fmt.Errorf("unexpected character in boolean value")
34+
return nil, errors.New("unexpected character in boolean value")
3535
}
3636
}
3737
n.raw = false

pkg/iac/scanners/azure/arm/parser/armjson/parse_null.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package armjson
22

33
import (
4-
"fmt"
4+
"errors"
55

66
"github.com/aquasecurity/trivy/pkg/iac/types"
77
)
@@ -14,7 +14,7 @@ func (p *parser) parseNull(parentMetadata *types.Metadata) (Node, error) {
1414

1515
for _, expected := range nullRunes {
1616
if !p.swallowIfEqual(expected) {
17-
return nil, fmt.Errorf("unexpected character")
17+
return nil, errors.New("unexpected character")
1818
}
1919
}
2020
n.raw = nil

pkg/iac/scanners/azure/expressions/lex.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package expressions
22

33
import (
44
"bufio"
5+
"errors"
56
"fmt"
67
"strconv"
78
"strings"
@@ -119,7 +120,7 @@ func (l *lexer) lexString(terminator rune) (Token, error) {
119120
func (l *lexer) readEscapedChar() (rune, error) {
120121
r, err := l.read()
121122
if err != nil {
122-
return 0, fmt.Errorf("unexpected EOF")
123+
return 0, errors.New("unexpected EOF")
123124
}
124125
switch r {
125126
case 'n':

pkg/iac/scanners/azure/functions/date_time_add.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package functions
22

33
import (
4+
"errors"
45
"fmt"
56
"regexp"
67
"strconv"
@@ -65,7 +66,7 @@ func parseISO8601(from string) (Iso8601Duration, error) {
6566
if pattern.MatchString(from) {
6667
match = pattern.FindStringSubmatch(from)
6768
} else {
68-
return d, fmt.Errorf("could not parse duration string")
69+
return d, errors.New("could not parse duration string")
6970
}
7071

7172
for i, name := range pattern.SubexpNames() {

0 commit comments

Comments
 (0)