Enable gocritic `equalFold` and fix issues (#34952)

Continuation of https://github.com/go-gitea/gitea/pull/34678.

---------

Signed-off-by: silverwind <me@silverwind.io>
pull/34970/head^2
silverwind 2025-07-06 18:53:34 +07:00 committed by GitHub
parent ba943fb773
commit 95a935aca0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 21 additions and 22 deletions

@ -50,6 +50,8 @@ linters:
require-explanation: true require-explanation: true
require-specific: true require-specific: true
gocritic: gocritic:
enabled-checks:
- equalFold
disabled-checks: disabled-checks:
- ifElseChain - ifElseChain
- singleCaseSwitch # Every time this occurred in the code, there was no other way. - singleCaseSwitch # Every time this occurred in the code, there was no other way.

@ -157,18 +157,17 @@ func UpdateLanguageStats(ctx context.Context, repo *Repository, commitID string,
for lang, size := range stats { for lang, size := range stats {
if size > s { if size > s {
s = size s = size
topLang = strings.ToLower(lang) topLang = lang
} }
} }
for lang, size := range stats { for lang, size := range stats {
upd := false upd := false
llang := strings.ToLower(lang)
for _, s := range oldstats { for _, s := range oldstats {
// Update already existing language // Update already existing language
if strings.ToLower(s.Language) == llang { if strings.EqualFold(s.Language, lang) {
s.CommitID = commitID s.CommitID = commitID
s.IsPrimary = llang == topLang s.IsPrimary = lang == topLang
s.Size = size s.Size = size
if _, err := sess.ID(s.ID).Cols("`commit_id`", "`size`", "`is_primary`").Update(s); err != nil { if _, err := sess.ID(s.ID).Cols("`commit_id`", "`size`", "`is_primary`").Update(s); err != nil {
return err return err
@ -182,7 +181,7 @@ func UpdateLanguageStats(ctx context.Context, repo *Repository, commitID string,
if err := db.Insert(ctx, &LanguageStat{ if err := db.Insert(ctx, &LanguageStat{
RepoID: repo.ID, RepoID: repo.ID,
CommitID: commitID, CommitID: commitID,
IsPrimary: llang == topLang, IsPrimary: lang == topLang,
Language: lang, Language: lang,
Size: size, Size: size,
}); err != nil { }); err != nil {

@ -91,8 +91,7 @@ func (r *stripRenderer) processAutoLink(w io.Writer, link []byte) {
} }
// Note: we're not attempting to match the URL scheme (http/https) // Note: we're not attempting to match the URL scheme (http/https)
host := strings.ToLower(u.Host) if u.Host != "" && !strings.EqualFold(u.Host, r.localhost.Host) {
if host != "" && host != strings.ToLower(r.localhost.Host) {
// Process out of band // Process out of band
r.links = append(r.links, linkStr) r.links = append(r.links, linkStr)
return return

@ -88,7 +88,7 @@ func ParsePackage(r io.Reader) (*Package, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else if strings.ToLower(hd.Name) == "readme.md" { } else if strings.EqualFold(hd.Name, "readme.md") {
data, err := io.ReadAll(tr) data, err := io.ReadAll(tr)
if err != nil { if err != nil {
return nil, err return nil, err

@ -62,11 +62,11 @@ func (c logCompression) IsValid() bool {
} }
func (c logCompression) IsNone() bool { func (c logCompression) IsNone() bool {
return strings.ToLower(string(c)) == "none" return string(c) == "none"
} }
func (c logCompression) IsZstd() bool { func (c logCompression) IsZstd() bool {
return c == "" || strings.ToLower(string(c)) == "zstd" return c == "" || string(c) == "zstd"
} }
func loadActionsFrom(rootCfg ConfigProvider) error { func loadActionsFrom(rootCfg ConfigProvider) error {

@ -12,8 +12,7 @@ import (
// SliceContainsString sequential searches if string exists in slice. // SliceContainsString sequential searches if string exists in slice.
func SliceContainsString(slice []string, target string, insensitive ...bool) bool { func SliceContainsString(slice []string, target string, insensitive ...bool) bool {
if len(insensitive) != 0 && insensitive[0] { if len(insensitive) != 0 && insensitive[0] {
target = strings.ToLower(target) return slices.ContainsFunc(slice, func(t string) bool { return strings.EqualFold(t, target) })
return slices.ContainsFunc(slice, func(t string) bool { return strings.ToLower(t) == target })
} }
return slices.Contains(slice, target) return slices.Contains(slice, target)

@ -59,7 +59,7 @@ func TimeEstimateParse(timeStr string) (int64, error) {
unit := timeStr[match[4]:match[5]] unit := timeStr[match[4]:match[5]]
found := false found := false
for _, u := range timeStrGlobalVars().units { for _, u := range timeStrGlobalVars().units {
if strings.ToLower(unit) == u.name { if strings.EqualFold(unit, u.name) {
total += amount * u.num total += amount * u.num
found = true found = true
break break

@ -145,7 +145,7 @@ func repoAssignment() func(ctx *context.APIContext) {
) )
// Check if the user is the same as the repository owner. // Check if the user is the same as the repository owner.
if ctx.IsSigned && ctx.Doer.LowerName == strings.ToLower(userName) { if ctx.IsSigned && strings.EqualFold(ctx.Doer.LowerName, userName) {
owner = ctx.Doer owner = ctx.Doer
} else { } else {
owner, err = user_model.GetUserByName(ctx, userName) owner, err = user_model.GetUserByName(ctx, userName)

@ -276,7 +276,7 @@ func GetRepoPermissions(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden" // "$ref": "#/responses/forbidden"
collaboratorUsername := ctx.PathParam("collaborator") collaboratorUsername := ctx.PathParam("collaborator")
if !ctx.Doer.IsAdmin && ctx.Doer.LowerName != strings.ToLower(collaboratorUsername) && !ctx.IsUserRepoAdmin() { if !ctx.Doer.IsAdmin && !strings.EqualFold(ctx.Doer.LowerName, collaboratorUsername) && !ctx.IsUserRepoAdmin() {
ctx.APIError(http.StatusForbidden, "Only admins can query all permissions, repo admins can query all repo permissions, collaborators can query only their own") ctx.APIError(http.StatusForbidden, "Only admins can query all permissions, repo admins can query all repo permissions, collaborators can query only their own")
return return
} }

@ -669,7 +669,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
newRepoName = *opts.Name newRepoName = *opts.Name
} }
// Check if repository name has been changed and not just a case change // Check if repository name has been changed and not just a case change
if repo.LowerName != strings.ToLower(newRepoName) { if !strings.EqualFold(repo.LowerName, newRepoName) {
if err := repo_service.ChangeRepositoryName(ctx, ctx.Doer, repo, newRepoName); err != nil { if err := repo_service.ChangeRepositoryName(ctx, ctx.Doer, repo, newRepoName); err != nil {
switch { switch {
case repo_model.IsErrRepoAlreadyExist(err): case repo_model.IsErrRepoAlreadyExist(err):

@ -165,7 +165,7 @@ func handleSettingsPostUpdate(ctx *context.Context) {
newRepoName := form.RepoName newRepoName := form.RepoName
// Check if repository name has been changed. // Check if repository name has been changed.
if repo.LowerName != strings.ToLower(newRepoName) { if !strings.EqualFold(repo.LowerName, newRepoName) {
// Close the GitRepo if open // Close the GitRepo if open
if ctx.Repo.GitRepo != nil { if ctx.Repo.GitRepo != nil {
ctx.Repo.GitRepo.Close() ctx.Repo.GitRepo.Close()

@ -241,7 +241,7 @@ func (source *Source) listLdapGroupMemberships(l *ldap.Conn, uid string, applyGr
} }
func (source *Source) getUserAttributeListedInGroup(entry *ldap.Entry) string { func (source *Source) getUserAttributeListedInGroup(entry *ldap.Entry) string {
if strings.ToLower(source.UserUID) == "dn" { if strings.EqualFold(source.UserUID, "dn") {
return entry.DN return entry.DN
} }

@ -208,7 +208,7 @@ func OrgAssignment(opts OrgAssignmentOptions) func(ctx *Context) {
if len(teamName) > 0 { if len(teamName) > 0 {
teamExists := false teamExists := false
for _, team := range ctx.Org.Teams { for _, team := range ctx.Org.Teams {
if team.LowerName == strings.ToLower(teamName) { if strings.EqualFold(team.LowerName, teamName) {
teamExists = true teamExists = true
ctx.Org.Team = team ctx.Org.Team = team
ctx.Org.IsTeamMember = true ctx.Org.IsTeamMember = true

@ -429,7 +429,7 @@ func RepoAssignment(ctx *Context) {
} }
// Check if the user is the same as the repository owner // Check if the user is the same as the repository owner
if ctx.IsSigned && ctx.Doer.LowerName == strings.ToLower(userName) { if ctx.IsSigned && strings.EqualFold(ctx.Doer.LowerName, userName) {
ctx.Repo.Owner = ctx.Doer ctx.Repo.Owner = ctx.Doer
} else { } else {
ctx.Repo.Owner, err = user_model.GetUserByName(ctx, userName) ctx.Repo.Owner, err = user_model.GetUserByName(ctx, userName)

@ -61,7 +61,7 @@ func UserAssignmentAPI() func(ctx *APIContext) {
func userAssignment(ctx *Base, doer *user_model.User, errCb func(int, any)) (contextUser *user_model.User) { func userAssignment(ctx *Base, doer *user_model.User, errCb func(int, any)) (contextUser *user_model.User) {
username := ctx.PathParam("username") username := ctx.PathParam("username")
if doer != nil && doer.LowerName == strings.ToLower(username) { if doer != nil && strings.EqualFold(doer.LowerName, username) {
contextUser = doer contextUser = doer
} else { } else {
var err error var err error

@ -144,7 +144,7 @@ func CleanGitTreePath(name string) string {
name = util.PathJoinRel(name) name = util.PathJoinRel(name)
// Git disallows any filenames to have a .git directory in them. // Git disallows any filenames to have a .git directory in them.
for part := range strings.SplitSeq(name, "/") { for part := range strings.SplitSeq(name, "/") {
if strings.ToLower(part) == ".git" { if strings.EqualFold(part, ".git") {
return "" return ""
} }
} }