Skip to content

Commit f48ed26

Browse files
Thomas StrombergThomas Stromberg
authored andcommitted
Lint improvements
1 parent e3e2be9 commit f48ed26

File tree

9 files changed

+46
-29
lines changed

9 files changed

+46
-29
lines changed

.golangci.yml

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ linters:
3939
- ginkgolinter # [if you use ginkgo/gomega] enforces standards of using ginkgo and gomega
4040
- gochecknoglobals # checks that no global variables exist
4141
- cyclop # replaced by revive
42+
- gocyclo # replaced by revive
43+
- forbidigo # needs configuration to be useful
4244
- funlen # replaced by revive
4345
- godox # TODO's are OK
4446
- ireturn # It's OK
@@ -47,6 +49,7 @@ linters:
4749
- goconst # finds repeated strings that could be replaced by a constant
4850
- goheader # checks is file header matches to pattern
4951
- gomodguard # [use more powerful depguard] allow and block lists linter for direct Go module dependencies
52+
- gomoddirectives
5053
- err113 # bad advice about dynamic errors
5154
- lll # [replaced by golines] reports long lines
5255
- mnd # detects magic numbers, duplicated by revive
@@ -98,6 +101,7 @@ linters:
98101
errcheck:
99102
# Report about not checking of errors in type assertions: `a := b.(MyStruct)`.
100103
check-type-assertions: true
104+
check-blank: true
101105

102106
exhaustive:
103107
# Program elements to check for exhaustiveness.
@@ -148,10 +152,10 @@ linters:
148152

149153
nakedret:
150154
# Default: 30
151-
max-func-lines: 4
155+
max-func-lines: 7
152156

153157
nestif:
154-
min-complexity: 12
158+
min-complexity: 15
155159

156160
nolintlint:
157161
# Exclude following linters from requiring an explanation.
@@ -167,24 +171,21 @@ linters:
167171
rules:
168172
- name: add-constant
169173
severity: warning
170-
disabled: false
171-
exclude: [""]
172-
arguments:
173-
- max-lit-count: "5"
174-
allow-strs: '"","\n"'
175-
allow-ints: "0,1,2,3,256,1024"
176-
allow-floats: "0.0,0.,1.0,1.,2.0,2."
174+
disabled: true
177175
- name: cognitive-complexity
178-
arguments: [50]
176+
disabled: true # prefer maintidx
179177
- name: cyclomatic
180-
severity: warning
181-
arguments: [30]
178+
disabled: true # prefer maintidx
182179
- name: function-length
183180
arguments: [150, 225]
184181
- name: line-length-limit
185182
arguments: [150]
186183
- name: nested-structs
187184
disabled: true
185+
- name: max-public-structs
186+
arguments: [10]
187+
- name: flag-parameter # fixes are difficult
188+
disabled: true
188189

189190
rowserrcheck:
190191
# database/sql is always checked.
@@ -206,8 +207,14 @@ linters:
206207
os-temp-dir: true
207208

208209
varnamelen:
209-
max-distance: 40
210+
max-distance: 75
210211
min-name-length: 2
212+
check-receivers: false
213+
ignore-names:
214+
- r
215+
- w
216+
- f
217+
- err
211218

212219
exclusions:
213220
# Default: []
@@ -225,6 +232,8 @@ linters:
225232
linters:
226233
- dupl
227234
- gosec
235+
- godot
236+
- govet # alignment
228237
- noctx
229238
- perfsprint
230239
- revive

Makefile

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ LINTERS :=
3737
FIXERS :=
3838

3939
GOLANGCI_LINT_CONFIG := $(LINT_ROOT)/.golangci.yml
40-
GOLANGCI_LINT_VERSION ?= v2.3.0
40+
GOLANGCI_LINT_VERSION ?= v2.5.0
4141
GOLANGCI_LINT_BIN := $(LINT_ROOT)/out/linters/golangci-lint-$(GOLANGCI_LINT_VERSION)-$(LINT_ARCH)
4242
$(GOLANGCI_LINT_BIN):
4343
mkdir -p $(LINT_ROOT)/out/linters
@@ -67,9 +67,19 @@ yamllint-lint: $(YAMLLINT_BIN)
6767
PYTHONPATH=$(YAMLLINT_ROOT)/dist $(YAMLLINT_ROOT)/dist/bin/yamllint .
6868

6969
.PHONY: _lint $(LINTERS)
70-
_lint: $(LINTERS)
70+
_lint:
71+
@exit_code=0; \
72+
for target in $(LINTERS); do \
73+
$(MAKE) $$target || exit_code=1; \
74+
done; \
75+
exit $$exit_code
7176

7277
.PHONY: fix $(FIXERS)
73-
fix: $(FIXERS)
78+
fix:
79+
@exit_code=0; \
80+
for target in $(FIXERS); do \
81+
$(MAKE) $$target || exit_code=1; \
82+
done; \
83+
exit $$exit_code
7484

7585
# END: lint-install .

examples/custom_retry_function_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ func (e *RetriableError) Error() string {
2424
return fmt.Sprintf("%s (retry after %v)", e.Err.Error(), e.RetryAfter)
2525
}
2626

27-
var _ error = (*RetriableError)(nil)
28-
2927
// TestCustomRetryFunction shows how to use a custom retry function.
3028
func TestCustomRetryFunction(t *testing.T) {
3129
attempts := 5 // server succeeds after 5 attempts
@@ -35,12 +33,12 @@ func TestCustomRetryFunction(t *testing.T) {
3533
// HTTP 429 status code with Retry-After header in seconds
3634
w.Header().Add("Retry-After", "1")
3735
w.WriteHeader(http.StatusTooManyRequests)
38-
_, _ = w.Write([]byte("Server limit reached"))
36+
w.Write([]byte("Server limit reached")) //nolint:errcheck // Test server response
3937
attempts--
4038
return
4139
}
4240
w.WriteHeader(http.StatusOK)
43-
_, _ = w.Write([]byte("hello"))
41+
w.Write([]byte("hello")) //nolint:errcheck // Test server response
4442
}))
4543
defer ts.Close()
4644

examples/delay_based_on_error_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (err SomeOtherError) Error() string {
3838

3939
func TestCustomRetryFunctionBasedOnKindOfError(t *testing.T) {
4040
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
41-
_, _ = fmt.Fprintln(w, "hello")
41+
fmt.Fprintln(w, "hello") //nolint:errcheck // Test server response
4242
}))
4343
defer ts.Close()
4444

examples/errors_history_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestErrorHistory(t *testing.T) {
2929
if err != nil {
3030
return err
3131
}
32-
defer func() { _ = resp.Body.Close() }()
32+
defer resp.Body.Close() //nolint:errcheck // Error already checked in main function
3333
if resp.StatusCode != http.StatusOK {
3434
return fmt.Errorf("failed HTTP - %d", resp.StatusCode)
3535
}

examples/http_get_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
func TestGet(t *testing.T) {
1414
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
15-
_, _ = fmt.Fprintln(w, "hello")
15+
fmt.Fprintln(w, "hello") //nolint:errcheck // Test server response
1616
}))
1717
defer ts.Close()
1818

options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package retry //nolint:revive // More than 5 public structs are necessary for API flexibility
1+
package retry
22

33
import (
44
"context"

retry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error {
8282
// By default, it will retry up to 10 times with exponential backoff and jitter.
8383
// The behavior can be customized using Option functions.
8484
//
85-
//nolint:gocognit,gocyclo,revive // Complexity is necessary for retry logic
85+
//nolint:gocognit,gocyclo // Complexity is necessary for retry logic
8686
func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) (T, error) {
8787
var attempt uint
8888
var emptyT T

retry_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ func BenchmarkDo(b *testing.B) {
741741
testError := errors.New("test error")
742742

743743
for range b.N {
744-
_ = Do(
744+
Do( //nolint:errcheck // Benchmark intentionally ignores errors
745745
func() error {
746746
return testError
747747
},
@@ -755,7 +755,7 @@ func BenchmarkDoWithData(b *testing.B) {
755755
testError := errors.New("test error")
756756

757757
for range b.N {
758-
_, _ = DoWithData(
758+
DoWithData( //nolint:errcheck // Benchmark intentionally ignores errors
759759
func() (int, error) {
760760
return 0, testError
761761
},
@@ -767,7 +767,7 @@ func BenchmarkDoWithData(b *testing.B) {
767767

768768
func BenchmarkDoNoErrors(b *testing.B) {
769769
for range b.N {
770-
_ = Do(
770+
Do( //nolint:errcheck // Benchmark intentionally ignores errors
771771
func() error {
772772
return nil
773773
},
@@ -779,7 +779,7 @@ func BenchmarkDoNoErrors(b *testing.B) {
779779

780780
func BenchmarkDoWithDataNoErrors(b *testing.B) {
781781
for range b.N {
782-
_, _ = DoWithData(
782+
DoWithData( //nolint:errcheck // Benchmark intentionally ignores errors
783783
func() (int, error) {
784784
return 0, nil
785785
},

0 commit comments

Comments
 (0)