Skip to content

Commit 13d5d2e

Browse files
authored
Remove redundant len check around loop (#27464)
This pull request is a minor code cleanup. From the Go specification (https://go.dev/ref/spec#For_range): > "1. For a nil slice, the number of iterations is 0." > "3. If the map is nil, the number of iterations is 0." `len` returns 0 if the slice or map is nil (https://pkg.go.dev/builtin#len). Therefore, checking `len(v) > 0` before a loop is unnecessary. --- At the time of writing this pull request, there wasn't a lint rule that catches these issues. The closest I could find is https://staticcheck.dev/docs/checks/#S103 Signed-off-by: Eng Zer Jun <[email protected]>
1 parent 6cdeb77 commit 13d5d2e

File tree

4 files changed

+32
-46
lines changed

4 files changed

+32
-46
lines changed

modules/git/repo_commit.go

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,13 @@ func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) ([]*Co
116116
c.AddArguments("-i")
117117

118118
// add authors if present in search query
119-
if len(opts.Authors) > 0 {
120-
for _, v := range opts.Authors {
121-
c.AddOptionFormat("--author=%s", v)
122-
}
119+
for _, v := range opts.Authors {
120+
c.AddOptionFormat("--author=%s", v)
123121
}
124122

125123
// add committers if present in search query
126-
if len(opts.Committers) > 0 {
127-
for _, v := range opts.Committers {
128-
c.AddOptionFormat("--committer=%s", v)
129-
}
124+
for _, v := range opts.Committers {
125+
c.AddOptionFormat("--committer=%s", v)
130126
}
131127

132128
// add time constraints if present in search query
@@ -150,10 +146,8 @@ func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) ([]*Co
150146

151147
// add remaining keywords from search string
152148
// note this is done only for command created above
153-
if len(opts.Keywords) > 0 {
154-
for _, v := range opts.Keywords {
155-
cmd.AddOptionFormat("--grep=%s", v)
156-
}
149+
for _, v := range opts.Keywords {
150+
cmd.AddOptionFormat("--grep=%s", v)
157151
}
158152

159153
// search for commits matching given constraints and keywords in commit msg
@@ -168,25 +162,23 @@ func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) ([]*Co
168162

169163
// if there are any keywords (ie not committer:, author:, time:)
170164
// then let's iterate over them
171-
if len(opts.Keywords) > 0 {
172-
for _, v := range opts.Keywords {
173-
// ignore anything not matching a valid sha pattern
174-
if IsValidSHAPattern(v) {
175-
// create new git log command with 1 commit limit
176-
hashCmd := NewCommand(repo.Ctx, "log", "-1", prettyLogFormat)
177-
// add previous arguments except for --grep and --all
178-
addCommonSearchArgs(hashCmd)
179-
// add keyword as <commit>
180-
hashCmd.AddDynamicArguments(v)
181-
182-
// search with given constraints for commit matching sha hash of v
183-
hashMatching, _, err := hashCmd.RunStdBytes(&RunOpts{Dir: repo.Path})
184-
if err != nil || bytes.Contains(stdout, hashMatching) {
185-
continue
186-
}
187-
stdout = append(stdout, hashMatching...)
188-
stdout = append(stdout, '\n')
165+
for _, v := range opts.Keywords {
166+
// ignore anything not matching a valid sha pattern
167+
if IsValidSHAPattern(v) {
168+
// create new git log command with 1 commit limit
169+
hashCmd := NewCommand(repo.Ctx, "log", "-1", prettyLogFormat)
170+
// add previous arguments except for --grep and --all
171+
addCommonSearchArgs(hashCmd)
172+
// add keyword as <commit>
173+
hashCmd.AddDynamicArguments(v)
174+
175+
// search with given constraints for commit matching sha hash of v
176+
hashMatching, _, err := hashCmd.RunStdBytes(&RunOpts{Dir: repo.Path})
177+
if err != nil || bytes.Contains(stdout, hashMatching) {
178+
continue
189179
}
180+
stdout = append(stdout, hashMatching...)
181+
stdout = append(stdout, '\n')
190182
}
191183
}
192184

modules/setting/config_provider.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,9 @@ func NewConfigProviderFromFile(file string, extraConfigs ...string) (ConfigProvi
213213
}
214214
}
215215

216-
if len(extraConfigs) > 0 {
217-
for _, s := range extraConfigs {
218-
if err := cfg.Append([]byte(s)); err != nil {
219-
return nil, fmt.Errorf("unable to append more config: %v", err)
220-
}
216+
for _, s := range extraConfigs {
217+
if err := cfg.Append([]byte(s)); err != nil {
218+
return nil, fmt.Errorf("unable to append more config: %v", err)
221219
}
222220
}
223221

routers/web/repo/issue.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -965,10 +965,8 @@ func NewIssue(ctx *context.Context) {
965965

966966
_, templateErrs := issue_service.GetTemplatesFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo)
967967
templateLoaded, errs := setTemplateIfExists(ctx, issueTemplateKey, IssueTemplateCandidates)
968-
if len(errs) > 0 {
969-
for k, v := range errs {
970-
templateErrs[k] = v
971-
}
968+
for k, v := range errs {
969+
templateErrs[k] = v
972970
}
973971
if ctx.Written() {
974972
return

services/pull/pull.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,12 @@ func NewPullRequest(ctx context.Context, repo *repo_model.Repository, issue *iss
152152
if issue.Milestone != nil {
153153
notify_service.IssueChangeMilestone(ctx, issue.Poster, issue, 0)
154154
}
155-
if len(assigneeIDs) > 0 {
156-
for _, assigneeID := range assigneeIDs {
157-
assignee, err := user_model.GetUserByID(ctx, assigneeID)
158-
if err != nil {
159-
return ErrDependenciesLeft
160-
}
161-
notify_service.IssueChangeAssignee(ctx, issue.Poster, issue, assignee, false, assigneeCommentMap[assigneeID])
155+
for _, assigneeID := range assigneeIDs {
156+
assignee, err := user_model.GetUserByID(ctx, assigneeID)
157+
if err != nil {
158+
return ErrDependenciesLeft
162159
}
160+
notify_service.IssueChangeAssignee(ctx, issue.Poster, issue, assignee, false, assigneeCommentMap[assigneeID])
163161
}
164162

165163
return nil

0 commit comments

Comments
 (0)