Skip to content

Commit 363cdbb

Browse files
authored
lint: add support for errcheck (#2918)
* lint: add support for errcheck * Fix * Fix
1 parent ff87aee commit 363cdbb

File tree

54 files changed

+150
-146
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+150
-146
lines changed

.golangci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ linters:
1818
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: false, auto-fix: false]
1919
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted. [fast: false, auto-fix: false]
2020
- errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`. [fast: false, auto-fix: false]
21+
- errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13. [fast: false, auto-fix: false]
2122
- exptostd # Detects functions from golang.org/x/exp/ that can be replaced by std functions. [auto-fix]
23+
- fatcontext # Detects nested contexts in loops and function literals. [auto-fix]
2224
- forbidigo # Forbids identifiers [fast: true, auto-fix: false]
2325
- gci # Gci controls golang package import order and makes it always deterministic. [fast: true, auto-fix: false]
2426
- gocheckcompilerdirectives # Checks that go compiler directive comments (//go:) are valid. [fast: true, auto-fix: false]
@@ -38,6 +40,7 @@ linters:
3840
- gosmopolitan # Report certain i18n/l10n anti-patterns in your Go codebase [fast: false, auto-fix: false]
3941
- govet #(vet, vetshadow): Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: false, auto-fix: false]
4042
- grouper # An analyzer to analyze expression groups. [fast: true, auto-fix: false]
43+
- iface # Detect the incorrect use of interfaces, helping developers avoid interface pollution. [auto-fix]
4144
- importas # Enforces consistent import aliases [fast: false, auto-fix: false]
4245
- ineffassign # Detects when assignments to existing variables are not used [fast: true, auto-fix: false]
4346
- intrange # intrange is a linter to find places where for loops could make use of an integer range. [fast: true, auto-fix: false]
@@ -86,7 +89,6 @@ linters:
8689
- depguard # Go linter that checks if package imports are in a list of acceptable packages [fast: true, auto-fix: false]
8790
- dupl # Tool for code clone detection [fast: true, auto-fix: false]
8891
- err113 # Golang linter to check the errors handling expressions [fast: false, auto-fix: false]
89-
- errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13. [fast: false, auto-fix: false]
9092
- exhaustive # check exhaustiveness of enum switch statements [fast: false, auto-fix: false]
9193
- forcetypeassert # finds forced type assertions [fast: true, auto-fix: false]
9294
- funlen # Tool for detection of long functions [fast: true, auto-fix: false]

internal/acctest/checks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func CheckResourceAttrFunc(name string, key string, test func(string) error) res
9797

9898
err := test(value)
9999
if err != nil {
100-
return fmt.Errorf("test for %s %s did not pass test: %s", name, key, err)
100+
return fmt.Errorf("test for %s %s did not pass test: %w", name, key, err)
101101
}
102102

103103
return nil

internal/datasource/search.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (e *TooManyResultsError) Error() string {
6969
}
7070

7171
func (e *TooManyResultsError) Is(err error) bool {
72-
_, ok := err.(*TooManyResultsError) //nolint:errorlint // Explicitly does *not* match down the error tree
72+
_, ok := err.(*TooManyResultsError)
7373

7474
return ok
7575
}

internal/meta/meta.go

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

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net/http"
78
"os"
@@ -162,7 +163,8 @@ func customizeUserAgent(providerVersion string, terraformVersion string) string
162163
func loadProfile(ctx context.Context, d *schema.ResourceData) (*scw.Profile, *CredentialsSource, error) {
163164
config, err := scw.LoadConfig()
164165
// If the config file do not exist, don't return an error as we may find config in ENV or flags.
165-
if _, isNotFoundError := err.(*scw.ConfigFileNotFoundError); isNotFoundError {
166+
var configFileNotFoundError *scw.ConfigFileNotFoundError
167+
if errors.As(err, &configFileNotFoundError) {
166168
config = &scw.Config{}
167169
} else if err != nil {
168170
return nil, nil, err

internal/services/applesilicon/testfuncs/sweep.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func testSweepAppleSiliconServer(_ string) error {
2525

2626
listServers, err := asAPI.ListServers(&applesiliconSDK.ListServersRequest{Zone: zone}, scw.WithAllPages())
2727
if err != nil {
28-
return fmt.Errorf("error listing apple silicon servers in (%s) in sweeper: %s", zone, err)
28+
return fmt.Errorf("error listing apple silicon servers in (%s) in sweeper: %w", zone, err)
2929
}
3030

3131
for _, server := range listServers.Servers {
@@ -34,7 +34,7 @@ func testSweepAppleSiliconServer(_ string) error {
3434
Zone: zone,
3535
})
3636
if errDelete != nil {
37-
return fmt.Errorf("error deleting apple silicon server in sweeper: %s", err)
37+
return fmt.Errorf("error deleting apple silicon server in sweeper: %w", err)
3838
}
3939
}
4040

internal/services/baremetal/testfuncs/sweep.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func testSweepServer(_ string) error {
3636
ServerID: server.ID,
3737
})
3838
if err != nil {
39-
return fmt.Errorf("error deleting server in sweeper: %s", err)
39+
return fmt.Errorf("error deleting server in sweeper: %w", err)
4040
}
4141
}
4242

internal/services/block/testfuncs/sweep.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func testSweepBlockVolume(_ string) error {
3232
Zone: zone,
3333
}, scw.WithAllPages())
3434
if err != nil {
35-
return fmt.Errorf("error listing volume in (%s) in sweeper: %s", zone, err)
35+
return fmt.Errorf("error listing volume in (%s) in sweeper: %w", zone, err)
3636
}
3737

3838
for _, volume := range listVolumes.Volumes {
@@ -43,7 +43,7 @@ func testSweepBlockVolume(_ string) error {
4343
if err != nil {
4444
logging.L.Debugf("sweeper: error (%s)", err)
4545

46-
return fmt.Errorf("error deleting volume in sweeper: %s", err)
46+
return fmt.Errorf("error deleting volume in sweeper: %w", err)
4747
}
4848
}
4949

@@ -62,7 +62,7 @@ func testSweepSnapshot(_ string) error {
6262
Zone: zone,
6363
}, scw.WithAllPages())
6464
if err != nil {
65-
return fmt.Errorf("error listing snapshot in (%s) in sweeper: %s", zone, err)
65+
return fmt.Errorf("error listing snapshot in (%s) in sweeper: %w", zone, err)
6666
}
6767

6868
for _, snapshot := range listSnapshots.Snapshots {
@@ -73,7 +73,7 @@ func testSweepSnapshot(_ string) error {
7373
if err != nil {
7474
logging.L.Debugf("sweeper: error (%s)", err)
7575

76-
return fmt.Errorf("error deleting snapshot in sweeper: %s", err)
76+
return fmt.Errorf("error deleting snapshot in sweeper: %w", err)
7777
}
7878
}
7979

internal/services/container/testfuncs/checks.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestConfigContainerNamespace(tt *acctest.TestTools, n string) resource.Test
4444
Region: region,
4545
})
4646
if err != nil {
47-
return fmt.Errorf("error waiting namespace: %v", err)
47+
return fmt.Errorf("error waiting namespace: %w", err)
4848
}
4949

5050
meta := tt.Meta
@@ -61,20 +61,20 @@ func TestConfigContainerNamespace(tt *acctest.TestTools, n string) resource.Test
6161

6262
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
6363
if err != nil {
64-
return fmt.Errorf("could not connect to Docker: %v", err)
64+
return fmt.Errorf("could not connect to Docker: %w", err)
6565
}
6666

6767
encodedJSON, err := json.Marshal(authConfig)
6868
if err != nil {
69-
return fmt.Errorf("could not marshal auth config: %v", err)
69+
return fmt.Errorf("could not marshal auth config: %w", err)
7070
}
7171

7272
ctx := context.Background()
7373
authStr := base64.URLEncoding.EncodeToString(encodedJSON)
7474

7575
out, err := cli.ImagePull(ctx, testDockerIMG, image.PullOptions{})
7676
if err != nil {
77-
return fmt.Errorf("could not pull image: %v", err)
77+
return fmt.Errorf("could not pull image: %w", err)
7878
}
7979

8080
defer out.Close()
@@ -89,7 +89,7 @@ func TestConfigContainerNamespace(tt *acctest.TestTools, n string) resource.Test
8989

9090
err = json.Unmarshal(streamBytes, &errorMessage)
9191
if err != nil {
92-
return fmt.Errorf("could not unmarshal: %v", err)
92+
return fmt.Errorf("could not unmarshal: %w", err)
9393
}
9494

9595
if errorMessage.Error != "" {
@@ -102,12 +102,12 @@ func TestConfigContainerNamespace(tt *acctest.TestTools, n string) resource.Test
102102

103103
err = cli.ImageTag(ctx, imageTag, scwTag)
104104
if err != nil {
105-
return fmt.Errorf("could not tag image: %v", err)
105+
return fmt.Errorf("could not tag image: %w", err)
106106
}
107107

108108
pusher, err := cli.ImagePush(ctx, scwTag, image.PushOptions{RegistryAuth: authStr})
109109
if err != nil {
110-
return fmt.Errorf("could not push image: %v", err)
110+
return fmt.Errorf("could not push image: %w", err)
111111
}
112112

113113
defer pusher.Close()
@@ -122,7 +122,7 @@ func TestConfigContainerNamespace(tt *acctest.TestTools, n string) resource.Test
122122

123123
err = json.Unmarshal(streamBytes, &errorMessage)
124124
if err != nil {
125-
return fmt.Errorf("could not unmarshal: %v", err)
125+
return fmt.Errorf("could not unmarshal: %w", err)
126126
}
127127

128128
if errorMessage.Error != "" {
@@ -135,7 +135,7 @@ func TestConfigContainerNamespace(tt *acctest.TestTools, n string) resource.Test
135135
Region: region,
136136
})
137137
if err != nil {
138-
return fmt.Errorf("error waiting namespace: %v", err)
138+
return fmt.Errorf("error waiting namespace: %w", err)
139139
}
140140

141141
return nil

internal/services/container/testfuncs/sweep.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func testSweepTrigger(_ string) error {
3737
Region: region,
3838
}, scw.WithAllPages())
3939
if err != nil {
40-
return fmt.Errorf("error listing trigger in (%s) in sweeper: %s", region, err)
40+
return fmt.Errorf("error listing trigger in (%s) in sweeper: %w", region, err)
4141
}
4242

4343
for _, trigger := range listTriggers.Triggers {
@@ -48,7 +48,7 @@ func testSweepTrigger(_ string) error {
4848
if err != nil {
4949
logging.L.Debugf("sweeper: error (%s)", err)
5050

51-
return fmt.Errorf("error deleting trigger in sweeper: %s", err)
51+
return fmt.Errorf("error deleting trigger in sweeper: %w", err)
5252
}
5353
}
5454

@@ -67,7 +67,7 @@ func testSweepContainer(_ string) error {
6767
Region: region,
6868
}, scw.WithAllPages())
6969
if err != nil {
70-
return fmt.Errorf("error listing containers in (%s) in sweeper: %s", region, err)
70+
return fmt.Errorf("error listing containers in (%s) in sweeper: %w", region, err)
7171
}
7272

7373
for _, cont := range listNamespaces.Containers {
@@ -78,7 +78,7 @@ func testSweepContainer(_ string) error {
7878
if err != nil {
7979
logging.L.Debugf("sweeper: error (%s)", err)
8080

81-
return fmt.Errorf("error deleting container in sweeper: %s", err)
81+
return fmt.Errorf("error deleting container in sweeper: %w", err)
8282
}
8383
}
8484

@@ -97,7 +97,7 @@ func testSweepNamespace(_ string) error {
9797
Region: region,
9898
}, scw.WithAllPages())
9999
if err != nil {
100-
return fmt.Errorf("error listing namespaces in (%s) in sweeper: %s", region, err)
100+
return fmt.Errorf("error listing namespaces in (%s) in sweeper: %w", region, err)
101101
}
102102

103103
for _, ns := range listNamespaces.Namespaces {
@@ -108,7 +108,7 @@ func testSweepNamespace(_ string) error {
108108
if err != nil {
109109
logging.L.Debugf("sweeper: error (%s)", err)
110110

111-
return fmt.Errorf("error deleting namespace in sweeper: %s", err)
111+
return fmt.Errorf("error deleting namespace in sweeper: %w", err)
112112
}
113113
}
114114

internal/services/flexibleip/testfuncs/sweep.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func testSweepFlexibleIP(_ string) error {
3434
Zone: zone,
3535
})
3636
if err != nil {
37-
return fmt.Errorf("error deleting ip in sweeper: %s", err)
37+
return fmt.Errorf("error deleting ip in sweeper: %w", err)
3838
}
3939
}
4040

0 commit comments

Comments
 (0)