loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <engzerjun@gmail.com>

authored by

Eng Zer Jun and committed by
GitHub
13d5d2e7 6cdeb779

+31 -45
+21 -29
modules/git/repo_commit.go
··· 116 116 c.AddArguments("-i") 117 117 118 118 // 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) 123 121 } 124 122 125 123 // 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) 130 126 } 131 127 132 128 // add time constraints if present in search query ··· 150 146 151 147 // add remaining keywords from search string 152 148 // 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) 157 151 } 158 152 159 153 // search for commits matching given constraints and keywords in commit msg ··· 168 162 169 163 // if there are any keywords (ie not committer:, author:, time:) 170 164 // 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) 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) 181 174 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') 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 189 179 } 180 + stdout = append(stdout, hashMatching...) 181 + stdout = append(stdout, '\n') 190 182 } 191 183 } 192 184
+3 -5
modules/setting/config_provider.go
··· 213 213 } 214 214 } 215 215 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) 221 219 } 222 220 } 223 221
+2 -4
routers/web/repo/issue.go
··· 965 965 966 966 _, templateErrs := issue_service.GetTemplatesFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo) 967 967 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 972 970 } 973 971 if ctx.Written() { 974 972 return
+5 -7
services/pull/pull.go
··· 152 152 if issue.Milestone != nil { 153 153 notify_service.IssueChangeMilestone(ctx, issue.Poster, issue, 0) 154 154 } 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 162 159 } 160 + notify_service.IssueChangeAssignee(ctx, issue.Poster, issue, assignee, false, assigneeCommentMap[assigneeID]) 163 161 } 164 162 165 163 return nil