1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

Add more linters to improve code readability (#19989)

Add nakedret, unconvert, wastedassign, stylecheck and nolintlint linters to improve code readability

- nakedret - https://github.com/alexkohler/nakedret - nakedret is a Go static analysis tool to find naked returns in functions greater than a specified function length.
- unconvert - https://github.com/mdempsky/unconvert - Remove unnecessary type conversions
- wastedassign - https://github.com/sanposhiho/wastedassign -  wastedassign finds wasted assignment statements.
- notlintlint -  Reports ill-formed or insufficient nolint directives
- stylecheck - https://staticcheck.io/docs/checks/#ST - keep style consistent
  - excluded: [ST1003 - Poorly chosen identifier](https://staticcheck.io/docs/checks/#ST1003) and [ST1005 - Incorrectly formatted error string](https://staticcheck.io/docs/checks/#ST1005)
This commit is contained in:
Wim
2022-06-20 12:02:49 +02:00
committed by GitHub
parent 3289abcefc
commit cb50375e2b
147 changed files with 402 additions and 397 deletions

View File

@@ -176,12 +176,12 @@ func ReadBatchLine(rd *bufio.Reader) (sha []byte, typ string, size int64, err er
typ = typ[:idx]
size, err = strconv.ParseInt(sizeStr, 10, 64)
return
return sha, typ, size, err
}
// ReadTagObjectID reads a tag object ID hash from a cat-file --batch stream, throwing away the rest of the stream.
func ReadTagObjectID(rd *bufio.Reader, size int64) (string, error) {
id := ""
var id string
var n int64
headerLoop:
for {
@@ -216,7 +216,7 @@ headerLoop:
// ReadTreeID reads a tree ID from a cat-file --batch stream, throwing away the rest of the stream.
func ReadTreeID(rd *bufio.Reader, size int64) (string, error) {
id := ""
var id string
var n int64
headerLoop:
for {
@@ -328,7 +328,7 @@ func ParseTreeLine(rd *bufio.Reader, modeBuf, fnameBuf, shaBuf []byte) (mode, fn
// Deal with the 20-byte SHA
idx = 0
for idx < 20 {
read := 0
var read int
read, err = rd.Read(shaBuf[idx:20])
n += read
if err != nil {
@@ -337,7 +337,7 @@ func ParseTreeLine(rd *bufio.Reader, modeBuf, fnameBuf, shaBuf []byte) (mode, fn
idx += read
}
sha = shaBuf
return
return mode, fname, sha, n, err
}
var callerPrefix string

View File

@@ -99,7 +99,7 @@ func (b *blobReader) Read(p []byte) (n int, err error) {
}
n, err = b.rd.Read(p)
b.n -= int64(n)
return
return n, err
}
// Close implements io.Closer

View File

@@ -418,7 +418,7 @@ func (c *Commit) LoadBranchName() (err error) {
}
c.Branch, err = c.GetBranchName()
return
return err
}
// GetTagName gets the current tag name for given commit

View File

@@ -157,7 +157,7 @@ func GetLastCommitForPaths(ctx context.Context, cache *LastCommitCache, commit *
if typ != "commit" {
return nil, fmt.Errorf("unexpected type: %s for commit id: %s", typ, commitID)
}
c, err = CommitFromReader(commit.repo, MustIDFromString(string(commitID)), io.LimitReader(batchReader, int64(size)))
c, err = CommitFromReader(commit.repo, MustIDFromString(commitID), io.LimitReader(batchReader, size))
if err != nil {
return nil, err
}

View File

@@ -115,7 +115,7 @@ func ParseDiffHunkString(diffhunk string) (leftLine, leftHunk, rightLine, righHu
rightLine = leftLine
righHunk = leftHunk
}
return
return leftLine, leftHunk, rightLine, righHunk
}
// Example: @@ -1,8 +1,9 @@ => [..., 1, 8, 1, 9]

View File

@@ -116,7 +116,7 @@ func FindLFSFile(repo *git.Repository, hash git.SHA1) ([]*LFSResult, error) {
continue
case "commit":
// Read in the commit to get its tree and in case this is one of the last used commits
curCommit, err = git.CommitFromReader(repo, git.MustIDFromString(string(commitID)), io.LimitReader(batchReader, int64(size)))
curCommit, err = git.CommitFromReader(repo, git.MustIDFromString(string(commitID)), io.LimitReader(batchReader, size))
if err != nil {
return nil, err
}

View File

@@ -334,7 +334,7 @@ func (wr *lineSeparatedAttributeWriter) Write(p []byte) (n int, err error) {
wr.tmp = []byte(remaining[3:])
break
}
return l, fmt.Errorf("unexpected tail %s", string(remaining))
return l, fmt.Errorf("unexpected tail %s", remaining)
}
_, _ = sb.WriteRune(rn)
remaining = tail

View File

@@ -101,5 +101,5 @@ func (repo *Repository) Close() (err error) {
repo.checkReader = nil
repo.checkWriter = nil
}
return
return err
}

View File

@@ -95,7 +95,7 @@ func callShowRef(ctx context.Context, repoPath, prefix, arg string, skip, limit
return nil
})
return
return branchNames, countAll, err
}
func walkShowRef(ctx context.Context, repoPath, arg string, skip, limit int, walkfn func(sha1, refname string) error) (countAll int, err error) {

View File

@@ -132,7 +132,7 @@ type lineCountWriter struct {
func (l *lineCountWriter) Write(p []byte) (n int, err error) {
n = len(p)
l.numLines += bytes.Count(p, []byte{'\000'})
return
return n, err
}
// GetDiffNumChangedFiles counts the number of changed files
@@ -177,7 +177,7 @@ func (repo *Repository) GetDiffShortStat(base, head string) (numFiles, totalAddi
if err != nil && strings.Contains(err.Error(), "no merge base") {
return GetDiffShortStat(repo.Ctx, repo.Path, base, head)
}
return
return numFiles, totalAdditions, totalDeletions, err
}
// GetDiffShortStat counts number of changed files, number of additions and deletions
@@ -231,7 +231,7 @@ func parseDiffStat(stdout string) (numFiles, totalAdditions, totalDeletions int,
return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumDeletions %v", stdout, err)
}
}
return
return numFiles, totalAdditions, totalDeletions, err
}
// GetDiffOrPatch generates either diff or formatted patch data between given revisions

View File

@@ -117,8 +117,8 @@ func TestReadWritePullHead(t *testing.T) {
return
}
assert.Len(t, string(headContents), 40)
assert.True(t, string(headContents) == newCommit)
assert.Len(t, headContents, 40)
assert.True(t, headContents == newCommit)
// Remove file after the test
err = repo.RemoveReference(PullPrefix + "1/head")

View File

@@ -64,7 +64,7 @@ func (repo *Repository) ReadTreeToTemporaryIndex(treeish string) (filename, tmpD
defer cancel()
return "", "", func() {}, err
}
return
return filename, tmpDir, cancel, err
}
// EmptyIndex empties the index

View File

@@ -27,7 +27,7 @@ func (repo *Repository) IsTagExist(name string) bool {
// returning at most limit tags, or all if limit is 0.
func (repo *Repository) GetTags(skip, limit int) (tags []string, err error) {
tags, _, err = callShowRef(repo.Ctx, repo.Path, TagPrefix, "--tags", skip, limit)
return
return tags, err
}
// GetTagType gets the type of the tag, either commit (simple) or tag (annotated)

View File

@@ -58,5 +58,5 @@ func NewHasher(t ObjectType, size int64) Hasher {
// Sum generates a SHA1 for the provided hash
func (h Hasher) Sum() (sha1 SHA1) {
copy(sha1[:], h.Hash.Sum(nil))
return
return sha1
}

View File

@@ -91,5 +91,5 @@ func newSignatureFromCommitline(line []byte) (sig *Signature, err error) {
return
}
}
return
return sig, err
}

View File

@@ -163,7 +163,7 @@ func (l *LimitedReaderCloser) Read(p []byte) (n int, err error) {
}
n, err = l.R.Read(p)
l.N -= int64(n)
return
return n, err
}
// Close implements io.Closer