From 25fbccdff556cf92e9fa8c8762a0f12ef690b937 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Thu, 10 Oct 2024 19:43:40 +0800 Subject: [PATCH] style: upgrade linter --- .github/workflows/lint.yaml | 2 +- .golangci.yaml | 24 +++++++------------ internal/collections/infra/mysql_repo_test.go | 4 ++-- internal/index/mysql_repository_test.go | 10 ++++---- internal/pkg/generic/set/set_test.go | 1 - pkg/duration/duration_test.go | 1 - pkg/wiki/spec_test.go | 1 - web/handler/character/character_test.go | 1 - web/handler/person/person_test.go | 1 - web/handler/revision_test.go | 2 -- web/handler/subject/get_test.go | 2 -- web/handler/user/user_test.go | 1 - web/req/auth_test.go | 2 -- 13 files changed, 16 insertions(+), 36 deletions(-) diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 3ad959c20..8cf84936b 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -53,4 +53,4 @@ jobs: - name: Run linters uses: golangci/golangci-lint-action@v6 with: - version: v1.59.1 + version: v1.61.0 diff --git a/.golangci.yaml b/.golangci.yaml index f1be719f4..380125165 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -18,9 +18,7 @@ run: # If false (default) - golangci-lint acquires file lock on start. allow-parallel-runners: true - skip-files: [] - - go: "1.22" + go: "1.23" # output configuration options output: @@ -106,8 +104,6 @@ linters-settings: - standard # Captures all standard packages if they do not match another section. - default # Contains all imports that could not be matched to another section type. - prefix(github.com/bangumi/server) # Groups all imports with the specified Prefix. - sectionSeparators: - - newLine depguard: rules: @@ -141,8 +137,8 @@ linters-settings: - operation - return - assign - ignored-functions: strconv\..*,time\..*,make,math\..*,strings\..* - ignored-numbers: 1,2,3,10,100,1000,10000 + ignored-functions: [strconv\..*, time\..*, make, math\..*, strings\..*] + ignored-numbers: ["1", "2", "3", "10", "100", "1000", "10000"] gosimple: # Select the Go version to target. The default is '1.13'. @@ -181,7 +177,7 @@ linters-settings: checks: ["all"] stylecheck: - # Select the Go version to target. The default is '1.13'. + # Select the Go version to target. The default is '1.13'. testpackage: # regexp pattern to skip files @@ -203,12 +199,9 @@ linters-settings: nlreturn: block-size: 3 - ifshort: - # Maximum length of vars declaration measured in number of lines, after which linter won't suggest using short syntax. - # Has higher priority than max-decl-chars. - max-decl-lines: 1 - # Maximum length of vars declaration measured in number of characters, after which linter won't suggest using short syntax. - max-decl-chars: 30 + gosec: + excludes: + - G115 tagliatelle: # Check the struck tag name case. @@ -236,8 +229,9 @@ linters: - errchkjson - errname - errorlint + # https://github.com/golangci/golangci-lint/issues/5065 - exhaustive - - exportloopref + - copyloopvar - forbidigo - forcetypeassert - funlen diff --git a/internal/collections/infra/mysql_repo_test.go b/internal/collections/infra/mysql_repo_test.go index 923230929..b464faee7 100644 --- a/internal/collections/infra/mysql_repo_test.go +++ b/internal/collections/infra/mysql_repo_test.go @@ -143,12 +143,12 @@ func TestMysqlRepo_ListSubjectCollection(t *testing.T) { require.NoError(t, err) } - for i := 0; i < 2; i++ { + for i := uint32(0); i < 2; i++ { err = q.SubjectCollection. WithContext(context.Background()). Create(&dao.SubjectCollection{ UserID: uid, - SubjectID: model.SubjectID(200 + i), + SubjectID: 200 + i, SubjectType: model.SubjectTypeGame, UpdatedTime: uint32(time.Now().Unix()), }) diff --git a/internal/index/mysql_repository_test.go b/internal/index/mysql_repository_test.go index 2aef0a569..d57e2b288 100644 --- a/internal/index/mysql_repository_test.go +++ b/internal/index/mysql_repository_test.go @@ -189,9 +189,8 @@ func TestMysqlRepo_DeleteIndex2(t *testing.T) { err := repo.New(ctx, index) require.NoError(t, err) - for i := 10; i < 20; i++ { - _, err = repo.AddOrUpdateIndexSubject(ctx, index.ID, model.SubjectID(i), - uint32(i), fmt.Sprintf("comment %d", i)) + for i := uint32(10); i < 20; i++ { + _, err = repo.AddOrUpdateIndexSubject(ctx, index.ID, i, i, fmt.Sprintf("comment %d", i)) require.NoError(t, err) } @@ -292,9 +291,8 @@ func TestMysqlRepo_DeleteIndexSubject(t *testing.T) { require.NotEqual(t, 0, index.ID) require.NoError(t, err) - for i := 10; i < 20; i++ { - _, err = repo.AddOrUpdateIndexSubject(ctx, index.ID, model.SubjectID(i), - uint32(i), fmt.Sprintf("comment %d", i)) + for i := uint32(10); i < 20; i++ { + _, err = repo.AddOrUpdateIndexSubject(ctx, index.ID, i, i, fmt.Sprintf("comment %d", i)) require.NoError(t, err) } diff --git a/internal/pkg/generic/set/set_test.go b/internal/pkg/generic/set/set_test.go index 5c8034303..cad2a3b41 100644 --- a/internal/pkg/generic/set/set_test.go +++ b/internal/pkg/generic/set/set_test.go @@ -43,7 +43,6 @@ func TestFromSlice(t *testing.T) { {"init without values", []string{}}, } for _, tc := range testcases { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() s := set.FromSlice[string](tc.input) diff --git a/pkg/duration/duration_test.go b/pkg/duration/duration_test.go index 3f87e5cd6..a05a2014e 100644 --- a/pkg/duration/duration_test.go +++ b/pkg/duration/duration_test.go @@ -64,7 +64,6 @@ func TestParse(t *testing.T) { } for _, tc := range testcases { - tc := tc t.Run(tc.Name, func(t *testing.T) { t.Parallel() actual, err := duration.Parse(tc.Input) diff --git a/pkg/wiki/spec_test.go b/pkg/wiki/spec_test.go index e3c9c0f0b..c030c4b5d 100644 --- a/pkg/wiki/spec_test.go +++ b/pkg/wiki/spec_test.go @@ -88,7 +88,6 @@ func TestAgainstInvalidSpec(t *testing.T) { for _, file := range files { // name := file.Name() - file := file t.Run(file.Name(), func(t *testing.T) { t.Parallel() raw, err := os.ReadFile(filepath.Join(caseRoot, file.Name())) diff --git a/web/handler/character/character_test.go b/web/handler/character/character_test.go index 305c2b06a..ddf5cff3d 100644 --- a/web/handler/character/character_test.go +++ b/web/handler/character/character_test.go @@ -92,7 +92,6 @@ func TestCharacter_GetImage(t *testing.T) { app := test.GetWebApp(t, test.Mock{CharacterRepo: m}) for _, imageType := range []string{"large", "grid", "medium", "small"} { - imageType := imageType t.Run(imageType, func(t *testing.T) { t.Parallel() diff --git a/web/handler/person/person_test.go b/web/handler/person/person_test.go index 64dd45f24..073e5b00f 100644 --- a/web/handler/person/person_test.go +++ b/web/handler/person/person_test.go @@ -61,7 +61,6 @@ func TestPerson_GetImage(t *testing.T) { app := test.GetWebApp(t, test.Mock{PersonRepo: m}) for _, imageType := range []string{"small", "grid", "large", "medium"} { - imageType := imageType t.Run(imageType, func(t *testing.T) { t.Parallel() diff --git a/web/handler/revision_test.go b/web/handler/revision_test.go index 13c54d233..88690293d 100644 --- a/web/handler/revision_test.go +++ b/web/handler/revision_test.go @@ -62,7 +62,6 @@ func TestHandler_ListPersonRevision_Bad_ID(t *testing.T) { badIDs := []string{"-1", "a", "0"} for _, id := range badIDs { - id := id t.Run(id, func(t *testing.T) { t.Parallel() @@ -122,7 +121,6 @@ func TestHandler_ListSubjectRevision_Bad_ID(t *testing.T) { badIDs := []string{"-1", "a", "0"} for _, id := range badIDs { - id := id t.Run(id, func(t *testing.T) { t.Parallel() diff --git a/web/handler/subject/get_test.go b/web/handler/subject/get_test.go index 8f9b1fc91..a5a520450 100644 --- a/web/handler/subject/get_test.go +++ b/web/handler/subject/get_test.go @@ -134,7 +134,6 @@ func TestSubject_Get_bad_id(t *testing.T) { app := test.GetWebApp(t, test.Mock{SubjectRepo: m}) for _, path := range []string{"/v0/subjects/0", "/v0/subjects/-1", "/v0/subjects/a"} { - path := path t.Run(path, func(t *testing.T) { t.Parallel() @@ -154,7 +153,6 @@ func TestSubject_GetImage_302(t *testing.T) { app := test.GetWebApp(t, test.Mock{SubjectRepo: m}) for _, imageType := range []string{"small", "grid", "large", "medium", "common"} { - imageType := imageType t.Run(imageType, func(t *testing.T) { t.Parallel() diff --git a/web/handler/user/user_test.go b/web/handler/user/user_test.go index 87fdc7122..9201023cf 100644 --- a/web/handler/user/user_test.go +++ b/web/handler/user/user_test.go @@ -101,7 +101,6 @@ func TestUser_GetAvatar_302(t *testing.T) { app := test.GetWebApp(t, test.Mock{UserRepo: m}) for _, imageType := range []string{"large", "medium", "small"} { - imageType := imageType t.Run(imageType, func(t *testing.T) { t.Parallel() diff --git a/web/req/auth_test.go b/web/req/auth_test.go index 2cb9fedda..d519d114f 100644 --- a/web/req/auth_test.go +++ b/web/req/auth_test.go @@ -32,7 +32,6 @@ func TestLoginPass(t *testing.T) { } validate := validator.New() for i, login := range testCase { - login := login t.Run(fmt.Sprintf("success %d", i), func(t *testing.T) { t.Parallel() require.NoError(t, validate.Struct(login)) @@ -50,7 +49,6 @@ func TestLoginErr(t *testing.T) { } validate := validator.New() for i, login := range testCase { - login := login t.Run(fmt.Sprintf("fail %d", i), func(t *testing.T) { t.Parallel() require.Error(t, validate.Struct(login))