Skip to content

Commit c4287b1

Browse files
authored
ci(golangci-lint): update configuration and fix lint issues (#4247)
* ci: update golangci-lint configuration and lint settings - Update golangci-lint to version 2 - Enable new linters and adjust existing ones - Update lint settings across multiple test files - Remove unused struct and variable checks - Add new lint exclusions for generated code and specific directories Signed-off-by: Flc <four_leaf_clover@foxmail.com> * ci(github): update golangci-lint-action to v8 and lint version to v2.3.4 Signed-off-by: Flc <four_leaf_clover@foxmail.com> * ci: downgrade golangci-lint to v2.1.6 Signed-off-by: Flc <four_leaf_clover@foxmail.com> * ci(golangci): add gofumpt linter and fix related issues- Added gofumpt linter to .golangci.yml Signed-off-by: Flc <four_leaf_clover@foxmail.com> * test: ignore testifylint and gofumpt lints in specific test cases Signed-off-by: Flc <four_leaf_clover@foxmail.com> * build(deps): remove golang.org/x/lint - Remove golang.org/x/lint package from go.mod - Update related dependencies in go.sum Signed-off-by: flc1125 <four_leaf_clover@foxmail.com> * build(deps): downgrade golang.org/x/mod and golang.org/x/tools - Downgrade golang.org/x/mod from v0.24.0 to v0.18.0 - Downgrade golang.org/x/tools from v0.33.0 to v.22.0 These changes are made to address compatibility issues with the current project setup. Signed-off-by: flc1125 <four_leaf_clover@foxmail.com> --------- Signed-off-by: Flc <four_leaf_clover@foxmail.com> Signed-off-by: flc1125 <four_leaf_clover@foxmail.com>
1 parent 8fb3136 commit c4287b1

27 files changed

Lines changed: 164 additions & 149 deletions

.github/workflows/gin.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ jobs:
2424
with:
2525
go-version: "^1"
2626
- name: Setup golangci-lint
27-
uses: golangci/golangci-lint-action@v6
27+
uses: golangci/golangci-lint-action@v8
2828
with:
29-
version: v1.61.0
29+
version: v2.1.6
3030
args: --verbose
3131
test:
3232
needs: lint

.golangci.yml

Lines changed: 65 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
run:
2-
timeout: 5m
1+
version: "2"
32
linters:
43
enable:
54
- asciicheck
5+
- copyloopvar
66
- dogsled
77
- durationcheck
8-
- errcheck
98
- errorlint
10-
- copyloopvar
11-
- gci
12-
- gofmt
13-
- goimports
149
- gosec
1510
- misspell
1611
- nakedret
@@ -21,51 +16,66 @@ linters:
2116
- testifylint
2217
- usestdlibvars
2318
- wastedassign
24-
25-
linters-settings:
26-
gosec:
27-
# To select a subset of rules to run.
28-
# Available rules: https://github.com/securego/gosec#available-rules
29-
# Default: [] - means include all rules
30-
includes:
31-
- G102
32-
- G106
33-
- G108
34-
- G109
35-
- G111
36-
- G112
37-
- G201
38-
- G203
39-
perfsprint:
40-
err-error: true
41-
errorf: true
42-
int-conversion: true
43-
sprintf1: true
44-
strconcat: true
45-
testifylint:
46-
enable-all: true
47-
48-
issues:
49-
exclude-rules:
50-
- linters:
51-
- structcheck
52-
- unused
53-
text: "`data` is unused"
54-
- linters:
55-
- staticcheck
56-
text: "SA1019:"
57-
- linters:
58-
- revive
59-
text: "var-naming:"
60-
- linters:
61-
- revive
62-
text: "exported:"
63-
- path: _test\.go
64-
linters:
65-
- gosec # security is not make sense in tests
66-
- linters:
67-
- revive
68-
path: _test\.go
69-
- path: gin.go
70-
linters:
71-
- gci
19+
settings:
20+
gosec:
21+
includes:
22+
- G102
23+
- G106
24+
- G108
25+
- G109
26+
- G111
27+
- G112
28+
- G201
29+
- G203
30+
perfsprint:
31+
int-conversion: true
32+
err-error: true
33+
errorf: true
34+
sprintf1: true
35+
strconcat: true
36+
testifylint:
37+
enable-all: true
38+
exclusions:
39+
generated: lax
40+
presets:
41+
- comments
42+
- common-false-positives
43+
- legacy
44+
- std-error-handling
45+
rules:
46+
- linters:
47+
- structcheck
48+
- unused
49+
text: '`data` is unused'
50+
- linters:
51+
- staticcheck
52+
text: 'SA1019:'
53+
- linters:
54+
- revive
55+
text: 'var-naming:'
56+
- linters:
57+
- revive
58+
text: 'exported:'
59+
- linters:
60+
- gosec
61+
path: _test\.go
62+
- linters:
63+
- revive
64+
path: _test\.go
65+
paths:
66+
- third_party$
67+
- builtin$
68+
- examples$
69+
formatters:
70+
enable:
71+
- gci
72+
- gofmt
73+
- gofumpt
74+
- goimports
75+
exclusions:
76+
generated: lax
77+
paths:
78+
- third_party$
79+
- builtin$
80+
- examples$
81+
- gin.go

binding/binding_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ type FooBarFileStruct struct {
5151
type FooBarFileFailStruct struct {
5252
FooBarStruct
5353
File *multipart.FileHeader `invalid_name:"file" binding:"required"`
54-
// for unexport test
55-
data *multipart.FileHeader `form:"data" binding:"required"`
5654
}
5755

5856
type FooDefaultBarStruct struct {
@@ -1063,7 +1061,7 @@ func testFormBindingInvalidName(t *testing.T, method, path, badPath, body, badBo
10631061
}
10641062
err := b.Bind(req, &obj)
10651063
require.NoError(t, err)
1066-
assert.Equal(t, "", obj.TestName)
1064+
assert.Empty(t, obj.TestName)
10671065

10681066
obj = InvalidNameType{}
10691067
req = requestWithBody(method, badPath, badBody)
@@ -1318,7 +1316,7 @@ func testBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, bad
13181316
req := requestWithBody(http.MethodPost, path, body)
13191317
err := b.Bind(req, &obj)
13201318
require.Error(t, err)
1321-
assert.Equal(t, "", obj.Foo)
1319+
assert.Empty(t, obj.Foo)
13221320

13231321
obj = FooStruct{}
13241322
req = requestWithBody(http.MethodPost, badPath, badBody)

binding/default_validator_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ func TestSliceValidationError(t *testing.T) {
1818
{"has nil elements", SliceValidationError{errors.New("test error"), nil}, "[0]: test error"},
1919
{"has zero elements", SliceValidationError{}, ""},
2020
{"has one element", SliceValidationError{errors.New("test one error")}, "[0]: test one error"},
21-
{"has two elements",
21+
{
22+
"has two elements",
2223
SliceValidationError{
2324
errors.New("first error"),
2425
errors.New("second error"),
2526
},
2627
"[0]: first error\n[1]: second error",
2728
},
28-
{"has many elements",
29+
{
30+
"has many elements",
2931
SliceValidationError{
3032
errors.New("first error"),
3133
errors.New("second error"),

binding/form.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import (
1111

1212
const defaultMemory = 32 << 20
1313

14-
type formBinding struct{}
15-
type formPostBinding struct{}
16-
type formMultipartBinding struct{}
14+
type (
15+
formBinding struct{}
16+
formPostBinding struct{}
17+
formMultipartBinding struct{}
18+
)
1719

1820
func (formBinding) Name() string {
1921
return "form"

binding/form_mapping_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,9 @@ func TestMappingCustomStructTypeWithFormTag(t *testing.T) {
509509
err := mappingByPtr(&s, formSource{"data": {`file:/foo:happiness`}}, "form")
510510
require.NoError(t, err)
511511

512-
assert.EqualValues(t, "file", s.FileData.Protocol)
513-
assert.EqualValues(t, "/foo", s.FileData.Path)
514-
assert.EqualValues(t, "happiness", s.FileData.Name)
512+
assert.Equal(t, "file", s.FileData.Protocol)
513+
assert.Equal(t, "/foo", s.FileData.Path)
514+
assert.Equal(t, "happiness", s.FileData.Name)
515515
}
516516

517517
func TestMappingCustomStructTypeWithURITag(t *testing.T) {
@@ -521,9 +521,9 @@ func TestMappingCustomStructTypeWithURITag(t *testing.T) {
521521
err := mappingByPtr(&s, formSource{"data": {`file:/foo:happiness`}}, "uri")
522522
require.NoError(t, err)
523523

524-
assert.EqualValues(t, "file", s.FileData.Protocol)
525-
assert.EqualValues(t, "/foo", s.FileData.Path)
526-
assert.EqualValues(t, "happiness", s.FileData.Name)
524+
assert.Equal(t, "file", s.FileData.Protocol)
525+
assert.Equal(t, "/foo", s.FileData.Path)
526+
assert.Equal(t, "happiness", s.FileData.Name)
527527
}
528528

529529
func TestMappingCustomPointerStructTypeWithFormTag(t *testing.T) {
@@ -533,9 +533,9 @@ func TestMappingCustomPointerStructTypeWithFormTag(t *testing.T) {
533533
err := mappingByPtr(&s, formSource{"data": {`file:/foo:happiness`}}, "form")
534534
require.NoError(t, err)
535535

536-
assert.EqualValues(t, "file", s.FileData.Protocol)
537-
assert.EqualValues(t, "/foo", s.FileData.Path)
538-
assert.EqualValues(t, "happiness", s.FileData.Name)
536+
assert.Equal(t, "file", s.FileData.Protocol)
537+
assert.Equal(t, "/foo", s.FileData.Path)
538+
assert.Equal(t, "happiness", s.FileData.Name)
539539
}
540540

541541
func TestMappingCustomPointerStructTypeWithURITag(t *testing.T) {
@@ -545,9 +545,9 @@ func TestMappingCustomPointerStructTypeWithURITag(t *testing.T) {
545545
err := mappingByPtr(&s, formSource{"data": {`file:/foo:happiness`}}, "uri")
546546
require.NoError(t, err)
547547

548-
assert.EqualValues(t, "file", s.FileData.Protocol)
549-
assert.EqualValues(t, "/foo", s.FileData.Path)
550-
assert.EqualValues(t, "happiness", s.FileData.Name)
548+
assert.Equal(t, "file", s.FileData.Protocol)
549+
assert.Equal(t, "/foo", s.FileData.Path)
550+
assert.Equal(t, "happiness", s.FileData.Name)
551551
}
552552

553553
type customPath []string
@@ -570,8 +570,8 @@ func TestMappingCustomSliceUri(t *testing.T) {
570570
err := mappingByPtr(&s, formSource{"path": {`bar/foo`}}, "uri")
571571
require.NoError(t, err)
572572

573-
assert.EqualValues(t, "bar", s.FileData[0])
574-
assert.EqualValues(t, "foo", s.FileData[1])
573+
assert.Equal(t, "bar", s.FileData[0])
574+
assert.Equal(t, "foo", s.FileData[1])
575575
}
576576

577577
func TestMappingCustomSliceForm(t *testing.T) {
@@ -581,8 +581,8 @@ func TestMappingCustomSliceForm(t *testing.T) {
581581
err := mappingByPtr(&s, formSource{"path": {`bar/foo`}}, "form")
582582
require.NoError(t, err)
583583

584-
assert.EqualValues(t, "bar", s.FileData[0])
585-
assert.EqualValues(t, "foo", s.FileData[1])
584+
assert.Equal(t, "bar", s.FileData[0])
585+
assert.Equal(t, "foo", s.FileData[1])
586586
}
587587

588588
type objectID [12]byte
@@ -621,7 +621,7 @@ func TestMappingCustomArrayUri(t *testing.T) {
621621
require.NoError(t, err)
622622

623623
expected, _ := convertTo(val)
624-
assert.EqualValues(t, expected, s.FileData)
624+
assert.Equal(t, expected, s.FileData)
625625
}
626626

627627
func TestMappingCustomArrayForm(t *testing.T) {
@@ -633,5 +633,5 @@ func TestMappingCustomArrayForm(t *testing.T) {
633633
require.NoError(t, err)
634634

635635
expected, _ := convertTo(val)
636-
assert.EqualValues(t, expected, s.FileData)
636+
assert.Equal(t, expected, s.FileData)
637637
}

binding/header.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ func (headerBinding) Name() string {
1717
}
1818

1919
func (headerBinding) Bind(req *http.Request, obj any) error {
20-
2120
if err := mapHeader(obj, req.Header); err != nil {
2221
return err
2322
}

binding/validate_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,16 @@ type structNoValidationPointer struct {
158158
}
159159

160160
func TestValidateNoValidationPointers(t *testing.T) {
161-
//origin := createNoValidation_values()
162-
//test := createNoValidation_values()
161+
// origin := createNoValidation_values()
162+
// test := createNoValidation_values()
163163
empty := structNoValidationPointer{}
164164

165-
//assert.Nil(t, validate(test))
166-
//assert.Nil(t, validate(&test))
165+
// assert.Nil(t, validate(test))
166+
// assert.Nil(t, validate(&test))
167167
require.NoError(t, validate(empty))
168168
require.NoError(t, validate(&empty))
169169

170-
//assert.Equal(t, origin, test)
170+
// assert.Equal(t, origin, test)
171171
}
172172

173173
type Object map[string]any
@@ -198,7 +198,7 @@ type structModifyValidation struct {
198198
}
199199

200200
func toZero(sl validator.StructLevel) {
201-
var s *structModifyValidation = sl.Top().Interface().(*structModifyValidation)
201+
s := sl.Top().Interface().(*structModifyValidation)
202202
s.Integer = 0
203203
}
204204

@@ -249,5 +249,5 @@ func TestValidatorEngine(t *testing.T) {
249249
// Check that we got back non-nil errs
250250
require.Error(t, errs)
251251
// Check that the error matches expectation
252-
require.Error(t, errs, "", "", "notone")
252+
require.Error(t, errs, "notone")
253253
}

binding/xml.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func (xmlBinding) Bind(req *http.Request, obj any) error {
2424
func (xmlBinding) BindBody(body []byte, obj any) error {
2525
return decodeXML(bytes.NewReader(body), obj)
2626
}
27+
2728
func decodeXML(r io.Reader, obj any) error {
2829
decoder := xml.NewDecoder(r)
2930
if err := decoder.Decode(obj); err != nil {

0 commit comments

Comments
 (0)