Skip to content

Commit 106c059

Browse files
authored
Add revive linter (#3755)
## Changes - Add revive linter with a subset of rules. - Fix issues found. ## Why Good rules, some stylistic, some catch real bugs, e.g. recursion in IsZero(). ------ https://chatgpt.com/codex/tasks/task_e_68eccd655f1c8325aded897ab7a3bddb Original PR: denik#6
1 parent de9567a commit 106c059

File tree

19 files changed

+98
-99
lines changed

19 files changed

+98
-99
lines changed

.golangci.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ linters:
1010
- intrange
1111
- mirror
1212
- perfsprint
13+
- revive
1314
- staticcheck
1415
- testifylint
1516
- unconvert
@@ -27,6 +28,24 @@ linters:
2728
- (*github.com/spf13/cobra.Command).MarkFlagRequired
2829
- (*github.com/spf13/pflag.FlagSet).MarkDeprecated
2930
- (*github.com/spf13/pflag.FlagSet).MarkHidden
31+
revive:
32+
rules:
33+
- name: blank-imports
34+
- name: context-as-argument
35+
exclude:
36+
- TEST
37+
- "**/integration/**"
38+
- "**/internal/testcli/**"
39+
- "**/internal/testutil/**"
40+
- "**/libs/testdiff/**"
41+
- "**/libs/python/pythontest/**"
42+
- name: context-keys-type
43+
- name: error-return
44+
- name: errorf
45+
- name: range-val-in-closure
46+
- name: waitgroup-by-value
47+
- name: unconditional-recursion
48+
- name: use-waitgroup-go
3049
gocritic:
3150
disable-all: true
3251
enabled-checks:

bundle/internal/validation/enum.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7+
"go/format"
78
"os"
89
"path/filepath"
910
"reflect"
@@ -236,9 +237,16 @@ func generateEnumFields(outPath string) error {
236237
return fmt.Errorf("failed to execute template: %w", err)
237238
}
238239

240+
// Format the generated source with gofmt-equivalent rules so minor template
241+
// whitespace changes don't create noisy diffs in the checked-in artifacts.
242+
formatted, err := format.Source(generatedCode.Bytes())
243+
if err != nil {
244+
return fmt.Errorf("failed to format generated code: %w", err)
245+
}
246+
239247
// Write generated code to file
240248
filePath := filepath.Join(outPath, "enum_fields.go")
241-
if err := os.WriteFile(filePath, generatedCode.Bytes(), 0o644); err != nil {
249+
if err := os.WriteFile(filePath, formatted, 0o644); err != nil {
242250
return fmt.Errorf("failed to write generated code: %w", err)
243251
}
244252

@@ -251,10 +259,6 @@ const enumValidationTemplate = `package generated
251259
// THIS FILE IS AUTOGENERATED.
252260
// DO NOT EDIT THIS FILE DIRECTLY.
253261
254-
import (
255-
_ "github.com/databricks/cli/libs/dyn"
256-
)
257-
258262
// EnumFields maps [dyn.Pattern] to valid enum values they should have.
259263
var EnumFields = map[string][]string{
260264
{{- range . }}

bundle/internal/validation/generated/enum_fields.go

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle/internal/validation/generated/required_fields.go

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle/internal/validation/required.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"fmt"
6+
"go/format"
67
"os"
78
"path/filepath"
89
"reflect"
@@ -185,9 +186,16 @@ func generateRequiredFields(outPath string) error {
185186
return fmt.Errorf("failed to execute template: %w", err)
186187
}
187188

189+
// Format the generated source with gofmt-equivalent rules so minor template
190+
// whitespace changes don't create noisy diffs in the checked-in artifacts.
191+
formatted, err := format.Source(generatedCode.Bytes())
192+
if err != nil {
193+
return fmt.Errorf("failed to format generated code: %w", err)
194+
}
195+
188196
// Write generated code to file
189197
filePath := filepath.Join(outPath, "required_fields.go")
190-
if err := os.WriteFile(filePath, generatedCode.Bytes(), 0o644); err != nil {
198+
if err := os.WriteFile(filePath, formatted, 0o644); err != nil {
191199
return fmt.Errorf("failed to write generated code: %w", err)
192200
}
193201

@@ -200,10 +208,6 @@ const validationTemplate = `package generated
200208
// THIS FILE IS AUTOGENERATED.
201209
// DO NOT EDIT THIS FILE DIRECTLY.
202210
203-
import (
204-
_ "github.com/databricks/cli/libs/dyn"
205-
)
206-
207211
// RequiredFields maps [dyn.Pattern] to required fields they should have.
208212
var RequiredFields = map[string][]string{
209213
{{- range . }}

bundle/mutator_read_only.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,13 @@ func ApplyParallel(ctx context.Context, b *Bundle, mutators ...ReadOnlyMutator)
3838
}
3939

4040
for ind, m := range mutators {
41-
wg.Add(1)
42-
go func() {
43-
defer wg.Done()
41+
wg.Go(func() {
4442
// We're not using bundle.ApplyContext here because we don't do copy between typed and dynamic values
4543
diags := m.Apply(contexts[ind], b)
4644
for _, d := range diags {
4745
logdiag.LogDiag(ctx, d)
4846
}
49-
}()
47+
})
5048
}
5149

5250
wg.Wait()

cmd/auth/profiles.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,12 @@ func newProfilesCommand() *cobra.Command {
115115
if profile.IsEmpty() {
116116
continue
117117
}
118-
wg.Add(1)
119-
go func() {
118+
wg.Go(func() {
120119
ctx := cmd.Context()
121120
t := time.Now()
122121
profile.Load(ctx, iniFile.Path(), skipValidate)
123122
log.Debugf(ctx, "Profile %q took %s to load", profile.Name, time.Since(t))
124-
wg.Done()
125-
}()
123+
})
126124
profiles = append(profiles, profile)
127125
}
128126
wg.Wait()

cmd/auth/token.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ func loadToken(ctx context.Context, args loadTokenArgs) (*oauth2.Token, error) {
129129
// This is captured in an acceptance test under "cmd/auth/token".
130130
err = errors.New("cache: databricks OAuth is not configured for this host")
131131
}
132-
if err, ok := auth.RewriteAuthError(ctx, args.authArguments.Host, args.authArguments.AccountID, args.profileName, err); ok {
133-
return nil, err
132+
if rewritten, rewrittenErr := auth.RewriteAuthError(ctx, args.authArguments.Host, args.authArguments.AccountID, args.profileName, err); rewritten {
133+
return nil, rewrittenErr
134134
}
135135
helpMsg := helpfulError(ctx, args.profileName, oauthArgument)
136136
return nil, fmt.Errorf("%w. %s", err, helpMsg)

cmd/root/auth.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ func emptyHttpRequest(ctx context.Context) *http.Request {
314314
}
315315

316316
func renderError(ctx context.Context, cfg *config.Config, err error) error {
317-
err, _ = auth.RewriteAuthError(ctx, cfg.Host, cfg.AccountID, cfg.Profile, err)
317+
if rewritten, newErr := auth.RewriteAuthError(ctx, cfg.Host, cfg.AccountID, cfg.Profile, err); rewritten {
318+
return newErr
319+
}
318320
return err
319321
}

experimental/ssh/internal/proxy/client_server_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,7 @@ func TestHandover(t *testing.T) {
165165
}
166166

167167
wg := sync.WaitGroup{}
168-
wg.Add(1)
169-
go func() {
170-
defer wg.Done()
168+
wg.Go(func() {
171169
handoverCount := 0
172170
for {
173171
select {
@@ -182,7 +180,7 @@ func TestHandover(t *testing.T) {
182180
time.Sleep(time.Millisecond)
183181
}
184182
}
185-
}()
183+
})
186184

187185
wg.Wait()
188186

0 commit comments

Comments
 (0)