1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-08-06 10:25:22 +02:00

chore(cleanup): replaces unnecessary calls to formatting functions by non-formatting equivalents (#7994)

This PR replaces unnecessary calls to formatting functions (`fmt.Printf`, `fmt.Errorf`, ...) by non-formatting equivalents.
Resolves #7967

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7994
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: chavacava <chavacava@noreply.codeberg.org>
Co-committed-by: chavacava <chavacava@noreply.codeberg.org>
This commit is contained in:
chavacava 2025-05-29 17:34:29 +02:00 committed by Gusted
parent 25f3f8e1d2
commit 99d697263f
126 changed files with 340 additions and 281 deletions

View file

@ -30,7 +30,7 @@ func TestTimedDiscoveryCache(t *testing.T) {
// Make sure we can retrieve them
if di := dc.Get("foo"); di == nil {
t.Errorf("Expected a result, got nil")
t.Error("Expected a result, got nil")
} else if di.OpEndpoint() != "opEndpoint" || di.OpLocalID() != "opLocalID" || di.ClaimedID() != "claimedID" {
t.Errorf("Expected opEndpoint opLocalID claimedID, got %v %v %v", di.OpEndpoint(), di.OpLocalID(), di.ClaimedID())
}
@ -44,6 +44,6 @@ func TestTimedDiscoveryCache(t *testing.T) {
time.Sleep(100 * time.Millisecond)
if di := dc.Get("foo"); di != nil {
t.Errorf("Expected a nil, got a result")
t.Error("Expected a nil, got a result")
}
}

View file

@ -8,6 +8,7 @@ package identicon
import (
"crypto/sha256"
"errors"
"fmt"
"image"
"image/color"
@ -29,7 +30,7 @@ type Identicon struct {
// fore all possible foreground colors. only one foreground color will be picked randomly for one image
func New(size int, back color.Color, fore ...color.Color) (*Identicon, error) {
if len(fore) == 0 {
return nil, fmt.Errorf("foreground is not set")
return nil, errors.New("foreground is not set")
}
if size < minImageSize {

View file

@ -4,6 +4,7 @@
package cache
import (
"errors"
"fmt"
"strconv"
"time"
@ -48,7 +49,7 @@ const (
func Test() (time.Duration, error) {
if conn == nil {
return 0, fmt.Errorf("default cache not initialized")
return 0, errors.New("default cache not initialized")
}
testData := fmt.Sprintf("%x", make([]byte, 500))
@ -63,10 +64,10 @@ func Test() (time.Duration, error) {
}
testVal := conn.Get(testCacheKey)
if testVal == nil {
return 0, fmt.Errorf("expect cache hit but got none")
return 0, errors.New("expect cache hit but got none")
}
if testVal != testData {
return 0, fmt.Errorf("expect cache to return same value as stored but got other")
return 0, errors.New("expect cache to return same value as stored but got other")
}
return time.Since(start), nil

View file

@ -4,7 +4,7 @@
package cache
import (
"fmt"
"errors"
"testing"
"time"
@ -45,7 +45,7 @@ func TestGetString(t *testing.T) {
createTestCache()
data, err := GetString("key", func() (string, error) {
return "", fmt.Errorf("some error")
return "", errors.New("some error")
})
require.Error(t, err)
assert.Empty(t, data)
@ -70,7 +70,7 @@ func TestGetString(t *testing.T) {
assert.Equal(t, "some data", data)
data, err = GetString("key", func() (string, error) {
return "", fmt.Errorf("some error")
return "", errors.New("some error")
})
require.NoError(t, err)
assert.Equal(t, "some data", data)
@ -81,7 +81,7 @@ func TestGetInt(t *testing.T) {
createTestCache()
data, err := GetInt("key", func() (int, error) {
return 0, fmt.Errorf("some error")
return 0, errors.New("some error")
})
require.Error(t, err)
assert.Equal(t, 0, data)
@ -106,7 +106,7 @@ func TestGetInt(t *testing.T) {
assert.Equal(t, 100, data)
data, err = GetInt("key", func() (int, error) {
return 0, fmt.Errorf("some error")
return 0, errors.New("some error")
})
require.NoError(t, err)
assert.Equal(t, 100, data)
@ -117,7 +117,7 @@ func TestGetInt64(t *testing.T) {
createTestCache()
data, err := GetInt64("key", func() (int64, error) {
return 0, fmt.Errorf("some error")
return 0, errors.New("some error")
})
require.Error(t, err)
assert.EqualValues(t, 0, data)
@ -142,7 +142,7 @@ func TestGetInt64(t *testing.T) {
assert.EqualValues(t, 100, data)
data, err = GetInt64("key", func() (int64, error) {
return 0, fmt.Errorf("some error")
return 0, errors.New("some error")
})
require.NoError(t, err)
assert.EqualValues(t, 100, data)

View file

@ -23,16 +23,16 @@ func TestLookup(t *testing.T) {
d := FromAlias("beer")
if !reflect.DeepEqual(a, b) {
t.Errorf("a and b should equal")
t.Error("a and b should equal")
}
if !reflect.DeepEqual(b, c) {
t.Errorf("b and c should equal")
t.Error("b and c should equal")
}
if !reflect.DeepEqual(c, d) {
t.Errorf("c and d should equal")
t.Error("c and d should equal")
}
if !reflect.DeepEqual(a, d) {
t.Errorf("a and d should equal")
t.Error("a and d should equal")
}
m := FromCode("\U0001f44d")
@ -40,13 +40,13 @@ func TestLookup(t *testing.T) {
o := FromAlias("+1")
if !reflect.DeepEqual(m, n) {
t.Errorf("m and n should equal")
t.Error("m and n should equal")
}
if !reflect.DeepEqual(n, o) {
t.Errorf("n and o should equal")
t.Error("n and o should equal")
}
if !reflect.DeepEqual(m, o) {
t.Errorf("m and o should equal")
t.Error("m and o should equal")
}
}

View file

@ -4,7 +4,7 @@
package forgefed
import (
"fmt"
"errors"
"reflect"
"strings"
"testing"
@ -99,7 +99,7 @@ func Test_LikeUnmarshalJSON(t *testing.T) {
"invalid": {
item: []byte(`{"type":"Invalid","actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1","object":"https://codeberg.org/api/activitypub/repository-id/1"`),
want: &ForgeLike{},
wantErr: fmt.Errorf("cannot parse JSON"),
wantErr: errors.New("cannot parse JSON"),
},
}

View file

@ -4,7 +4,7 @@
package forgefed
import (
"fmt"
"errors"
"reflect"
"strings"
"testing"
@ -125,7 +125,7 @@ func Test_UndoLikeUnmarshalJSON(t *testing.T) {
"invalid": {
item: []byte(`invalid JSON`),
want: nil,
wantErr: fmt.Errorf("cannot parse JSON"),
wantErr: errors.New("cannot parse JSON"),
},
}

View file

@ -166,31 +166,31 @@ func TestShouldThrowErrorOnInvalidInput(t *testing.T) {
var err any
_, err = NewPersonID("", "forgejo")
if err == nil {
t.Errorf("empty input should be invalid.")
t.Error("empty input should be invalid.")
}
_, err = NewPersonID("http://localhost:3000/api/v1/something", "forgejo")
if err == nil {
t.Errorf("localhost uris are not external")
t.Error("localhost uris are not external")
}
_, err = NewPersonID("./api/v1/something", "forgejo")
if err == nil {
t.Errorf("relative uris are not allowed")
t.Error("relative uris are not allowed")
}
_, err = NewPersonID("http://1.2.3.4/api/v1/something", "forgejo")
if err == nil {
t.Errorf("uri may not be ip-4 based")
t.Error("uri may not be ip-4 based")
}
_, err = NewPersonID("http:///[fe80::1ff:fe23:4567:890a%25eth0]/api/v1/something", "forgejo")
if err == nil {
t.Errorf("uri may not be ip-6 based")
t.Error("uri may not be ip-6 based")
}
_, err = NewPersonID("https://codeberg.org/api/v1/activitypub/../activitypub/user-id/12345", "forgejo")
if err == nil {
t.Errorf("uri may not contain relative path elements")
t.Error("uri may not contain relative path elements")
}
_, err = NewPersonID("https://myuser@an.other.host/api/v1/activitypub/user-id/1", "forgejo")
if err == nil {
t.Errorf("uri may not contain unparsed elements")
t.Error("uri may not contain unparsed elements")
}
_, err = NewPersonID("https://an.other.host/api/v1/activitypub/user-id/1", "forgejo")
if err != nil {

View file

@ -9,7 +9,6 @@ import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os/exec"
"strconv"
@ -386,7 +385,7 @@ func parseSubmoduleContent(bs []byte) (*ObjectCache, error) {
}
submoduleCache := newObjectCache()
if len(cfg.Submodules) == 0 {
return nil, fmt.Errorf("no submodules found")
return nil, errors.New("no submodules found")
}
for _, subModule := range cfg.Submodules {
submoduleCache.Set(subModule.Path, subModule.URL)

View file

@ -7,6 +7,7 @@ import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
@ -116,7 +117,7 @@ func (ca GitAttribute) Bool() optional.Option[bool] {
// instantiation.
func (repo *Repository) gitCheckAttrCommand(treeish string, attributes ...string) (*Command, *RunOpts, context.CancelFunc, error) {
if len(attributes) == 0 {
return nil, nil, nil, fmt.Errorf("no provided attributes to check-attr")
return nil, nil, nil, errors.New("no provided attributes to check-attr")
}
env := os.Environ()

View file

@ -41,7 +41,7 @@ type Branch struct {
// GetHEADBranch returns corresponding branch of HEAD.
func (repo *Repository) GetHEADBranch() (*Branch, error) {
if repo == nil {
return nil, fmt.Errorf("nil repo")
return nil, errors.New("nil repo")
}
stdout, _, err := NewCommand(repo.Ctx, "symbolic-ref", "HEAD").RunStdString(&RunOpts{Dir: repo.Path})
if err != nil {

View file

@ -5,7 +5,7 @@ package internal
import (
"context"
"fmt"
"errors"
"forgejo.org/models/db"
repo_model "forgejo.org/models/repo"
@ -57,13 +57,13 @@ type dummyIndexer struct {
}
func (d *dummyIndexer) Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *RepoChanges) error {
return fmt.Errorf("indexer is not ready")
return errors.New("indexer is not ready")
}
func (d *dummyIndexer) Delete(ctx context.Context, repoID int64) error {
return fmt.Errorf("indexer is not ready")
return errors.New("indexer is not ready")
}
func (d *dummyIndexer) Search(ctx context.Context, opts *SearchOptions) (int64, []*SearchResult, []*SearchResultLanguages, error) {
return 0, nil, nil, fmt.Errorf("indexer is not ready")
return 0, nil, nil, errors.New("indexer is not ready")
}

View file

@ -5,7 +5,7 @@ package bleve
import (
"context"
"fmt"
"errors"
"forgejo.org/modules/indexer/internal"
"forgejo.org/modules/log"
@ -38,11 +38,11 @@ func NewIndexer(indexDir string, version int, mappingGetter func() (mapping.Inde
// Init initializes the indexer
func (i *Indexer) Init(_ context.Context) (bool, error) {
if i == nil {
return false, fmt.Errorf("cannot init nil indexer")
return false, errors.New("cannot init nil indexer")
}
if i.Indexer != nil {
return false, fmt.Errorf("indexer is already initialized")
return false, errors.New("indexer is already initialized")
}
indexer, version, err := openIndexer(i.indexDir, i.version)
@ -82,10 +82,10 @@ func (i *Indexer) Init(_ context.Context) (bool, error) {
// Ping checks if the indexer is available
func (i *Indexer) Ping(_ context.Context) error {
if i == nil {
return fmt.Errorf("cannot ping nil indexer")
return errors.New("cannot ping nil indexer")
}
if i.Indexer == nil {
return fmt.Errorf("indexer is not initialized")
return errors.New("indexer is not initialized")
}
return nil
}

View file

@ -5,6 +5,7 @@ package elasticsearch
import (
"context"
"errors"
"fmt"
"forgejo.org/modules/indexer/internal"
@ -36,10 +37,10 @@ func NewIndexer(url, indexName string, version int, mapping string) *Indexer {
// Init initializes the indexer
func (i *Indexer) Init(ctx context.Context) (bool, error) {
if i == nil {
return false, fmt.Errorf("cannot init nil indexer")
return false, errors.New("cannot init nil indexer")
}
if i.Client != nil {
return false, fmt.Errorf("indexer is already initialized")
return false, errors.New("indexer is already initialized")
}
client, err := i.initClient()
@ -66,10 +67,10 @@ func (i *Indexer) Init(ctx context.Context) (bool, error) {
// Ping checks if the indexer is available
func (i *Indexer) Ping(ctx context.Context) error {
if i == nil {
return fmt.Errorf("cannot ping nil indexer")
return errors.New("cannot ping nil indexer")
}
if i.Client == nil {
return fmt.Errorf("indexer is not initialized")
return errors.New("indexer is not initialized")
}
resp, err := i.Client.ClusterHealth().Do(ctx)

View file

@ -5,7 +5,7 @@ package internal
import (
"context"
"fmt"
"errors"
)
// Indexer defines an basic indexer interface
@ -27,11 +27,11 @@ func NewDummyIndexer() Indexer {
type dummyIndexer struct{}
func (d *dummyIndexer) Init(ctx context.Context) (bool, error) {
return false, fmt.Errorf("indexer is not ready")
return false, errors.New("indexer is not ready")
}
func (d *dummyIndexer) Ping(ctx context.Context) error {
return fmt.Errorf("indexer is not ready")
return errors.New("indexer is not ready")
}
func (d *dummyIndexer) Close() {}

View file

@ -5,6 +5,7 @@ package meilisearch
import (
"context"
"errors"
"fmt"
"github.com/meilisearch/meilisearch-go"
@ -33,11 +34,11 @@ func NewIndexer(url, apiKey, indexName string, version int, settings *meilisearc
// Init initializes the indexer
func (i *Indexer) Init(_ context.Context) (bool, error) {
if i == nil {
return false, fmt.Errorf("cannot init nil indexer")
return false, errors.New("cannot init nil indexer")
}
if i.Client != nil {
return false, fmt.Errorf("indexer is already initialized")
return false, errors.New("indexer is already initialized")
}
i.Client = meilisearch.New(i.url, meilisearch.WithAPIKey(i.apiKey))
@ -63,10 +64,10 @@ func (i *Indexer) Init(_ context.Context) (bool, error) {
// Ping checks if the indexer is available
func (i *Indexer) Ping(ctx context.Context) error {
if i == nil {
return fmt.Errorf("cannot ping nil indexer")
return errors.New("cannot ping nil indexer")
}
if i.Client == nil {
return fmt.Errorf("indexer is not initialized")
return errors.New("indexer is not initialized")
}
resp, err := i.Client.Health()
if err != nil {

View file

@ -5,7 +5,7 @@ package internal
import (
"context"
"fmt"
"errors"
"forgejo.org/modules/indexer/internal"
)
@ -30,13 +30,13 @@ type dummyIndexer struct {
}
func (d *dummyIndexer) Index(_ context.Context, _ ...*IndexerData) error {
return fmt.Errorf("indexer is not ready")
return errors.New("indexer is not ready")
}
func (d *dummyIndexer) Delete(_ context.Context, _ ...int64) error {
return fmt.Errorf("indexer is not ready")
return errors.New("indexer is not ready")
}
func (d *dummyIndexer) Search(_ context.Context, _ *SearchOptions) (*SearchResult, error) {
return nil, fmt.Errorf("indexer is not ready")
return nil, errors.New("indexer is not ready")
}

View file

@ -4,7 +4,7 @@
package stats
import (
"fmt"
"errors"
repo_model "forgejo.org/models/repo"
"forgejo.org/modules/graceful"
@ -31,7 +31,7 @@ func handler(items ...int64) []int64 {
func initStatsQueue() error {
statsQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "repo_stats_update", handler)
if statsQueue == nil {
return fmt.Errorf("unable to create repo_stats_update queue")
return errors.New("unable to create repo_stats_update queue")
}
go graceful.GetManager().RunWithCancel(statsQueue)
return nil

View file

@ -4,6 +4,7 @@
package template
import (
"errors"
"fmt"
"net/url"
"regexp"
@ -31,17 +32,17 @@ func Validate(template *api.IssueTemplate) error {
func validateMetadata(template *api.IssueTemplate) error {
if strings.TrimSpace(template.Name) == "" {
return fmt.Errorf("'name' is required")
return errors.New("'name' is required")
}
if strings.TrimSpace(template.About) == "" {
return fmt.Errorf("'about' is required")
return errors.New("'about' is required")
}
return nil
}
func validateYaml(template *api.IssueTemplate) error {
if len(template.Fields) == 0 {
return fmt.Errorf("'body' is required")
return errors.New("'body' is required")
}
ids := make(container.Set[string])
for idx, field := range template.Fields {

View file

@ -5,7 +5,7 @@
package markdown
import (
"fmt"
"errors"
"html/template"
"io"
"strings"
@ -54,7 +54,7 @@ func (l *limitWriter) Write(data []byte) (int, error) {
if err != nil {
return n, err
}
return n, fmt.Errorf("rendered content too large - truncating render")
return n, errors.New("rendered content too large - truncating render")
}
n, err := l.w.Write(data)
l.sum += int64(n)

View file

@ -262,7 +262,7 @@ func (p *iniConfigProvider) Save() error {
}
filename := p.file
if filename == "" {
return fmt.Errorf("config file path must not be empty")
return errors.New("config file path must not be empty")
}
if p.loadedFromEmpty {
if err := os.MkdirAll(filepath.Dir(filename), os.ModePerm); err != nil {

View file

@ -4,6 +4,7 @@
package setting
import (
"errors"
"fmt"
"net/mail"
"strings"
@ -68,7 +69,7 @@ func checkReplyToAddress() error {
}
if parsed.Name != "" {
return fmt.Errorf("name must not be set")
return errors.New("name must not be set")
}
c := strings.Count(IncomingEmail.ReplyToAddress, IncomingEmail.TokenPlaceholder)

View file

@ -4,6 +4,7 @@
package templates
import (
"errors"
"fmt"
"html"
"html/template"
@ -33,7 +34,7 @@ func dictMerge(base map[string]any, arg any) bool {
// The dot syntax is highly discouraged because it might cause unclear key conflicts. It's always good to use explicit keys.
func dict(args ...any) (map[string]any, error) {
if len(args)%2 != 0 {
return nil, fmt.Errorf("invalid dict constructor syntax: must have key-value pairs")
return nil, errors.New("invalid dict constructor syntax: must have key-value pairs")
}
m := make(map[string]any, len(args)/2)
for i := 0; i < len(args); i += 2 {

View file

@ -38,7 +38,7 @@ func Test_IsValid(t *testing.T) {
func Test_ValidateNotEmpty_ForString(t *testing.T) {
sut := ""
if len(ValidateNotEmpty(sut, "dummyField")) == 0 {
t.Errorf("sut should be invalid")
t.Error("sut should be invalid")
}
sut = "not empty"
if res := ValidateNotEmpty(sut, "dummyField"); len(res) > 0 {
@ -49,7 +49,7 @@ func Test_ValidateNotEmpty_ForString(t *testing.T) {
func Test_ValidateNotEmpty_ForTimestamp(t *testing.T) {
sut := timeutil.TimeStamp(0)
if res := ValidateNotEmpty(sut, "dummyField"); len(res) == 0 {
t.Errorf("sut should be invalid")
t.Error("sut should be invalid")
}
sut = timeutil.TimeStampNow()
if res := ValidateNotEmpty(sut, "dummyField"); len(res) > 0 {
@ -60,7 +60,7 @@ func Test_ValidateNotEmpty_ForTimestamp(t *testing.T) {
func Test_ValidateMaxLen(t *testing.T) {
sut := "0123456789"
if len(ValidateMaxLen(sut, 9, "dummyField")) == 0 {
t.Errorf("sut should be invalid")
t.Error("sut should be invalid")
}
sut = "0123456789"
if res := ValidateMaxLen(sut, 11, "dummyField"); len(res) > 0 {