Skip to content

Commit 433b082

Browse files
authored
Update linter to v2.0.2 (#1405)
## Fixes Or Enhances Recently, the Dependabot [PR](#1403) for the linter update failed. This PR updates the linter to `v2.0.2`, adds a linter configuration file, and addresses some obvious errors: - Error return value of `file.Close` is not checked (errcheck) - Error return value of `os.Remove` is not checked (errcheck) - could use strings.ReplaceAll instead (staticcheck) - could omit type interface{} from declaration; it will be inferred from the right-hand side (staticcheck) **Make sure that you've checked the boxes below before you submit PR:** - [ ] Tests exist or have been written that cover this particular change. @go-playground/validator-maintainers
1 parent 8592022 commit 433b082

File tree

6 files changed

+136
-22
lines changed

6 files changed

+136
-22
lines changed

Diff for: .github/workflows/workflow.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ jobs:
3838
with:
3939
go-version: 1.24.x
4040
- name: golangci-lint
41-
uses: golangci/golangci-lint-action@v6
41+
uses: golangci/golangci-lint-action@v7
4242
with:
4343
version: latest

Diff for: .golangci.yaml

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
version: "2"
2+
linters:
3+
default: all
4+
disable:
5+
- asasalint
6+
- asciicheck
7+
- bidichk
8+
- bodyclose
9+
- canonicalheader
10+
- containedctx
11+
- contextcheck
12+
- copyloopvar
13+
- cyclop
14+
- decorder
15+
- depguard
16+
- dogsled
17+
- dupl
18+
- dupword
19+
- durationcheck
20+
- err113
21+
- errcheck
22+
- errchkjson
23+
- errname
24+
- errorlint
25+
- exhaustive
26+
- exhaustruct
27+
- exptostd
28+
- fatcontext
29+
- forbidigo
30+
- forcetypeassert
31+
- funlen
32+
- ginkgolinter
33+
- gocheckcompilerdirectives
34+
- gochecknoglobals
35+
- gochecknoinits
36+
- gochecksumtype
37+
- gocognit
38+
- goconst
39+
- gocritic
40+
- gocyclo
41+
- godot
42+
- godox
43+
- goheader
44+
- gomoddirectives
45+
- gomodguard
46+
- goprintffuncname
47+
- gosec
48+
- gosmopolitan
49+
- govet
50+
- grouper
51+
- iface
52+
- importas
53+
- inamedparam
54+
- ineffassign
55+
- interfacebloat
56+
- intrange
57+
- ireturn
58+
- lll
59+
- loggercheck
60+
- maintidx
61+
- makezero
62+
- mirror
63+
- misspell
64+
- mnd
65+
- musttag
66+
- nakedret
67+
- nestif
68+
- nilerr
69+
- nilnesserr
70+
- nilnil
71+
- nlreturn
72+
- noctx
73+
- nolintlint
74+
- nonamedreturns
75+
- nosprintfhostport
76+
- paralleltest
77+
- perfsprint
78+
- prealloc
79+
- predeclared
80+
- promlinter
81+
- protogetter
82+
- reassign
83+
- recvcheck
84+
- revive
85+
- rowserrcheck
86+
- sloglint
87+
- spancheck
88+
- sqlclosecheck
89+
- staticcheck
90+
- tagalign
91+
- tagliatelle
92+
- testableexamples
93+
- testifylint
94+
- testpackage
95+
- thelper
96+
- tparallel
97+
- unparam
98+
- varnamelen
99+
- whitespace
100+
- wrapcheck
101+
- wsl
102+
- zerologlint

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ GOCMD=go
33
linters-install:
44
@golangci-lint --version >/dev/null 2>&1 || { \
55
echo "installing linting tools..."; \
6-
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s v1.41.1; \
6+
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s v2.0.2; \
77
}
88

99
lint: linters-install

Diff for: baked_in.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func parseOneOfParam2(s string) []string {
260260
oneofValsCacheRWLock.Lock()
261261
vals = splitParamsRegex().FindAllString(s, -1)
262262
for i := 0; i < len(vals); i++ {
263-
vals[i] = strings.Replace(vals[i], "'", "", -1)
263+
vals[i] = strings.ReplaceAll(vals[i], "'", "")
264264
}
265265
oneofValsCache[s] = vals
266266
oneofValsCacheRWLock.Unlock()
@@ -1619,7 +1619,9 @@ func isImage(fl FieldLevel) bool {
16191619
if err != nil {
16201620
return false
16211621
}
1622-
defer file.Close()
1622+
defer func() {
1623+
_ = file.Close()
1624+
}()
16231625

16241626
mime, err := mimetype.DetectReader(file)
16251627
if err != nil {

Diff for: cache.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func (v *Validate) parseFieldTagsRecursive(tag string, fieldName string, alias s
309309
}
310310

311311
if len(vals) > 1 {
312-
current.param = strings.Replace(strings.Replace(vals[1], utf8HexComma, ",", -1), utf8Pipe, "|", -1)
312+
current.param = strings.ReplaceAll(strings.ReplaceAll(vals[1], utf8HexComma, ","), utf8Pipe, "|")
313313
}
314314
}
315315
current.isBlockEnd = true

Diff for: validator_test.go

+27-17
Original file line numberDiff line numberDiff line change
@@ -3088,7 +3088,7 @@ func TestBadKeyValidation(t *testing.T) {
30883088

30893089
func TestInterfaceErrValidation(t *testing.T) {
30903090
var v2 interface{} = 1
3091-
var v1 interface{} = v2
3091+
var v1 = v2
30923092

30933093
validate := New()
30943094
errs := validate.Var(v1, "len=1")
@@ -5941,13 +5941,15 @@ func TestFileValidation(t *testing.T) {
59415941
func TestImageValidation(t *testing.T) {
59425942
validate := New()
59435943

5944+
tmpDir := t.TempDir()
5945+
59445946
paths := map[string]string{
59455947
"empty": "",
59465948
"directory": "testdata",
5947-
"missing": filepath.Join("testdata", "none.png"),
5948-
"png": filepath.Join("testdata", "image.png"),
5949-
"jpeg": filepath.Join("testdata", "image.jpg"),
5950-
"mp3": filepath.Join("testdata", "music.mp3"),
5949+
"missing": filepath.Join(tmpDir, "none.png"),
5950+
"png": filepath.Join(tmpDir, "image.png"),
5951+
"jpeg": filepath.Join(tmpDir, "image.jpg"),
5952+
"mp3": filepath.Join(tmpDir, "music.mp3"),
59515953
}
59525954

59535955
tests := []struct {
@@ -5983,14 +5985,18 @@ func TestImageValidation(t *testing.T) {
59835985
true,
59845986
func() {
59855987
img := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{10, 10}})
5986-
f, _ := os.Create(paths["png"])
5987-
err := png.Encode(f, img)
5988-
if err != nil {
5989-
panic(fmt.Sprintf("Could not encode file in PNG. Error: %s", err))
5990-
}
5988+
f, err := os.Create(paths["png"])
5989+
Equal(t, err, nil)
5990+
defer func() {
5991+
_ = f.Close()
5992+
}()
5993+
5994+
err = png.Encode(f, img)
5995+
Equal(t, err, nil)
59915996
},
59925997
func() {
5993-
os.Remove(paths["png"])
5998+
err := os.Remove(paths["png"])
5999+
Equal(t, err, nil)
59946000
},
59956001
},
59966002
{
@@ -6000,14 +6006,18 @@ func TestImageValidation(t *testing.T) {
60006006
func() {
60016007
var opt jpeg.Options
60026008
img := image.NewGray(image.Rect(0, 0, 10, 10))
6003-
f, _ := os.Create(paths["jpeg"])
6004-
err := jpeg.Encode(f, img, &opt)
6005-
if err != nil {
6006-
panic(fmt.Sprintf("Could not encode file in JPEG. Error: %s", err))
6007-
}
6009+
f, err := os.Create(paths["jpeg"])
6010+
Equal(t, err, nil)
6011+
defer func() {
6012+
_ = f.Close()
6013+
}()
6014+
6015+
err = jpeg.Encode(f, img, &opt)
6016+
Equal(t, err, nil)
60086017
},
60096018
func() {
6010-
os.Remove(paths["jpeg"])
6019+
err := os.Remove(paths["jpeg"])
6020+
Equal(t, err, nil)
60116021
},
60126022
},
60136023
{

0 commit comments

Comments
 (0)