Skip to content

Commit fc2d75f

Browse files
silverwindlunnydelvh
authored
Enable unparam linter (go-gitea#31277)
Enable [unparam](https://github.com/mvdan/unparam) linter. Often I could not tell the intention why param is unused, so I put `//nolint` for those cases like webhook request creation functions never using `ctx`. --------- Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: delvh <[email protected]>
1 parent 4bf848a commit fc2d75f

File tree

27 files changed

+86
-135
lines changed

27 files changed

+86
-135
lines changed

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ linters:
2222
- typecheck
2323
- unconvert
2424
- unused
25+
- unparam
2526
- wastedassign
2627

2728
run:

models/dbfs/dbfile.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,15 @@ func fileTimestampToTime(timestamp int64) time.Time {
215215
return time.UnixMicro(timestamp)
216216
}
217217

218-
func (f *file) loadMetaByPath() (*dbfsMeta, error) {
218+
func (f *file) loadMetaByPath() error {
219219
var fileMeta dbfsMeta
220220
if ok, err := db.GetEngine(f.ctx).Where("full_path = ?", f.fullPath).Get(&fileMeta); err != nil {
221-
return nil, err
221+
return err
222222
} else if ok {
223223
f.metaID = fileMeta.ID
224224
f.blockSize = fileMeta.BlockSize
225-
return &fileMeta, nil
226225
}
227-
return nil, nil
226+
return nil
228227
}
229228

230229
func (f *file) open(flag int) (err error) {
@@ -288,10 +287,7 @@ func (f *file) createEmpty() error {
288287
if err != nil {
289288
return err
290289
}
291-
if _, err = f.loadMetaByPath(); err != nil {
292-
return err
293-
}
294-
return nil
290+
return f.loadMetaByPath()
295291
}
296292

297293
func (f *file) truncate() error {
@@ -368,8 +364,5 @@ func buildPath(path string) string {
368364
func newDbFile(ctx context.Context, path string) (*file, error) {
369365
path = buildPath(path)
370366
f := &file{ctx: ctx, fullPath: path, blockSize: defaultFileBlockSize}
371-
if _, err := f.loadMetaByPath(); err != nil {
372-
return nil, err
373-
}
374-
return f, nil
367+
return f, f.loadMetaByPath()
375368
}

models/issues/issue_search.go

+20-31
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,19 @@ func applySorts(sess *xorm.Session, sortType string, priorityRepoID int64) {
9999
}
100100
}
101101

102-
func applyLimit(sess *xorm.Session, opts *IssuesOptions) *xorm.Session {
102+
func applyLimit(sess *xorm.Session, opts *IssuesOptions) {
103103
if opts.Paginator == nil || opts.Paginator.IsListAll() {
104-
return sess
104+
return
105105
}
106106

107107
start := 0
108108
if opts.Paginator.Page > 1 {
109109
start = (opts.Paginator.Page - 1) * opts.Paginator.PageSize
110110
}
111111
sess.Limit(opts.Paginator.PageSize, start)
112-
113-
return sess
114112
}
115113

116-
func applyLabelsCondition(sess *xorm.Session, opts *IssuesOptions) *xorm.Session {
114+
func applyLabelsCondition(sess *xorm.Session, opts *IssuesOptions) {
117115
if len(opts.LabelIDs) > 0 {
118116
if opts.LabelIDs[0] == 0 {
119117
sess.Where("issue.id NOT IN (SELECT issue_id FROM issue_label)")
@@ -136,11 +134,9 @@ func applyLabelsCondition(sess *xorm.Session, opts *IssuesOptions) *xorm.Session
136134
if len(opts.ExcludedLabelNames) > 0 {
137135
sess.And(builder.NotIn("issue.id", BuildLabelNamesIssueIDsCondition(opts.ExcludedLabelNames)))
138136
}
139-
140-
return sess
141137
}
142138

143-
func applyMilestoneCondition(sess *xorm.Session, opts *IssuesOptions) *xorm.Session {
139+
func applyMilestoneCondition(sess *xorm.Session, opts *IssuesOptions) {
144140
if len(opts.MilestoneIDs) == 1 && opts.MilestoneIDs[0] == db.NoConditionID {
145141
sess.And("issue.milestone_id = 0")
146142
} else if len(opts.MilestoneIDs) > 0 {
@@ -153,11 +149,9 @@ func applyMilestoneCondition(sess *xorm.Session, opts *IssuesOptions) *xorm.Sess
153149
From("milestone").
154150
Where(builder.In("name", opts.IncludeMilestones)))
155151
}
156-
157-
return sess
158152
}
159153

160-
func applyProjectCondition(sess *xorm.Session, opts *IssuesOptions) *xorm.Session {
154+
func applyProjectCondition(sess *xorm.Session, opts *IssuesOptions) {
161155
if opts.ProjectID > 0 { // specific project
162156
sess.Join("INNER", "project_issue", "issue.id = project_issue.issue_id").
163157
And("project_issue.project_id=?", opts.ProjectID)
@@ -166,21 +160,19 @@ func applyProjectCondition(sess *xorm.Session, opts *IssuesOptions) *xorm.Sessio
166160
}
167161
// opts.ProjectID == 0 means all projects,
168162
// do not need to apply any condition
169-
return sess
170163
}
171164

172-
func applyProjectColumnCondition(sess *xorm.Session, opts *IssuesOptions) *xorm.Session {
165+
func applyProjectColumnCondition(sess *xorm.Session, opts *IssuesOptions) {
173166
// opts.ProjectColumnID == 0 means all project columns,
174167
// do not need to apply any condition
175168
if opts.ProjectColumnID > 0 {
176169
sess.In("issue.id", builder.Select("issue_id").From("project_issue").Where(builder.Eq{"project_board_id": opts.ProjectColumnID}))
177170
} else if opts.ProjectColumnID == db.NoConditionID {
178171
sess.In("issue.id", builder.Select("issue_id").From("project_issue").Where(builder.Eq{"project_board_id": 0}))
179172
}
180-
return sess
181173
}
182174

183-
func applyRepoConditions(sess *xorm.Session, opts *IssuesOptions) *xorm.Session {
175+
func applyRepoConditions(sess *xorm.Session, opts *IssuesOptions) {
184176
if len(opts.RepoIDs) == 1 {
185177
opts.RepoCond = builder.Eq{"issue.repo_id": opts.RepoIDs[0]}
186178
} else if len(opts.RepoIDs) > 1 {
@@ -195,10 +187,9 @@ func applyRepoConditions(sess *xorm.Session, opts *IssuesOptions) *xorm.Session
195187
if opts.RepoCond != nil {
196188
sess.And(opts.RepoCond)
197189
}
198-
return sess
199190
}
200191

201-
func applyConditions(sess *xorm.Session, opts *IssuesOptions) *xorm.Session {
192+
func applyConditions(sess *xorm.Session, opts *IssuesOptions) {
202193
if len(opts.IssueIDs) > 0 {
203194
sess.In("issue.id", opts.IssueIDs)
204195
}
@@ -261,8 +252,6 @@ func applyConditions(sess *xorm.Session, opts *IssuesOptions) *xorm.Session {
261252
if opts.User != nil {
262253
sess.And(issuePullAccessibleRepoCond("issue.repo_id", opts.User.ID, opts.Org, opts.Team, opts.IsPull.Value()))
263254
}
264-
265-
return sess
266255
}
267256

268257
// teamUnitsRepoCond returns query condition for those repo id in the special org team with special units access
@@ -339,22 +328,22 @@ func issuePullAccessibleRepoCond(repoIDstr string, userID int64, org *organizati
339328
return cond
340329
}
341330

342-
func applyAssigneeCondition(sess *xorm.Session, assigneeID int64) *xorm.Session {
343-
return sess.Join("INNER", "issue_assignees", "issue.id = issue_assignees.issue_id").
331+
func applyAssigneeCondition(sess *xorm.Session, assigneeID int64) {
332+
sess.Join("INNER", "issue_assignees", "issue.id = issue_assignees.issue_id").
344333
And("issue_assignees.assignee_id = ?", assigneeID)
345334
}
346335

347-
func applyPosterCondition(sess *xorm.Session, posterID int64) *xorm.Session {
348-
return sess.And("issue.poster_id=?", posterID)
336+
func applyPosterCondition(sess *xorm.Session, posterID int64) {
337+
sess.And("issue.poster_id=?", posterID)
349338
}
350339

351-
func applyMentionedCondition(sess *xorm.Session, mentionedID int64) *xorm.Session {
352-
return sess.Join("INNER", "issue_user", "issue.id = issue_user.issue_id").
340+
func applyMentionedCondition(sess *xorm.Session, mentionedID int64) {
341+
sess.Join("INNER", "issue_user", "issue.id = issue_user.issue_id").
353342
And("issue_user.is_mentioned = ?", true).
354343
And("issue_user.uid = ?", mentionedID)
355344
}
356345

357-
func applyReviewRequestedCondition(sess *xorm.Session, reviewRequestedID int64) *xorm.Session {
346+
func applyReviewRequestedCondition(sess *xorm.Session, reviewRequestedID int64) {
358347
existInTeamQuery := builder.Select("team_user.team_id").
359348
From("team_user").
360349
Where(builder.Eq{"team_user.uid": reviewRequestedID})
@@ -375,11 +364,11 @@ func applyReviewRequestedCondition(sess *xorm.Session, reviewRequestedID int64)
375364
),
376365
builder.In("review.id", maxReview),
377366
))
378-
return sess.Where("issue.poster_id <> ?", reviewRequestedID).
367+
sess.Where("issue.poster_id <> ?", reviewRequestedID).
379368
And(builder.In("issue.id", subQuery))
380369
}
381370

382-
func applyReviewedCondition(sess *xorm.Session, reviewedID int64) *xorm.Session {
371+
func applyReviewedCondition(sess *xorm.Session, reviewedID int64) {
383372
// Query for pull requests where you are a reviewer or commenter, excluding
384373
// any pull requests already returned by the review requested filter.
385374
notPoster := builder.Neq{"issue.poster_id": reviewedID}
@@ -406,11 +395,11 @@ func applyReviewedCondition(sess *xorm.Session, reviewedID int64) *xorm.Session
406395
builder.In("type", CommentTypeComment, CommentTypeCode, CommentTypeReview),
407396
)),
408397
)
409-
return sess.And(notPoster, builder.Or(reviewed, commented))
398+
sess.And(notPoster, builder.Or(reviewed, commented))
410399
}
411400

412-
func applySubscribedCondition(sess *xorm.Session, subscriberID int64) *xorm.Session {
413-
return sess.And(
401+
func applySubscribedCondition(sess *xorm.Session, subscriberID int64) {
402+
sess.And(
414403
builder.
415404
NotIn("issue.id",
416405
builder.Select("issue_id").

models/issues/pull_list.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type PullRequestsOptions struct {
2828
MilestoneID int64
2929
}
3030

31-
func listPullRequestStatement(ctx context.Context, baseRepoID int64, opts *PullRequestsOptions) (*xorm.Session, error) {
31+
func listPullRequestStatement(ctx context.Context, baseRepoID int64, opts *PullRequestsOptions) *xorm.Session {
3232
sess := db.GetEngine(ctx).Where("pull_request.base_repo_id=?", baseRepoID)
3333

3434
sess.Join("INNER", "issue", "pull_request.issue_id = issue.id")
@@ -46,7 +46,7 @@ func listPullRequestStatement(ctx context.Context, baseRepoID int64, opts *PullR
4646
sess.And("issue.milestone_id=?", opts.MilestoneID)
4747
}
4848

49-
return sess, nil
49+
return sess
5050
}
5151

5252
// GetUnmergedPullRequestsByHeadInfo returns all pull requests that are open and has not been merged
@@ -130,23 +130,15 @@ func PullRequests(ctx context.Context, baseRepoID int64, opts *PullRequestsOptio
130130
opts.Page = 1
131131
}
132132

133-
countSession, err := listPullRequestStatement(ctx, baseRepoID, opts)
134-
if err != nil {
135-
log.Error("listPullRequestStatement: %v", err)
136-
return nil, 0, err
137-
}
133+
countSession := listPullRequestStatement(ctx, baseRepoID, opts)
138134
maxResults, err := countSession.Count(new(PullRequest))
139135
if err != nil {
140136
log.Error("Count PRs: %v", err)
141137
return nil, maxResults, err
142138
}
143139

144-
findSession, err := listPullRequestStatement(ctx, baseRepoID, opts)
140+
findSession := listPullRequestStatement(ctx, baseRepoID, opts)
145141
applySorts(findSession, opts.SortType, 0)
146-
if err != nil {
147-
log.Error("listPullRequestStatement: %v", err)
148-
return nil, maxResults, err
149-
}
150142
findSession = db.SetSessionPagination(findSession, opts)
151143
prs := make([]*PullRequest, 0, opts.PageSize)
152144
return prs, maxResults, findSession.Find(&prs)

modules/auth/password/hash/common.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func parseIntParam(value, param, algorithmName, config string, previousErr error
1818
return parsed, previousErr // <- Keep the previous error as this function should still return an error once everything has been checked if any call failed
1919
}
2020

21-
func parseUIntParam(value, param, algorithmName, config string, previousErr error) (uint64, error) {
21+
func parseUIntParam(value, param, algorithmName, config string, previousErr error) (uint64, error) { //nolint:unparam
2222
parsed, err := strconv.ParseUint(value, 10, 64)
2323
if err != nil {
2424
log.Error("invalid integer for %s representation in %s hash spec %s", param, algorithmName, config)

modules/packages/cran/metadata.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,6 @@ func ParseDescription(r io.Reader) (*Package, error) {
185185
}
186186

187187
func setField(p *Package, data string) error {
188-
const listDelimiter = ", "
189-
190188
if data == "" {
191189
return nil
192190
}
@@ -215,28 +213,28 @@ func setField(p *Package, data string) error {
215213
case "Description":
216214
p.Metadata.Description = value
217215
case "URL":
218-
p.Metadata.ProjectURL = splitAndTrim(value, listDelimiter)
216+
p.Metadata.ProjectURL = splitAndTrim(value)
219217
case "License":
220218
p.Metadata.License = value
221219
case "Author":
222-
p.Metadata.Authors = splitAndTrim(authorReplacePattern.ReplaceAllString(value, ""), listDelimiter)
220+
p.Metadata.Authors = splitAndTrim(authorReplacePattern.ReplaceAllString(value, ""))
223221
case "Depends":
224-
p.Metadata.Depends = splitAndTrim(value, listDelimiter)
222+
p.Metadata.Depends = splitAndTrim(value)
225223
case "Imports":
226-
p.Metadata.Imports = splitAndTrim(value, listDelimiter)
224+
p.Metadata.Imports = splitAndTrim(value)
227225
case "Suggests":
228-
p.Metadata.Suggests = splitAndTrim(value, listDelimiter)
226+
p.Metadata.Suggests = splitAndTrim(value)
229227
case "LinkingTo":
230-
p.Metadata.LinkingTo = splitAndTrim(value, listDelimiter)
228+
p.Metadata.LinkingTo = splitAndTrim(value)
231229
case "NeedsCompilation":
232230
p.Metadata.NeedsCompilation = value == "yes"
233231
}
234232

235233
return nil
236234
}
237235

238-
func splitAndTrim(s, sep string) []string {
239-
items := strings.Split(s, sep)
236+
func splitAndTrim(s string) []string {
237+
items := strings.Split(s, ", ")
240238
for i := range items {
241239
items[i] = strings.TrimSpace(items[i])
242240
}

modules/setting/config_env.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func decodeEnvSectionKey(encoded string) (ok bool, section, key string) {
9797

9898
// decodeEnvironmentKey decode the environment key to section and key
9999
// The environment key is in the form of GITEA__SECTION__KEY or GITEA__SECTION__KEY__FILE
100-
func decodeEnvironmentKey(prefixGitea, suffixFile, envKey string) (ok bool, section, key string, useFileValue bool) {
100+
func decodeEnvironmentKey(prefixGitea, suffixFile, envKey string) (ok bool, section, key string, useFileValue bool) { //nolint:unparam
101101
if !strings.HasPrefix(envKey, prefixGitea) {
102102
return false, "", "", false
103103
}

modules/setting/storage.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ const (
161161
targetSecIsSec // target section is from the name seciont [name]
162162
)
163163

164-
func getStorageSectionByType(rootCfg ConfigProvider, typ string) (ConfigSection, targetSecType, error) {
164+
func getStorageSectionByType(rootCfg ConfigProvider, typ string) (ConfigSection, targetSecType, error) { //nolint:unparam
165165
targetSec, err := rootCfg.GetSection(storageSectionName + "." + typ)
166166
if err != nil {
167167
if !IsValidStorageType(StorageType(typ)) {

modules/storage/azureblob.go

+8-23
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,7 @@ func (a *AzureBlobStorage) getObjectNameFromPath(path string) string {
163163

164164
// Open opens a file
165165
func (a *AzureBlobStorage) Open(path string) (Object, error) {
166-
blobClient, err := a.getBlobClient(path)
167-
if err != nil {
168-
return nil, convertAzureBlobErr(err)
169-
}
166+
blobClient := a.getBlobClient(path)
170167
res, err := blobClient.GetProperties(a.ctx, &blob.GetPropertiesOptions{})
171168
if err != nil {
172169
return nil, convertAzureBlobErr(err)
@@ -229,10 +226,7 @@ func (a azureBlobFileInfo) Sys() any {
229226

230227
// Stat returns the stat information of the object
231228
func (a *AzureBlobStorage) Stat(path string) (os.FileInfo, error) {
232-
blobClient, err := a.getBlobClient(path)
233-
if err != nil {
234-
return nil, convertAzureBlobErr(err)
235-
}
229+
blobClient := a.getBlobClient(path)
236230
res, err := blobClient.GetProperties(a.ctx, &blob.GetPropertiesOptions{})
237231
if err != nil {
238232
return nil, convertAzureBlobErr(err)
@@ -247,20 +241,14 @@ func (a *AzureBlobStorage) Stat(path string) (os.FileInfo, error) {
247241

248242
// Delete delete a file
249243
func (a *AzureBlobStorage) Delete(path string) error {
250-
blobClient, err := a.getBlobClient(path)
251-
if err != nil {
252-
return convertAzureBlobErr(err)
253-
}
254-
_, err = blobClient.Delete(a.ctx, nil)
244+
blobClient := a.getBlobClient(path)
245+
_, err := blobClient.Delete(a.ctx, nil)
255246
return convertAzureBlobErr(err)
256247
}
257248

258249
// URL gets the redirect URL to a file. The presigned link is valid for 5 minutes.
259250
func (a *AzureBlobStorage) URL(path, name string) (*url.URL, error) {
260-
blobClient, err := a.getBlobClient(path)
261-
if err != nil {
262-
return nil, convertAzureBlobErr(err)
263-
}
251+
blobClient := a.getBlobClient(path)
264252

265253
startTime := time.Now()
266254
u, err := blobClient.GetSASURL(sas.BlobPermissions{
@@ -290,10 +278,7 @@ func (a *AzureBlobStorage) IterateObjects(dirName string, fn func(path string, o
290278
return convertAzureBlobErr(err)
291279
}
292280
for _, object := range resp.Segment.BlobItems {
293-
blobClient, err := a.getBlobClient(*object.Name)
294-
if err != nil {
295-
return convertAzureBlobErr(err)
296-
}
281+
blobClient := a.getBlobClient(*object.Name)
297282
object := &azureBlobObject{
298283
Context: a.ctx,
299284
blobClient: blobClient,
@@ -313,8 +298,8 @@ func (a *AzureBlobStorage) IterateObjects(dirName string, fn func(path string, o
313298
}
314299

315300
// Delete delete a file
316-
func (a *AzureBlobStorage) getBlobClient(path string) (*blob.Client, error) {
317-
return a.client.ServiceClient().NewContainerClient(a.cfg.Container).NewBlobClient(a.buildAzureBlobPath(path)), nil
301+
func (a *AzureBlobStorage) getBlobClient(path string) *blob.Client {
302+
return a.client.ServiceClient().NewContainerClient(a.cfg.Container).NewBlobClient(a.buildAzureBlobPath(path))
318303
}
319304

320305
func init() {

0 commit comments

Comments
 (0)