Skip to content

Commit ee3c82f

Browse files
Enable addtional linters (#34085)
enable mirror, usestdlibbars and perfsprint part of: #34083 --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent 56e42be commit ee3c82f

File tree

294 files changed

+848
-805
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

294 files changed

+848
-805
lines changed

.golangci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ linters:
1313
- gocritic
1414
- govet
1515
- ineffassign
16+
- mirror
1617
- nakedret
1718
- nolintlint
19+
- perfsprint
1820
- revive
1921
- staticcheck
2022
- testifylint
2123
- unconvert
2224
- unparam
2325
- unused
26+
- usestdlibvars
2427
- usetesting
2528
- wastedassign
2629
settings:

cmd/dump.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package cmd
66

77
import (
8-
"fmt"
98
"os"
109
"path"
1110
"path/filepath"
@@ -93,7 +92,7 @@ var CmdDump = &cli.Command{
9392
},
9493
&cli.StringFlag{
9594
Name: "type",
96-
Usage: fmt.Sprintf(`Dump output format, default to "zip", supported types: %s`, strings.Join(dump.SupportedOutputTypes, ", ")),
95+
Usage: `Dump output format, default to "zip", supported types: ` + strings.Join(dump.SupportedOutputTypes, ", "),
9796
},
9897
},
9998
}

cmd/main_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package cmd
55

66
import (
7+
"errors"
78
"fmt"
89
"io"
910
"path/filepath"
@@ -127,7 +128,7 @@ func TestCliCmd(t *testing.T) {
127128
}
128129

129130
func TestCliCmdError(t *testing.T) {
130-
app := newTestApp(func(ctx *cli.Context) error { return fmt.Errorf("normal error") })
131+
app := newTestApp(func(ctx *cli.Context) error { return errors.New("normal error") })
131132
r, err := runTestApp(app, "./gitea", "test-cmd")
132133
assert.Error(t, err)
133134
assert.Equal(t, 1, r.ExitCode)

cmd/serv.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func getLFSAuthToken(ctx context.Context, lfsVerb string, results *private.ServC
173173
if err != nil {
174174
return "", fail(ctx, "Failed to sign JWT Token", "Failed to sign JWT token: %v", err)
175175
}
176-
return fmt.Sprintf("Bearer %s", tokenString), nil
176+
return "Bearer " + tokenString, nil
177177
}
178178

179179
func runServ(c *cli.Context) error {
@@ -372,9 +372,9 @@ func runServ(c *cli.Context) error {
372372
repo_module.EnvPusherEmail+"="+results.UserEmail,
373373
repo_module.EnvPusherID+"="+strconv.FormatInt(results.UserID, 10),
374374
repo_module.EnvRepoID+"="+strconv.FormatInt(results.RepoID, 10),
375-
repo_module.EnvPRID+"="+fmt.Sprintf("%d", 0),
376-
repo_module.EnvDeployKeyID+"="+fmt.Sprintf("%d", results.DeployKeyID),
377-
repo_module.EnvKeyID+"="+fmt.Sprintf("%d", results.KeyID),
375+
repo_module.EnvPRID+"="+strconv.Itoa(0),
376+
repo_module.EnvDeployKeyID+"="+strconv.FormatInt(results.DeployKeyID, 10),
377+
repo_module.EnvKeyID+"="+strconv.FormatInt(results.KeyID, 10),
378378
repo_module.EnvAppURL+"="+setting.AppURL,
379379
)
380380
// to avoid breaking, here only use the minimal environment variables for the "gitea serv" command.

cmd/web_acme.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func runACME(listenAddr string, m http.Handler) error {
136136
}
137137

138138
func runLetsEncryptFallbackHandler(w http.ResponseWriter, r *http.Request) {
139-
if r.Method != "GET" && r.Method != "HEAD" {
139+
if r.Method != http.MethodGet && r.Method != http.MethodHead {
140140
http.Error(w, "Use HTTPS", http.StatusBadRequest)
141141
return
142142
}

contrib/backport/backport.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package main
66

77
import (
88
"context"
9+
"errors"
910
"fmt"
1011
"log"
1112
"net/http"
@@ -158,7 +159,7 @@ func runBackport(c *cli.Context) error {
158159

159160
args := c.Args().Slice()
160161
if len(args) == 0 && pr == "" {
161-
return fmt.Errorf("no PR number provided\nProvide a PR number to backport")
162+
return errors.New("no PR number provided\nProvide a PR number to backport")
162163
} else if len(args) != 1 && pr == "" {
163164
return fmt.Errorf("multiple PRs provided %v\nOnly a single PR can be backported at a time", args)
164165
}

models/actions/run.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package actions
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"slices"
1011
"strings"
@@ -245,7 +246,7 @@ func CancelPreviousJobs(ctx context.Context, repoID int64, ref, workflowID strin
245246

246247
// If the update affected 0 rows, it means the job has changed in the meantime, so we need to try again.
247248
if n == 0 {
248-
return cancelledJobs, fmt.Errorf("job has changed, try again")
249+
return cancelledJobs, errors.New("job has changed, try again")
249250
}
250251

251252
cancelledJobs = append(cancelledJobs, job)
@@ -412,7 +413,7 @@ func UpdateRun(ctx context.Context, run *ActionRun, cols ...string) error {
412413
return err
413414
}
414415
if affected == 0 {
415-
return fmt.Errorf("run has changed")
416+
return errors.New("run has changed")
416417
// It's impossible that the run is not found, since Gitea never deletes runs.
417418
}
418419

models/actions/task.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package actions
66
import (
77
"context"
88
"crypto/subtle"
9+
"errors"
910
"fmt"
1011
"time"
1112

@@ -361,7 +362,7 @@ func UpdateTaskByState(ctx context.Context, runnerID int64, state *runnerv1.Task
361362
} else if !has {
362363
return nil, util.ErrNotExist
363364
} else if runnerID != task.RunnerID {
364-
return nil, fmt.Errorf("invalid runner for task")
365+
return nil, errors.New("invalid runner for task")
365366
}
366367

367368
if task.Status.IsDone() {

models/activities/action_list.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package activities
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"strconv"
1011

@@ -205,7 +206,7 @@ func (actions ActionList) LoadIssues(ctx context.Context) error {
205206
// GetFeeds returns actions according to the provided options
206207
func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, int64, error) {
207208
if opts.RequestedUser == nil && opts.RequestedTeam == nil && opts.RequestedRepo == nil {
208-
return nil, 0, fmt.Errorf("need at least one of these filters: RequestedUser, RequestedTeam, RequestedRepo")
209+
return nil, 0, errors.New("need at least one of these filters: RequestedUser, RequestedTeam, RequestedRepo")
209210
}
210211

211212
var err error

models/asymkey/error.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func IsErrGPGKeyParsing(err error) bool {
132132
}
133133

134134
func (err ErrGPGKeyParsing) Error() string {
135-
return fmt.Sprintf("failed to parse gpg key %s", err.ParseError.Error())
135+
return "failed to parse gpg key " + err.ParseError.Error()
136136
}
137137

138138
// ErrGPGKeyNotExist represents a "GPGKeyNotExist" kind of error.

models/asymkey/gpg_key.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package asymkey
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"strings"
1011
"time"
@@ -207,7 +208,7 @@ func parseGPGKey(ctx context.Context, ownerID int64, e *openpgp.Entity, verified
207208
// deleteGPGKey does the actual key deletion
208209
func deleteGPGKey(ctx context.Context, keyID string) (int64, error) {
209210
if keyID == "" {
210-
return 0, fmt.Errorf("empty KeyId forbidden") // Should never happen but just to be sure
211+
return 0, errors.New("empty KeyId forbidden") // Should never happen but just to be sure
211212
}
212213
// Delete imported key
213214
n, err := db.GetEngine(ctx).Where("key_id=?", keyID).Delete(new(GPGKeyImport))

models/asymkey/gpg_key_commit_verification.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package asymkey
55

66
import (
7+
"errors"
78
"fmt"
89
"hash"
910

@@ -68,7 +69,7 @@ const (
6869
func verifySign(s *packet.Signature, h hash.Hash, k *GPGKey) error {
6970
// Check if key can sign
7071
if !k.CanSign {
71-
return fmt.Errorf("key can not sign")
72+
return errors.New("key can not sign")
7273
}
7374
// Decode key
7475
pkey, err := base64DecPubKey(k.Content)

models/asymkey/gpg_key_common.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bytes"
88
"crypto"
99
"encoding/base64"
10+
"errors"
1011
"fmt"
1112
"hash"
1213
"io"
@@ -75,7 +76,7 @@ func base64DecPubKey(content string) (*packet.PublicKey, error) {
7576
// Check type
7677
pkey, ok := p.(*packet.PublicKey)
7778
if !ok {
78-
return nil, fmt.Errorf("key is not a public key")
79+
return nil, errors.New("key is not a public key")
7980
}
8081
return pkey, nil
8182
}
@@ -122,15 +123,15 @@ func readArmoredSign(r io.Reader) (body io.Reader, err error) {
122123
func ExtractSignature(s string) (*packet.Signature, error) {
123124
r, err := readArmoredSign(strings.NewReader(s))
124125
if err != nil {
125-
return nil, fmt.Errorf("Failed to read signature armor")
126+
return nil, errors.New("Failed to read signature armor")
126127
}
127128
p, err := packet.Read(r)
128129
if err != nil {
129-
return nil, fmt.Errorf("Failed to read signature packet")
130+
return nil, errors.New("Failed to read signature packet")
130131
}
131132
sig, ok := p.(*packet.Signature)
132133
if !ok {
133-
return nil, fmt.Errorf("Packet is not a signature")
134+
return nil, errors.New("Packet is not a signature")
134135
}
135136
return sig, nil
136137
}

models/asymkey/ssh_key_commit_verification.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package asymkey
55

66
import (
7-
"bytes"
87
"context"
98
"fmt"
109
"strings"
@@ -65,7 +64,7 @@ func ParseCommitWithSSHSignature(ctx context.Context, c *git.Commit, committer *
6564
}
6665

6766
func verifySSHCommitVerification(sig, payload string, k *PublicKey, committer, signer *user_model.User, email string) *CommitVerification {
68-
if err := sshsig.Verify(bytes.NewBuffer([]byte(payload)), []byte(sig), []byte(k.Content), "git"); err != nil {
67+
if err := sshsig.Verify(strings.NewReader(payload), []byte(sig), []byte(k.Content), "git"); err != nil {
6968
return nil
7069
}
7170

models/asymkey/ssh_key_parse.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"encoding/base64"
1111
"encoding/binary"
1212
"encoding/pem"
13+
"errors"
1314
"fmt"
1415
"math/big"
1516
"os"
@@ -93,7 +94,7 @@ func parseKeyString(content string) (string, error) {
9394

9495
block, _ := pem.Decode([]byte(content))
9596
if block == nil {
96-
return "", fmt.Errorf("failed to parse PEM block containing the public key")
97+
return "", errors.New("failed to parse PEM block containing the public key")
9798
}
9899
if strings.Contains(block.Type, "PRIVATE") {
99100
return "", ErrKeyIsPrivate

models/asymkey/ssh_key_verify.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
package asymkey
55

66
import (
7-
"bytes"
87
"context"
8+
"strings"
99

1010
"code.gitea.io/gitea/models/db"
1111
"code.gitea.io/gitea/modules/log"
@@ -30,11 +30,11 @@ func VerifySSHKey(ctx context.Context, ownerID int64, fingerprint, token, signat
3030
return "", ErrKeyNotExist{}
3131
}
3232

33-
err = sshsig.Verify(bytes.NewBuffer([]byte(token)), []byte(signature), []byte(key.Content), "gitea")
33+
err = sshsig.Verify(strings.NewReader(token), []byte(signature), []byte(key.Content), "gitea")
3434
if err != nil {
3535
// edge case for Windows based shells that will add CR LF if piped to ssh-keygen command
3636
// see https://github.com/PowerShell/PowerShell/issues/5974
37-
if sshsig.Verify(bytes.NewBuffer([]byte(token+"\r\n")), []byte(signature), []byte(key.Content), "gitea") != nil {
37+
if sshsig.Verify(strings.NewReader(token+"\r\n"), []byte(signature), []byte(key.Content), "gitea") != nil {
3838
log.Error("Unable to validate token signature. Error: %v", err)
3939
return "", ErrSSHInvalidTokenSignature{
4040
Fingerprint: key.Fingerprint,

models/auth/access_token_scope_test.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ func TestAccessTokenScope_Normalize(t *testing.T) {
2828

2929
for _, scope := range GetAccessTokenCategories() {
3030
tests = append(tests,
31-
scopeTestNormalize{AccessTokenScope(fmt.Sprintf("read:%s", scope)), AccessTokenScope(fmt.Sprintf("read:%s", scope)), nil},
32-
scopeTestNormalize{AccessTokenScope(fmt.Sprintf("write:%s", scope)), AccessTokenScope(fmt.Sprintf("write:%s", scope)), nil},
33-
scopeTestNormalize{AccessTokenScope(fmt.Sprintf("write:%[1]s,read:%[1]s", scope)), AccessTokenScope(fmt.Sprintf("write:%s", scope)), nil},
34-
scopeTestNormalize{AccessTokenScope(fmt.Sprintf("read:%[1]s,write:%[1]s", scope)), AccessTokenScope(fmt.Sprintf("write:%s", scope)), nil},
35-
scopeTestNormalize{AccessTokenScope(fmt.Sprintf("read:%[1]s,write:%[1]s,write:%[1]s", scope)), AccessTokenScope(fmt.Sprintf("write:%s", scope)), nil},
31+
scopeTestNormalize{AccessTokenScope("read:" + scope), AccessTokenScope("read:" + scope), nil},
32+
scopeTestNormalize{AccessTokenScope("write:" + scope), AccessTokenScope("write:" + scope), nil},
33+
scopeTestNormalize{AccessTokenScope(fmt.Sprintf("write:%[1]s,read:%[1]s", scope)), AccessTokenScope("write:" + scope), nil},
34+
scopeTestNormalize{AccessTokenScope(fmt.Sprintf("read:%[1]s,write:%[1]s", scope)), AccessTokenScope("write:" + scope), nil},
35+
scopeTestNormalize{AccessTokenScope(fmt.Sprintf("read:%[1]s,write:%[1]s,write:%[1]s", scope)), AccessTokenScope("write:" + scope), nil},
3636
)
3737
}
3838

@@ -63,20 +63,20 @@ func TestAccessTokenScope_HasScope(t *testing.T) {
6363
for _, scope := range GetAccessTokenCategories() {
6464
tests = append(tests,
6565
scopeTestHasScope{
66-
AccessTokenScope(fmt.Sprintf("read:%s", scope)),
67-
AccessTokenScope(fmt.Sprintf("read:%s", scope)), true, nil,
66+
AccessTokenScope("read:" + scope),
67+
AccessTokenScope("read:" + scope), true, nil,
6868
},
6969
scopeTestHasScope{
70-
AccessTokenScope(fmt.Sprintf("write:%s", scope)),
71-
AccessTokenScope(fmt.Sprintf("write:%s", scope)), true, nil,
70+
AccessTokenScope("write:" + scope),
71+
AccessTokenScope("write:" + scope), true, nil,
7272
},
7373
scopeTestHasScope{
74-
AccessTokenScope(fmt.Sprintf("write:%s", scope)),
75-
AccessTokenScope(fmt.Sprintf("read:%s", scope)), true, nil,
74+
AccessTokenScope("write:" + scope),
75+
AccessTokenScope("read:" + scope), true, nil,
7676
},
7777
scopeTestHasScope{
78-
AccessTokenScope(fmt.Sprintf("read:%s", scope)),
79-
AccessTokenScope(fmt.Sprintf("write:%s", scope)), false, nil,
78+
AccessTokenScope("read:" + scope),
79+
AccessTokenScope("write:" + scope), false, nil,
8080
},
8181
)
8282
}

models/db/engine.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func IsTableNotEmpty(beanOrTableName any) (bool, error) {
127127

128128
// DeleteAllRecords will delete all the records of this table
129129
func DeleteAllRecords(tableName string) error {
130-
_, err := xormEngine.Exec(fmt.Sprintf("DELETE FROM %s", tableName))
130+
_, err := xormEngine.Exec("DELETE FROM " + tableName)
131131
return err
132132
}
133133

models/db/error.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (err ErrNotExist) Error() string {
6565
if err.ID != 0 {
6666
return fmt.Sprintf("%s does not exist [id: %d]", name, err.ID)
6767
}
68-
return fmt.Sprintf("%s does not exist", name)
68+
return name + " does not exist"
6969
}
7070

7171
// Unwrap unwraps this as a ErrNotExist err

models/git/commit_status.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (status *CommitStatus) HideActionsURL(ctx context.Context) {
222222
}
223223
}
224224

225-
prefix := fmt.Sprintf("%s/actions", status.Repo.Link())
225+
prefix := status.Repo.Link() + "/actions"
226226
if strings.HasPrefix(status.TargetURL, prefix) {
227227
status.TargetURL = ""
228228
}

models/issues/issue.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"html/template"
1111
"regexp"
1212
"slices"
13+
"strconv"
1314

1415
"code.gitea.io/gitea/models/db"
1516
project_model "code.gitea.io/gitea/models/project"
@@ -815,7 +816,7 @@ func ChangeIssueTimeEstimate(ctx context.Context, issue *Issue, doer *user_model
815816
Doer: doer,
816817
Repo: issue.Repo,
817818
Issue: issue,
818-
Content: fmt.Sprintf("%d", timeEstimate),
819+
Content: strconv.FormatInt(timeEstimate, 10),
819820
}
820821
if _, err := CreateComment(ctx, opts); err != nil {
821822
return fmt.Errorf("createComment: %w", err)

0 commit comments

Comments
 (0)