Skip to content

Enable gocritic equalFold and autofix issues #34678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ linters:
- pkg: gitea.com/go-chi/cache
desc: do not use the go-chi cache package, use gitea's cache system
gocritic:
enabled-checks:
- equalFold
disabled-checks:
- ifElseChain
- singleCaseSwitch # Every time this occurred in the code, there was no other way.
Expand Down
2 changes: 1 addition & 1 deletion models/repo/language_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func UpdateLanguageStats(ctx context.Context, repo *Repository, commitID string,
llang := strings.ToLower(lang)
for _, s := range oldstats {
// Update already existing language
if strings.ToLower(s.Language) == llang {
if strings.EqualFold(s.Language, llang) {
s.CommitID = commitID
s.IsPrimary = llang == topLang
s.Size = size
Expand Down
2 changes: 1 addition & 1 deletion modules/markup/mdstripper/mdstripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (r *stripRenderer) processAutoLink(w io.Writer, link []byte) {

// Note: we're not attempting to match the URL scheme (http/https)
host := strings.ToLower(u.Host)
if host != "" && host != strings.ToLower(r.localhost.Host) {
if host != "" && !strings.EqualFold(host, r.localhost.Host) {
// Process out of band
r.links = append(r.links, linkStr)
return
Expand Down
2 changes: 1 addition & 1 deletion modules/packages/pub/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func ParsePackage(r io.Reader) (*Package, error) {
if err != nil {
return nil, err
}
} else if strings.ToLower(hd.Name) == "readme.md" {
} else if strings.EqualFold(hd.Name, "readme.md") {
data, err := io.ReadAll(tr)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions modules/setting/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ func (c logCompression) IsValid() bool {
}

func (c logCompression) IsNone() bool {
return strings.ToLower(string(c)) == "none"
return strings.EqualFold(string(c), "none")
}

func (c logCompression) IsZstd() bool {
return c == "" || strings.ToLower(string(c)) == "zstd"
return c == "" || strings.EqualFold(string(c), "zstd")
}

func loadActionsFrom(rootCfg ConfigProvider) error {
Expand Down
2 changes: 1 addition & 1 deletion modules/util/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func SliceContainsString(slice []string, target string, insensitive ...bool) bool {
if len(insensitive) != 0 && insensitive[0] {
target = strings.ToLower(target)
return slices.ContainsFunc(slice, func(t string) bool { return strings.ToLower(t) == target })
return slices.ContainsFunc(slice, func(t string) bool { return strings.EqualFold(t, target) })
}

return slices.Contains(slice, target)
Expand Down
2 changes: 1 addition & 1 deletion modules/util/time_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TimeEstimateParse(timeStr string) (int64, error) {
unit := timeStr[match[4]:match[5]]
found := false
for _, u := range timeStrGlobalVars().units {
if strings.ToLower(unit) == u.name {
if strings.EqualFold(unit, u.name) {
total += amount * u.num
found = true
break
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func repoAssignment() func(ctx *context.APIContext) {
)

// 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
} else {
owner, err = user_model.GetUserByName(ctx, userName)
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/collaborators.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func GetRepoPermissions(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"

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")
return
}
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
newRepoName = *opts.Name
}
// 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 {
switch {
case repo_model.IsErrRepoAlreadyExist(err):
Expand Down
2 changes: 1 addition & 1 deletion routers/web/auth/oauth2_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func InfoOAuth(ctx *context.Context) {
var accessTokenScope auth.AccessTokenScope
if auHead := ctx.Req.Header.Get("Authorization"); auHead != "" {
auths := strings.Fields(auHead)
if len(auths) == 2 && (auths[0] == "token" || strings.ToLower(auths[0]) == "bearer") {
if len(auths) == 2 && (auths[0] == "token" || strings.EqualFold(auths[0], "bearer")) {
accessTokenScope, _ = auth_service.GetOAuthAccessTokenScopeAndUserID(ctx, auths[1])
}
}
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ func cleanUploadFileName(name string) string {
name = util.PathJoinRel(name)
// Git disallows any filenames to have a .git directory in them.
for _, part := range strings.Split(name, "/") {
if strings.ToLower(part) == ".git" {
if strings.EqualFold(part, ".git") {
return ""
}
}
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func handleSettingsPostUpdate(ctx *context.Context) {

newRepoName := form.RepoName
// Check if repository name has been changed.
if repo.LowerName != strings.ToLower(newRepoName) {
if !strings.EqualFold(repo.LowerName, newRepoName) {
// Close the GitRepo if open
if ctx.Repo.GitRepo != nil {
ctx.Repo.GitRepo.Close()
Expand Down
2 changes: 1 addition & 1 deletion services/auth/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (b *Basic) Verify(req *http.Request, w http.ResponseWriter, store DataStore
}

auths := strings.SplitN(baHead, " ", 2)
if len(auths) != 2 || (strings.ToLower(auths[0]) != "basic") {
if len(auths) != 2 || (!strings.EqualFold(auths[0], "basic")) {
return nil, nil
}

Expand Down
2 changes: 1 addition & 1 deletion services/auth/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func parseToken(req *http.Request) (string, bool) {
// check header token
if auHead := req.Header.Get("Authorization"); auHead != "" {
auths := strings.Fields(auHead)
if len(auths) == 2 && (auths[0] == "token" || strings.ToLower(auths[0]) == "bearer") {
if len(auths) == 2 && (auths[0] == "token" || strings.EqualFold(auths[0], "bearer")) {
return auths[1], true
}
}
Expand Down
2 changes: 1 addition & 1 deletion services/auth/source/ldap/source_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (source *Source) listLdapGroupMemberships(l *ldap.Conn, uid string, applyGr
}

func (source *Source) getUserAttributeListedInGroup(entry *ldap.Entry) string {
if strings.ToLower(source.UserUID) == "dn" {
if strings.EqualFold(source.UserUID, "dn") {
return entry.DN
}

Expand Down
2 changes: 1 addition & 1 deletion services/context/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func OrgAssignment(opts OrgAssignmentOptions) func(ctx *Context) {
if len(teamName) > 0 {
teamExists := false
for _, team := range ctx.Org.Teams {
if team.LowerName == strings.ToLower(teamName) {
if strings.EqualFold(team.LowerName, teamName) {
teamExists = true
ctx.Org.Team = team
ctx.Org.IsTeamMember = true
Expand Down
2 changes: 1 addition & 1 deletion services/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func RepoAssignment(ctx *Context) {
}

// 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
} else {
ctx.Repo.Owner, err = user_model.GetUserByName(ctx, userName)
Expand Down
2 changes: 1 addition & 1 deletion services/context/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
username := ctx.PathParam("username")

if doer != nil && doer.LowerName == strings.ToLower(username) {
if doer != nil && strings.EqualFold(doer.LowerName, username) {
contextUser = doer
} else {
var err error
Expand Down
2 changes: 1 addition & 1 deletion services/repository/files/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func CleanUploadFileName(name string) string {
name = util.PathJoinRel(name)
// Git disallows any filenames to have a .git directory in them.
for _, part := range strings.Split(name, "/") {
if strings.ToLower(part) == ".git" {
if strings.EqualFold(part, ".git") {
return ""
}
}
Expand Down