Skip to content

Commit b49dd8e

Browse files
authored
update golangci-lint to v2.7.0 (#36079)
- Update and autofix most issues - Corrected variable names to `cutOk` - Impossible condition in `services/migrations/onedev_test.go` removed - `modules/setting/config_env.go:128:3` looks like a false-positive, added nolint
1 parent ee6e371 commit b49dd8e

File tree

14 files changed

+53
-56
lines changed

14 files changed

+53
-56
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ XGO_VERSION := go-1.25.x
3232
AIR_PACKAGE ?= github.com/air-verse/air@v1
3333
EDITORCONFIG_CHECKER_PACKAGE ?= github.com/editorconfig-checker/editorconfig-checker/v3/cmd/editorconfig-checker@v3
3434
GOFUMPT_PACKAGE ?= mvdan.cc/gofumpt@v0.9.2
35-
GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.6.0
35+
GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.7.0
3636
GXZ_PACKAGE ?= github.com/ulikunitz/xz/cmd/gxz@v0.5.15
3737
MISSPELL_PACKAGE ?= github.com/golangci/misspell/cmd/misspell@v0.7.0
3838
SWAGGER_PACKAGE ?= github.com/go-swagger/go-swagger/cmd/swagger@v0.33.1

modules/git/attribute/attribute.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ func (attrs *Attributes) GetGitlabLanguage() optional.Option[string] {
9696
// gitlab-language may have additional parameters after the language
9797
// ignore them and just use the main language
9898
// https://docs.gitlab.com/ee/user/project/highlighting.html#override-syntax-highlighting-for-a-file-type
99-
if idx := strings.IndexByte(raw, '?'); idx >= 0 {
100-
return optional.Some(raw[:idx])
99+
if before, _, ok := strings.Cut(raw, "?"); ok {
100+
return optional.Some(before)
101101
}
102102
}
103103
return attrStr

modules/git/foreachref/parser.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ func (p *Parser) parseRef(refBlock string) (map[string]string, error) {
113113

114114
var fieldKey string
115115
var fieldVal string
116-
firstSpace := strings.Index(field, " ")
117-
if firstSpace > 0 {
118-
fieldKey = field[:firstSpace]
119-
fieldVal = field[firstSpace+1:]
116+
before, after, ok := strings.Cut(field, " ")
117+
if ok {
118+
fieldKey = before
119+
fieldVal = after
120120
} else {
121121
// could be the case if the requested field had no value
122122
fieldKey = field

modules/git/parse.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ func parseLsTreeLine(line []byte) (*LsTreeEntry, error) {
2727
// <mode> <type> <sha>\t<filename>
2828

2929
var err error
30-
posTab := bytes.IndexByte(line, '\t')
31-
if posTab == -1 {
30+
before, after, ok := bytes.Cut(line, []byte{'\t'})
31+
if !ok {
3232
return nil, fmt.Errorf("invalid ls-tree output (no tab): %q", line)
3333
}
3434

3535
entry := new(LsTreeEntry)
3636

37-
entryAttrs := line[:posTab]
38-
entryName := line[posTab+1:]
37+
entryAttrs := before
38+
entryName := after
3939

4040
entryMode, entryAttrs, _ := bytes.Cut(entryAttrs, sepSpace)
4141
_ /* entryType */, entryAttrs, _ = bytes.Cut(entryAttrs, sepSpace) // the type is not used, the mode is enough to determine the type

modules/highlight/highlight.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ func Code(fileName, language, code string) (output template.HTML, lexerName stri
7777

7878
if lexer == nil {
7979
// Attempt stripping off the '?'
80-
if idx := strings.IndexByte(language, '?'); idx > 0 {
81-
lexer = lexers.Get(language[:idx])
80+
if before, _, ok := strings.Cut(language, "?"); ok {
81+
lexer = lexers.Get(before)
8282
}
8383
}
8484
}

modules/indexer/code/internal/util.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ func FilenameIndexerID(repoID int64, filename string) string {
1717
}
1818

1919
func ParseIndexerID(indexerID string) (int64, string) {
20-
index := strings.IndexByte(indexerID, '_')
21-
if index == -1 {
20+
before, after, ok := strings.Cut(indexerID, "_")
21+
if !ok {
2222
log.Error("Unexpected ID in repo indexer: %s", indexerID)
2323
}
24-
repoID, _ := internal.ParseBase36(indexerID[:index])
25-
return repoID, indexerID[index+1:]
24+
repoID, _ := internal.ParseBase36(before)
25+
return repoID, after
2626
}
2727

2828
func FilenameOfIndexerID(indexerID string) string {
29-
index := strings.IndexByte(indexerID, '_')
30-
if index == -1 {
29+
_, after, ok := strings.Cut(indexerID, "_")
30+
if !ok {
3131
log.Error("Unexpected ID in repo indexer: %s", indexerID)
3232
}
33-
return indexerID[index+1:]
33+
return after
3434
}
3535

3636
// FilenameMatchIndexPos returns the boundaries of its first seven lines.

modules/markup/html_link.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) {
3333
// Of text and link contents
3434
sl := strings.SplitSeq(content, "|")
3535
for v := range sl {
36-
if equalPos := strings.IndexByte(v, '='); equalPos == -1 {
36+
if found := strings.Contains(v, "="); !found {
3737
// There is no equal in this argument; this is a mandatory arg
3838
if props["name"] == "" {
3939
if IsFullURLString(v) {
@@ -55,8 +55,8 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) {
5555
} else {
5656
// There is an equal; optional argument.
5757

58-
sep := strings.IndexByte(v, '=')
59-
key, val := v[:sep], html.UnescapeString(v[sep+1:])
58+
before, after, _ := strings.Cut(v, "=")
59+
key, val := before, html.UnescapeString(after)
6060

6161
// When parsing HTML, x/net/html will change all quotes which are
6262
// not used for syntax into UTF-8 quotes. So checking val[0] won't

modules/setting/config_env.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ func decodeEnvSectionKey(encoded string) (ok bool, section, key string) {
5151
for _, unescapeIdx := range escapeStringIndices {
5252
preceding := encoded[last:unescapeIdx[0]]
5353
if !inKey {
54-
if splitter := strings.Index(preceding, "__"); splitter > -1 {
55-
section += preceding[:splitter]
54+
if before, after, cutOk := strings.Cut(preceding, "__"); cutOk {
55+
section += before
5656
inKey = true
57-
key += preceding[splitter+2:]
57+
key += after
5858
} else {
5959
section += preceding
6060
}
@@ -77,9 +77,9 @@ func decodeEnvSectionKey(encoded string) (ok bool, section, key string) {
7777
}
7878
remaining := encoded[last:]
7979
if !inKey {
80-
if splitter := strings.Index(remaining, "__"); splitter > -1 {
81-
section += remaining[:splitter]
82-
key += remaining[splitter+2:]
80+
if before, after, cutOk := strings.Cut(remaining, "__"); cutOk {
81+
section += before
82+
key += after
8383
} else {
8484
section += remaining
8585
}
@@ -111,21 +111,21 @@ func decodeEnvironmentKey(prefixGitea, suffixFile, envKey string) (ok bool, sect
111111

112112
func EnvironmentToConfig(cfg ConfigProvider, envs []string) (changed bool) {
113113
for _, kv := range envs {
114-
idx := strings.IndexByte(kv, '=')
115-
if idx < 0 {
114+
before, after, ok := strings.Cut(kv, "=")
115+
if !ok {
116116
continue
117117
}
118118

119119
// parse the environment variable to config section name and key name
120-
envKey := kv[:idx]
121-
envValue := kv[idx+1:]
120+
envKey := before
121+
envValue := after
122122
ok, sectionName, keyName, useFileValue := decodeEnvironmentKey(EnvConfigKeyPrefixGitea, EnvConfigKeySuffixFile, envKey)
123123
if !ok {
124124
continue
125125
}
126126

127127
// use environment value as config value, or read the file content as value if the key indicates a file
128-
keyValue := envValue
128+
keyValue := envValue //nolint:staticcheck // false positive
129129
if useFileValue {
130130
fileContent, err := os.ReadFile(envValue)
131131
if err != nil {

modules/validation/binding.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ func addValidGroupTeamMapRule() {
215215
}
216216

217217
func portOnly(hostport string) string {
218-
colon := strings.IndexByte(hostport, ':')
219-
if colon == -1 {
218+
_, after, ok := strings.Cut(hostport, ":")
219+
if !ok {
220220
return ""
221221
}
222222
if i := strings.Index(hostport, "]:"); i != -1 {
@@ -225,7 +225,7 @@ func portOnly(hostport string) string {
225225
if strings.Contains(hostport, "]") {
226226
return ""
227227
}
228-
return hostport[colon+len(":"):]
228+
return after
229229
}
230230

231231
func validPort(p string) bool {

services/auth/source/pam/source_authenticate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ func (source *Source) Authenticate(ctx context.Context, user *user_model.User, u
3535
// Allow PAM sources with `@` in their name, like from Active Directory
3636
username := pamLogin
3737
email := pamLogin
38-
idx := strings.Index(pamLogin, "@")
39-
if idx > -1 {
40-
username = pamLogin[:idx]
38+
before, _, ok := strings.Cut(pamLogin, "@")
39+
if ok {
40+
username = before
4141
}
4242
if user_model.ValidateEmail(email) != nil {
4343
if source.EmailDomain != "" {

0 commit comments

Comments
 (0)