Skip to content

Commit

Permalink
fix: enable err-error and errorf rules from perfsprint linter (#7859)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
  • Loading branch information
mmorel-35 authored Dec 10, 2024
1 parent e8b31bf commit 156a2aa
Show file tree
Hide file tree
Showing 37 changed files with 102 additions and 69 deletions.
15 changes: 15 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ linters-settings:
- licence
- optimise
- simmilar
perfsprint:
# Optimizes even if it requires an int or uint type cast.
int-conversion: false
# Optimizes into `err.Error()` even if it is only equivalent for non-nil errors.
err-error: true
# Optimizes `fmt.Errorf`.
errorf: true
# Optimizes `fmt.Sprintf` with only one argument.
sprintf1: false
# Optimizes into strings concatenation.
strconcat: false
revive:
ignore-generated-header: true
testifylint:
Expand All @@ -100,6 +111,7 @@ linters:
- govet
- ineffassign
- misspell
- perfsprint
- revive
- tenv
- testifylint
Expand Down Expand Up @@ -140,5 +152,8 @@ issues:
linters:
- gocritic
text: "importShadow:"
- linters:
- perfsprint
text: "fmt.Sprint"
exclude-use-default: false
max-same-issues: 0
3 changes: 2 additions & 1 deletion examples/module/spring4shell/spring4shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"bufio"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -112,7 +113,7 @@ func (Spring4Shell) parseTomcatReleaseNotes(f *os.File, filePath string) (*seria

m := tomcatVersionRegex.FindStringSubmatch(string(b))
if len(m) != 2 {
return nil, fmt.Errorf("unknown tomcat release notes format")
return nil, errors.New("unknown tomcat release notes format")
}

return &serialize.AnalysisResult{
Expand Down
4 changes: 2 additions & 2 deletions magefiles/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"errors"
"log"
"os"

Expand Down Expand Up @@ -66,7 +66,7 @@ func VerifySchema() error {
return err
}
if !bytes.Equal(data, existing) {
return fmt.Errorf("schema is out of date:\n\nplease run 'mage schema:generate' and commit the changes\n")
return errors.New("schema is out of date:\n\nplease run 'mage schema:generate' and commit the changes\n")
}
return nil
}
11 changes: 6 additions & 5 deletions pkg/flag/kubernetes_flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flag

import (
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -192,10 +193,10 @@ func (f *K8sFlagGroup) ToOptions() (K8sOptions, error) {
exludeNodeLabels[excludeNodeParts[0]] = excludeNodeParts[1]
}
if len(f.ExcludeNamespaces.Value()) > 0 && len(f.IncludeNamespaces.Value()) > 0 {
return K8sOptions{}, fmt.Errorf("include-namespaces and exclude-namespaces flags cannot be used together")
return K8sOptions{}, errors.New("include-namespaces and exclude-namespaces flags cannot be used together")
}
if len(f.ExcludeKinds.Value()) > 0 && len(f.IncludeKinds.Value()) > 0 {
return K8sOptions{}, fmt.Errorf("include-kinds and exclude-kinds flags cannot be used together")
return K8sOptions{}, errors.New("include-kinds and exclude-kinds flags cannot be used together")
}

return K8sOptions{
Expand All @@ -222,12 +223,12 @@ func optionToTolerations(tolerationsOptions []string) ([]corev1.Toleration, erro
for _, toleration := range tolerationsOptions {
tolerationParts := strings.Split(toleration, ":")
if len(tolerationParts) < 2 {
return []corev1.Toleration{}, fmt.Errorf("toleration must include key and effect")
return []corev1.Toleration{}, errors.New("toleration must include key and effect")
}
if corev1.TaintEffect(tolerationParts[1]) != corev1.TaintEffectNoSchedule &&
corev1.TaintEffect(tolerationParts[1]) != corev1.TaintEffectPreferNoSchedule &&
corev1.TaintEffect(tolerationParts[1]) != corev1.TaintEffectNoExecute {
return []corev1.Toleration{}, fmt.Errorf("toleration effect must be a valid value")
return []corev1.Toleration{}, errors.New("toleration effect must be a valid value")
}
keyValue := strings.Split(tolerationParts[0], "=")
operator := corev1.TolerationOpEqual
Expand All @@ -245,7 +246,7 @@ func optionToTolerations(tolerationsOptions []string) ([]corev1.Toleration, erro
if len(tolerationParts) == 3 {
tolerationSec, err = strconv.Atoi(tolerationParts[2])
if err != nil {
return nil, fmt.Errorf("TolerationSeconds must must be a number")
return nil, errors.New("TolerationSeconds must must be a number")
}
toleration.TolerationSeconds = lo.ToPtr(int64(tolerationSec))
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/iac/adapters/cloudformation/aws/ecr/repository.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ecr

import (
"fmt"
"errors"

"github.com/liamg/iamgo"

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

parsed, err := iamgo.Parse(policyProp.GetJsonBytes())
Expand Down
4 changes: 2 additions & 2 deletions pkg/iac/adapters/cloudformation/aws/sqs/queue.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sqs

import (
"fmt"
"errors"

"github.com/liamg/iamgo"

Expand Down Expand Up @@ -59,5 +59,5 @@ func getPolicy(id string, ctx parser.FileContext) (*iam.Policy, error) {
}
}
}
return nil, fmt.Errorf("no matching policy found")
return nil, errors.New("no matching policy found")
}
7 changes: 4 additions & 3 deletions pkg/iac/rego/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rego

import (
"context"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -329,15 +330,15 @@ func (m *MetadataRetriever) RetrieveMetadata(ctx context.Context, module *ast.Mo
}

if len(set) != 1 {
return nil, fmt.Errorf("failed to parse metadata: unexpected set length")
return nil, errors.New("failed to parse metadata: unexpected set length")
}
if len(set[0].Expressions) != 1 {
return nil, fmt.Errorf("failed to parse metadata: unexpected expression length")
return nil, errors.New("failed to parse metadata: unexpected expression length")
}
expression := set[0].Expressions[0]
meta, ok := expression.Value.(map[string]any)
if !ok {
return nil, fmt.Errorf("failed to parse metadata: not an object")
return nil, errors.New("failed to parse metadata: not an object")
}

if err := metadata.update(meta); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/iac/rego/schemas/builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package schemas

import (
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -56,7 +57,7 @@ func (b *builder) fromInput(inputValue reflect.Value) error {
return err
}
if prop == nil {
return fmt.Errorf("property is nil")
return errors.New("property is nil")
}
b.schema.Properties = prop.Properties
b.schema.Type = prop.Type
Expand Down
3 changes: 2 additions & 1 deletion pkg/iac/scan/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scan

import (
"bufio"
"errors"
"fmt"
"io/fs"
"path/filepath"
Expand Down Expand Up @@ -141,7 +142,7 @@ func (r *Result) GetCode(opts ...CodeOption) (*Code, error) {

fsys := r.Metadata().Range().GetFS()
if fsys == nil {
return nil, fmt.Errorf("code unavailable: result was not mapped to a known filesystem")
return nil, errors.New("code unavailable: result was not mapped to a known filesystem")
}

innerRange := r.metadata.Range()
Expand Down
5 changes: 3 additions & 2 deletions pkg/iac/scanners/azure/arm/parser/armjson/decode.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package armjson

import (
"errors"
"fmt"
"reflect"

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

if !v.CanSet() {
return fmt.Errorf("target is not settable")
return errors.New("target is not settable")
}

switch n.kind {
Expand All @@ -59,7 +60,7 @@ func (n *node) decodeToValue(v reflect.Value) error {
case KindComment:
return n.decodeString(v)
case KindUnknown:
return fmt.Errorf("cannot decode unknown kind")
return errors.New("cannot decode unknown kind")
default:
return fmt.Errorf("decoding of kind 0x%x is not supported", n.kind)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/iac/scanners/azure/arm/parser/armjson/decode_array.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package armjson

import (
"fmt"
"errors"
"reflect"
)

Expand All @@ -14,7 +14,7 @@ func (n *node) decodeArray(v reflect.Value) error {
switch v.Kind() {
case reflect.Array:
if v.Len() != length {
return fmt.Errorf("invalid length")
return errors.New("invalid length")
}
case reflect.Slice:
v.Set(reflect.MakeSlice(v.Type(), length, length))
Expand All @@ -24,7 +24,7 @@ func (n *node) decodeArray(v reflect.Value) error {
v = reflect.New(slice.Type()).Elem()
v.Set(slice)
default:
return fmt.Errorf("invalid target type")
return errors.New("invalid target type")
}

elementType := v.Type().Elem()
Expand Down
3 changes: 2 additions & 1 deletion pkg/iac/scanners/azure/arm/parser/armjson/decode_number.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package armjson

import (
"errors"
"fmt"
"reflect"
)
Expand Down Expand Up @@ -42,5 +43,5 @@ func (n *node) decodeNumber(v reflect.Value) error {
return fmt.Errorf("cannot decode number value to %s target", v.Kind())
}

return fmt.Errorf("internal value is not numeric")
return errors.New("internal value is not numeric")
}
7 changes: 4 additions & 3 deletions pkg/iac/scanners/azure/arm/parser/armjson/decode_object.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package armjson

import (
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -54,19 +55,19 @@ func (n *node) decodeObjectToMap(v reflect.Value) error {

func (n *node) objectAsMap() (map[string]Node, error) {
if n.kind != KindObject {
return nil, fmt.Errorf("not an object")
return nil, errors.New("not an object")
}
properties := make(map[string]Node)
contents := n.content
for i := 0; i < len(contents); i += 2 {
key := contents[i]
if key.Kind() != KindString {
return nil, fmt.Errorf("invalid object key - please report this bug")
return nil, errors.New("invalid object key - please report this bug")
}
keyStr := key.(*node).raw.(string)

if i+1 >= len(contents) {
return nil, fmt.Errorf("missing object value - please report this bug")
return nil, errors.New("missing object value - please report this bug")
}
properties[keyStr] = contents[i+1]
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/iac/scanners/azure/arm/parser/armjson/parse_boolean.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package armjson

import (
"fmt"
"errors"

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

for _, expected := range falseRunes {
if !p.swallowIfEqual(expected) {
return nil, fmt.Errorf("unexpected character in boolean value")
return nil, errors.New("unexpected character in boolean value")
}
}
n.raw = false
Expand Down
4 changes: 2 additions & 2 deletions pkg/iac/scanners/azure/arm/parser/armjson/parse_null.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package armjson

import (
"fmt"
"errors"

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

for _, expected := range nullRunes {
if !p.swallowIfEqual(expected) {
return nil, fmt.Errorf("unexpected character")
return nil, errors.New("unexpected character")
}
}
n.raw = nil
Expand Down
3 changes: 2 additions & 1 deletion pkg/iac/scanners/azure/expressions/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package expressions

import (
"bufio"
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -119,7 +120,7 @@ func (l *lexer) lexString(terminator rune) (Token, error) {
func (l *lexer) readEscapedChar() (rune, error) {
r, err := l.read()
if err != nil {
return 0, fmt.Errorf("unexpected EOF")
return 0, errors.New("unexpected EOF")
}
switch r {
case 'n':
Expand Down
3 changes: 2 additions & 1 deletion pkg/iac/scanners/azure/functions/date_time_add.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package functions

import (
"errors"
"fmt"
"regexp"
"strconv"
Expand Down Expand Up @@ -65,7 +66,7 @@ func parseISO8601(from string) (Iso8601Duration, error) {
if pattern.MatchString(from) {
match = pattern.FindStringSubmatch(from)
} else {
return d, fmt.Errorf("could not parse duration string")
return d, errors.New("could not parse duration string")
}

for i, name := range pattern.SubexpNames() {
Expand Down
Loading

0 comments on commit 156a2aa

Please sign in to comment.