Skip to content

Commit b5ecae5

Browse files
42wimSysoev, Vladimir
authored and
Sysoev, Vladimir
committed
Add more linters to improve code readability (go-gitea#19989)
Add nakedret, unconvert, wastedassign, stylecheck and nolintlint linters to improve code readability - nakedret - https://github.com/alexkohler/nakedret - nakedret is a Go static analysis tool to find naked returns in functions greater than a specified function length. - unconvert - https://github.com/mdempsky/unconvert - Remove unnecessary type conversions - wastedassign - https://github.com/sanposhiho/wastedassign - wastedassign finds wasted assignment statements. - notlintlint - Reports ill-formed or insufficient nolint directives - stylecheck - https://staticcheck.io/docs/checks/#ST - keep style consistent - excluded: [ST1003 - Poorly chosen identifier](https://staticcheck.io/docs/checks/#ST1003) and [ST1005 - Incorrectly formatted error string](https://staticcheck.io/docs/checks/#ST1005)
1 parent 505d552 commit b5ecae5

File tree

147 files changed

+402
-397
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+402
-397
lines changed

Diff for: .golangci.yml

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ linters:
1919
- revive
2020
- gofumpt
2121
- depguard
22+
- nakedret
23+
- unconvert
24+
- wastedassign
25+
- nolintlint
26+
- stylecheck
2227
enable-all: false
2328
disable-all: true
2429
fast: false
@@ -32,6 +37,10 @@ run:
3237
- web_src
3338

3439
linters-settings:
40+
stylecheck:
41+
checks: ["all", "-ST1005", "-ST1003"]
42+
nakedret:
43+
max-func-lines: 0
3544
gocritic:
3645
disabled-checks:
3746
- ifElseChain

Diff for: cmd/hook.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ func writeDataPktLine(out io.Writer, data []byte) error {
792792
if err != nil {
793793
return fail("Internal Server Error", "Pkt-Line response failed: %v", err)
794794
}
795-
if 4 != lr {
795+
if lr != 4 {
796796
return fail("Internal Server Error", "Pkt-Line response failed: %v", err)
797797
}
798798

Diff for: integrations/api_issue_stopwatch_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestAPIListStopWatches(t *testing.T) {
3838
assert.EqualValues(t, issue.Title, apiWatches[0].IssueTitle)
3939
assert.EqualValues(t, repo.Name, apiWatches[0].RepoName)
4040
assert.EqualValues(t, repo.OwnerName, apiWatches[0].RepoOwnerName)
41-
assert.Greater(t, int64(apiWatches[0].Seconds), int64(0))
41+
assert.Greater(t, apiWatches[0].Seconds, int64(0))
4242
}
4343
}
4444

Diff for: integrations/api_packages_container_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func TestPackageContainer(t *testing.T) {
8888

8989
req = NewRequest(t, "GET", fmt.Sprintf("%sv2", setting.AppURL))
9090
addTokenAuthHeader(req, anonymousToken)
91-
resp = MakeRequest(t, req, http.StatusOK)
91+
MakeRequest(t, req, http.StatusOK)
9292
})
9393

9494
t.Run("User", func(t *testing.T) {
@@ -112,7 +112,7 @@ func TestPackageContainer(t *testing.T) {
112112

113113
req = NewRequest(t, "GET", fmt.Sprintf("%sv2", setting.AppURL))
114114
addTokenAuthHeader(req, userToken)
115-
resp = MakeRequest(t, req, http.StatusOK)
115+
MakeRequest(t, req, http.StatusOK)
116116
})
117117
})
118118

Diff for: integrations/editor_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
8282
"_csrf": csrf,
8383
"protected": "off",
8484
})
85-
resp = session.MakeRequest(t, req, http.StatusSeeOther)
85+
session.MakeRequest(t, req, http.StatusSeeOther)
8686
// Check if master branch has been locked successfully
8787
flashCookie = session.GetCookie("macaron_flash")
8888
assert.NotNil(t, flashCookie)
@@ -109,7 +109,7 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa
109109
"commit_choice": "direct",
110110
},
111111
)
112-
resp = session.MakeRequest(t, req, http.StatusSeeOther)
112+
session.MakeRequest(t, req, http.StatusSeeOther)
113113

114114
// Verify the change
115115
req = NewRequest(t, "GET", path.Join(user, repo, "raw/branch", branch, filePath))
@@ -139,7 +139,7 @@ func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, bra
139139
"new_branch_name": targetBranch,
140140
},
141141
)
142-
resp = session.MakeRequest(t, req, http.StatusSeeOther)
142+
session.MakeRequest(t, req, http.StatusSeeOther)
143143

144144
// Verify the change
145145
req = NewRequest(t, "GET", path.Join(user, repo, "raw/branch", targetBranch, filePath))

Diff for: integrations/git_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func standardCommitAndPushTest(t *testing.T, dstPath string) (little, big string
150150
defer PrintCurrentTest(t)()
151151
little, big = commitAndPushTest(t, dstPath, "data-file-")
152152
})
153-
return
153+
return little, big
154154
}
155155

156156
func lfsCommitAndPushTest(t *testing.T, dstPath string) (littleLFS, bigLFS string) {
@@ -191,7 +191,7 @@ func lfsCommitAndPushTest(t *testing.T, dstPath string) (littleLFS, bigLFS strin
191191
lockTest(t, dstPath)
192192
})
193193
})
194-
return
194+
return littleLFS, bigLFS
195195
}
196196

197197
func commitAndPushTest(t *testing.T, dstPath, prefix string) (little, big string) {
@@ -210,7 +210,7 @@ func commitAndPushTest(t *testing.T, dstPath, prefix string) (little, big string
210210
big = doCommitAndPush(t, bigSize, dstPath, prefix)
211211
})
212212
})
213-
return
213+
return little, big
214214
}
215215

216216
func rawTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS string) {

Diff for: integrations/integration_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ func getTokenForLoggedInUser(t testing.TB, session *TestSession) string {
438438
"_csrf": doc.GetCSRF(),
439439
"name": fmt.Sprintf("api-testing-token-%d", tokenCounter),
440440
})
441-
resp = session.MakeRequest(t, req, http.StatusSeeOther)
441+
session.MakeRequest(t, req, http.StatusSeeOther)
442442
req = NewRequest(t, "GET", "/user/settings/applications")
443443
resp = session.MakeRequest(t, req, http.StatusOK)
444444
htmlDoc := NewHTMLParser(t, resp.Body)

Diff for: integrations/nonascii_branches_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func testSrcRouteRedirect(t *testing.T, session *TestSession, user, repo, route,
2626

2727
// Perform redirect
2828
req = NewRequest(t, "GET", location)
29-
resp = session.MakeRequest(t, req, expectedStatus)
29+
session.MakeRequest(t, req, expectedStatus)
3030
}
3131

3232
func setDefaultBranch(t *testing.T, session *TestSession, user, repo, branch string) {

Diff for: integrations/oauth_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func TestAccessTokenExchangeWithBasicAuth(t *testing.T) {
197197
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt", // test PKCE additionally
198198
})
199199
req.Header.Add("Authorization", "Basic ZGE3ZGEzYmEtOWExMy00MTY3LTg1NmYtMzg5OWRlMGIwMTM4OmJsYWJsYQ==")
200-
resp = MakeRequest(t, req, http.StatusBadRequest)
200+
MakeRequest(t, req, http.StatusBadRequest)
201201

202202
// missing header
203203
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
@@ -206,7 +206,7 @@ func TestAccessTokenExchangeWithBasicAuth(t *testing.T) {
206206
"code": "authcode",
207207
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt", // test PKCE additionally
208208
})
209-
resp = MakeRequest(t, req, http.StatusBadRequest)
209+
MakeRequest(t, req, http.StatusBadRequest)
210210
}
211211

212212
func TestRefreshTokenInvalidation(t *testing.T) {

Diff for: integrations/repo_fork_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func testRepoFork(t *testing.T, session *TestSession, ownerName, repoName, forkO
4545
"uid": fmt.Sprintf("%d", forkOwner.ID),
4646
"repo_name": forkRepoName,
4747
})
48-
resp = session.MakeRequest(t, req, http.StatusSeeOther)
48+
session.MakeRequest(t, req, http.StatusSeeOther)
4949

5050
// Step4: check the existence of the forked repo
5151
req = NewRequestf(t, "GET", "/%s/%s", forkOwnerName, forkRepoName)

Diff for: integrations/repo_generate_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func testRepoGenerate(t *testing.T, session *TestSession, templateOwnerName, tem
4646
"repo_name": generateRepoName,
4747
"git_content": "true",
4848
})
49-
resp = session.MakeRequest(t, req, http.StatusSeeOther)
49+
session.MakeRequest(t, req, http.StatusSeeOther)
5050

5151
// Step4: check the existence of the generated repo
5252
req = NewRequestf(t, "GET", "/%s/%s", generateOwnerName, generateRepoName)

Diff for: integrations/user_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,6 @@ func TestListStopWatches(t *testing.T) {
245245
assert.EqualValues(t, issue.Title, apiWatches[0].IssueTitle)
246246
assert.EqualValues(t, repo.Name, apiWatches[0].RepoName)
247247
assert.EqualValues(t, repo.OwnerName, apiWatches[0].RepoOwnerName)
248-
assert.Greater(t, int64(apiWatches[0].Seconds), int64(0))
248+
assert.Greater(t, apiWatches[0].Seconds, int64(0))
249249
}
250250
}

Diff for: models/action.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ func DeleteOldActions(olderThan time.Duration) (err error) {
459459
}
460460

461461
_, err = db.GetEngine(db.DefaultContext).Where("created_unix < ?", time.Now().Add(-olderThan).Unix()).Delete(&Action{})
462-
return
462+
return err
463463
}
464464

465465
func notifyWatchers(ctx context.Context, actions ...*Action) error {

Diff for: models/admin/notice.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,5 @@ func DeleteOldSystemNotices(olderThan time.Duration) (err error) {
142142
}
143143

144144
_, err = db.GetEngine(db.DefaultContext).Where("created_unix < ?", time.Now().Add(-olderThan).Unix()).Delete(&Notice{})
145-
return
145+
return err
146146
}

Diff for: models/asymkey/gpg_key_commit_verification.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -520,5 +520,5 @@ func CalculateTrustStatus(verification *CommitVerification, repoTrustModel repo_
520520
}
521521
}
522522

523-
return
523+
return err
524524
}

Diff for: models/asymkey/ssh_key_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ func TestFromOpenSSH(t *testing.T) {
317317
td := t.TempDir()
318318

319319
data := []byte("hello, ssh world")
320-
dataPath := write(t, []byte(data), td, "data")
320+
dataPath := write(t, data, td, "data")
321321

322322
privPath := write(t, []byte(tt.priv), td, "id")
323323
write(t, []byte(tt.pub), td, "id.pub")
@@ -372,14 +372,14 @@ func TestToOpenSSH(t *testing.T) {
372372
td := t.TempDir()
373373

374374
data := []byte("hello, ssh world")
375-
write(t, []byte(data), td, "data")
375+
write(t, data, td, "data")
376376

377377
armored, err := sshsig.Sign([]byte(tt.priv), bytes.NewReader(data), "file")
378378
if err != nil {
379379
t.Fatal(err)
380380
}
381381

382-
sigPath := write(t, []byte(armored), td, "oursig")
382+
sigPath := write(t, armored, td, "oursig")
383383

384384
// Create an allowed_signers file with two keys to check against.
385385
allowedSigner := "[email protected] " + tt.pub + "\n"

Diff for: models/auth/oauth2.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func GetOAuth2ApplicationByClientID(ctx context.Context, clientID string) (app *
123123
if !has {
124124
return nil, ErrOAuthClientIDInvalid{ClientID: clientID}
125125
}
126-
return
126+
return app, err
127127
}
128128

129129
// GetOAuth2ApplicationByID returns the oauth2 application with the given id. Returns an error if not found.
@@ -143,7 +143,7 @@ func GetOAuth2ApplicationByID(ctx context.Context, id int64) (app *OAuth2Applica
143143
func GetOAuth2ApplicationsByUserID(ctx context.Context, userID int64) (apps []*OAuth2Application, err error) {
144144
apps = make([]*OAuth2Application, 0)
145145
err = db.GetEngine(ctx).Where("uid = ?", userID).Find(&apps)
146-
return
146+
return apps, err
147147
}
148148

149149
// CreateOAuth2ApplicationOptions holds options to create an oauth2 application
@@ -300,7 +300,7 @@ func (code *OAuth2AuthorizationCode) GenerateRedirectURI(state string) (redirect
300300
}
301301
q.Set("code", code.Code)
302302
redirect.RawQuery = q.Encode()
303-
return
303+
return redirect, err
304304
}
305305

306306
// Invalidate deletes the auth code from the database to invalidate this code
@@ -430,7 +430,7 @@ func GetOAuth2GrantByID(ctx context.Context, id int64) (grant *OAuth2Grant, err
430430
} else if !has {
431431
return nil, nil
432432
}
433-
return
433+
return grant, err
434434
}
435435

436436
// GetOAuth2GrantsByUserID lists all grants of a certain user

Diff for: models/db/engine.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,5 +285,5 @@ func DeleteAllRecords(tableName string) error {
285285
// GetMaxID will return max id of the table
286286
func GetMaxID(beanOrTableName interface{}) (maxID int64, err error) {
287287
_, err = x.Select("MAX(id)").Table(beanOrTableName).Get(&maxID)
288-
return
288+
return maxID, err
289289
}

Diff for: models/db/index.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func UpsertResourceIndex(ctx context.Context, tableName string, groupID int64) (
4444
default:
4545
return fmt.Errorf("database type not supported")
4646
}
47-
return
47+
return err
4848
}
4949

5050
var (

Diff for: models/db/list_options.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (opts *ListOptions) GetSkipTake() (skip, take int) {
5858
func (opts *ListOptions) GetStartEnd() (start, end int) {
5959
start, take := opts.GetSkipTake()
6060
end = start + take
61-
return
61+
return start, end
6262
}
6363

6464
// SetDefaultValues sets default values

Diff for: models/db/sql_postgres_with_schema.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (d *postgresSchemaDriver) Open(name string) (driver.Conn, error) {
4444
_, err := execer.Exec(`SELECT set_config(
4545
'search_path',
4646
$1 || ',' || current_setting('search_path'),
47-
false)`, []driver.Value{schemaValue}) //nolint
47+
false)`, []driver.Value{schemaValue})
4848
if err != nil {
4949
_ = conn.Close()
5050
return nil, err

Diff for: models/git/branches.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ func updateApprovalWhitelist(ctx context.Context, repo *repo_model.Repository, c
363363
whitelist = append(whitelist, userID)
364364
}
365365

366-
return
366+
return whitelist, err
367367
}
368368

369369
// updateUserWhitelist checks whether the user whitelist changed and returns a whitelist with
@@ -392,7 +392,7 @@ func updateUserWhitelist(ctx context.Context, repo *repo_model.Repository, curre
392392
whitelist = append(whitelist, userID)
393393
}
394394

395-
return
395+
return whitelist, err
396396
}
397397

398398
// updateTeamWhitelist checks whether the team whitelist changed and returns a whitelist with
@@ -415,7 +415,7 @@ func updateTeamWhitelist(ctx context.Context, repo *repo_model.Repository, curre
415415
}
416416
}
417417

418-
return
418+
return whitelist, err
419419
}
420420

421421
// DeleteProtectedBranch removes ProtectedBranch relation between the user and repository.
@@ -539,7 +539,7 @@ func FindRenamedBranch(repoID int64, from string) (branch *RenamedBranch, exist
539539
}
540540
exist, err = db.GetEngine(db.DefaultContext).Get(branch)
541541

542-
return
542+
return branch, exist, err
543543
}
544544

545545
// RenameBranch rename a branch

Diff for: models/git/commit_status.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func upsertCommitStatusIndex(ctx context.Context, repoID int64, sha string) (err
7474
default:
7575
return fmt.Errorf("database type not supported")
7676
}
77-
return
77+
return err
7878
}
7979

8080
// GetNextCommitStatusIndex retried 3 times to generate a resource index

Diff for: models/issues/assignees.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (issue *Issue) LoadAssignees(ctx context.Context) (err error) {
4242
if len(issue.Assignees) > 0 {
4343
issue.Assignee = issue.Assignees[0]
4444
}
45-
return
45+
return err
4646
}
4747

4848
// GetAssigneeIDsByIssue returns the IDs of users assigned to an issue
@@ -167,5 +167,5 @@ func MakeIDsFromAPIAssigneesToAdd(oneAssignee string, multipleAssignees []string
167167
// Get the IDs of all assignees
168168
assigneeIDs, err = user_model.GetUserIDsByNames(requestAssignees, false)
169169

170-
return
170+
return assigneeIDs, err
171171
}

0 commit comments

Comments
 (0)