Skip to content

Commit 82aa2fa

Browse files
authored
Merge pull request #2951 from alexandear/refactor/errors-new
refactor: replace fmt.Errorf with errors.New
2 parents 122a340 + 63cc564 commit 82aa2fa

File tree

13 files changed

+42
-27
lines changed

13 files changed

+42
-27
lines changed

.golangci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ linters:
6868
# - nlreturn
6969
- noctx
7070
- nolintlint
71+
- perfsprint
7172
# - rowserrcheck
7273
# - scopelint
7374
# - sqlclosecheck
@@ -122,6 +123,12 @@ linters-settings:
122123
minThreshold: 3
123124
errorlint:
124125
asserts: false
126+
perfsprint:
127+
int-conversion: false
128+
err-error: false
129+
errorf: true
130+
sprintf1: false
131+
strconcat: false
125132
revive:
126133
# Set below 0.8 to enable error-strings
127134
confidence: 0.6
@@ -179,3 +186,7 @@ issues:
179186
- text: "exported: comment on exported const"
180187
linters:
181188
- revive
189+
# Disable perfsprint for fmt.Sprint until https://github.com/catenacyber/perfsprint/issues/39 gets fixed.
190+
- text: "fmt.Sprint.* can be replaced with faster"
191+
linters:
192+
- perfsprint

cmd/limactl/copy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
9090
}
9191
}
9292
if legacySSH && len(instances) > 1 {
93-
return fmt.Errorf("more than one (instance) host is involved in this command, this is only supported for openSSH v8.0 or higher")
93+
return errors.New("more than one (instance) host is involved in this command, this is only supported for openSSH v8.0 or higher")
9494
}
9595
scpFlags = append(scpFlags, "-3", "--")
9696
scpArgs = append(scpFlags, scpArgs...)

cmd/limactl/genschema.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"os"
78

@@ -71,7 +72,7 @@ func genschemaAction(cmd *cobra.Command, args []string) error {
7172
}
7273

7374
if file == "" {
74-
return fmt.Errorf("need --schemafile to validate")
75+
return errors.New("need --schemafile to validate")
7576
}
7677
err = os.WriteFile(file, j, 0o644)
7778
if err != nil {

cmd/limactl/hostagent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func hostagentAction(cmd *cobra.Command, args []string) error {
5252
return err
5353
}
5454
if socket == "" {
55-
return fmt.Errorf("socket must be specified (limactl version mismatch?)")
55+
return errors.New("socket must be specified (limactl version mismatch?)")
5656
}
5757

5858
instName := args[0]

cmd/limactl/snapshot.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

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

@@ -56,7 +57,7 @@ func snapshotCreateAction(cmd *cobra.Command, args []string) error {
5657
}
5758

5859
if tag == "" {
59-
return fmt.Errorf("expected tag")
60+
return errors.New("expected tag")
6061
}
6162

6263
ctx := cmd.Context()
@@ -91,7 +92,7 @@ func snapshotDeleteAction(cmd *cobra.Command, args []string) error {
9192
}
9293

9394
if tag == "" {
94-
return fmt.Errorf("expected tag")
95+
return errors.New("expected tag")
9596
}
9697

9798
ctx := cmd.Context()
@@ -126,7 +127,7 @@ func snapshotApplyAction(cmd *cobra.Command, args []string) error {
126127
}
127128

128129
if tag == "" {
129-
return fmt.Errorf("expected tag")
130+
return errors.New("expected tag")
130131
}
131132

132133
ctx := cmd.Context()

cmd/limactl/sudoers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func sudoersAction(cmd *cobra.Command, args []string) error {
6262
case 0:
6363
// NOP
6464
case 1:
65-
return fmt.Errorf("the file argument can be specified only for --check mode")
65+
return errors.New("the file argument can be specified only for --check mode")
6666
default:
6767
return fmt.Errorf("unexpected arguments %v", args)
6868
}

pkg/downloader/downloader.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
181181
var localPath string
182182
if local == "" {
183183
if o.cacheDir == "" {
184-
return nil, fmt.Errorf("caching-only mode requires the cache directory to be specified")
184+
return nil, errors.New("caching-only mode requires the cache directory to be specified")
185185
}
186186
} else {
187187
var err error
@@ -347,10 +347,10 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
347347
return nil, err
348348
}
349349
if o.cacheDir == "" {
350-
return nil, fmt.Errorf("caching-only mode requires the cache directory to be specified")
350+
return nil, errors.New("caching-only mode requires the cache directory to be specified")
351351
}
352352
if IsLocal(remote) {
353-
return nil, fmt.Errorf("local files are not cached")
353+
return nil, errors.New("local files are not cached")
354354
}
355355

356356
shad := cacheDirectoryPath(o.cacheDir, remote)
@@ -431,7 +431,7 @@ func IsLocal(s string) bool {
431431
// - Expand a leading `~`, or convert relative to absolute name
432432
func canonicalLocalPath(s string) (string, error) {
433433
if s == "" {
434-
return "", fmt.Errorf("got empty path")
434+
return "", errors.New("got empty path")
435435
}
436436
if !IsLocal(s) {
437437
return "", fmt.Errorf("got non-local path: %q", s)
@@ -588,7 +588,7 @@ func validateCachedDigest(shadDigest string, expectedDigest digest.Digest) error
588588

589589
func validateLocalFileDigest(localPath string, expectedDigest digest.Digest) error {
590590
if localPath == "" {
591-
return fmt.Errorf("validateLocalFileDigest: got empty localPath")
591+
return errors.New("validateLocalFileDigest: got empty localPath")
592592
}
593593
if expectedDigest == "" {
594594
return nil
@@ -651,7 +651,7 @@ func matchLastModified(ctx context.Context, lastModifiedPath, url string) (match
651651

652652
func downloadHTTP(ctx context.Context, localPath, lastModified, contentType, url, description string, expectedDigest digest.Digest) error {
653653
if localPath == "" {
654-
return fmt.Errorf("downloadHTTP: got empty localPath")
654+
return errors.New("downloadHTTP: got empty localPath")
655655
}
656656
logrus.Debugf("downloading %q into %q", url, localPath)
657657

pkg/driver/driver.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package driver
22

33
import (
44
"context"
5-
"fmt"
5+
"errors"
66
"net"
77

88
"github.com/lima-vm/lima/pkg/store"
@@ -125,19 +125,19 @@ func (d *BaseDriver) GetDisplayConnection(_ context.Context) (string, error) {
125125
}
126126

127127
func (d *BaseDriver) CreateSnapshot(_ context.Context, _ string) error {
128-
return fmt.Errorf("unimplemented")
128+
return errors.New("unimplemented")
129129
}
130130

131131
func (d *BaseDriver) ApplySnapshot(_ context.Context, _ string) error {
132-
return fmt.Errorf("unimplemented")
132+
return errors.New("unimplemented")
133133
}
134134

135135
func (d *BaseDriver) DeleteSnapshot(_ context.Context, _ string) error {
136-
return fmt.Errorf("unimplemented")
136+
return errors.New("unimplemented")
137137
}
138138

139139
func (d *BaseDriver) ListSnapshots(_ context.Context) (string, error) {
140-
return "", fmt.Errorf("unimplemented")
140+
return "", errors.New("unimplemented")
141141
}
142142

143143
func (d *BaseDriver) ForwardGuestAgent() bool {

pkg/guestagent/procnettcp/procnettcp.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package procnettcp
33
import (
44
"bufio"
55
"encoding/hex"
6+
"errors"
67
"fmt"
78
"io"
89
"net"
@@ -59,10 +60,10 @@ func Parse(r io.Reader, kind Kind) ([]Entry, error) {
5960
fieldNames[fields[j]] = j
6061
}
6162
if _, ok := fieldNames["local_address"]; !ok {
62-
return nil, fmt.Errorf("field \"local_address\" not found")
63+
return nil, errors.New("field \"local_address\" not found")
6364
}
6465
if _, ok := fieldNames["st"]; !ok {
65-
return nil, fmt.Errorf("field \"st\" not found")
66+
return nil, errors.New("field \"st\" not found")
6667
}
6768

6869
default:

pkg/instance/delete.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package instance
22

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

@@ -12,7 +13,7 @@ import (
1213

1314
func Delete(ctx context.Context, inst *store.Instance, force bool) error {
1415
if inst.Protected {
15-
return fmt.Errorf("instance is protected to prohibit accidental removal (Hint: use `limactl unprotect`)")
16+
return errors.New("instance is protected to prohibit accidental removal (Hint: use `limactl unprotect`)")
1617
}
1718
if !force && inst.Status != store.StatusStopped {
1819
return fmt.Errorf("expected status %q, got %q", store.StatusStopped, inst.Status)

0 commit comments

Comments
 (0)