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

@@ -41,7 +41,7 @@ func getPublicKeyFromResponse(b []byte, keyID *url.URL) (p crypto.PublicKey, err
return
}
p, err = x509.ParsePKIXPublicKey(block.Bytes)
return
return p, err
}
func fetch(iri *url.URL) (b []byte, err error) {
@@ -59,7 +59,7 @@ func fetch(iri *url.URL) (b []byte, err error) {
return
}
b, err = io.ReadAll(io.LimitReader(resp.Body, setting.Federation.MaxSize))
return
return b, err
}
func verifyHTTPSignatures(ctx *gitea_context.APIContext) (authenticated bool, err error) {
@@ -87,7 +87,7 @@ func verifyHTTPSignatures(ctx *gitea_context.APIContext) (authenticated bool, er
// 3. Verify the other actor's key
algo := httpsig.Algorithm(setting.Federation.Algorithms[0])
authenticated = v.Verify(pubKey, algo) == nil
return
return authenticated, err
}
// ReqHTTPSignature function

View File

@@ -63,5 +63,5 @@ func subjectToSource(value []string) (result []models.NotificationSource) {
result = append(result, models.NotificationSourceRepository)
}
}
return
return result
}

View File

@@ -47,7 +47,7 @@ func listUserOrgs(ctx *context.APIContext, u *user_model.User) {
}
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
ctx.SetTotalCountHeader(int64(maxResults))
ctx.SetTotalCountHeader(maxResults)
ctx.JSON(http.StatusOK, &apiOrgs)
}

View File

@@ -251,7 +251,7 @@ func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, lastModified time
}
blob = entry.Blob()
return
return blob, lastModified
}
// GetArchive get archive of a repository

View File

@@ -321,5 +321,5 @@ func prepareForReplaceOrAdd(ctx *context.APIContext, form api.IssueLabelsOption)
return
}
return
return issue, labels, err
}

View File

@@ -57,7 +57,7 @@ func NewCommitStatus(ctx *context.APIContext) {
return
}
status := &git_model.CommitStatus{
State: api.CommitStatusState(form.State),
State: form.State,
TargetURL: form.TargetURL,
Description: form.Description,
Context: form.Context,

View File

@@ -133,7 +133,7 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID
BranchFilter: form.BranchFilter,
},
IsActive: form.Active,
Type: webhook.HookType(form.Type),
Type: form.Type,
}
if w.Type == webhook.SLACK {
channel, ok := form.Config["channel"]

View File

@@ -248,7 +248,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID, refFullN
}
// 5. Check if the doer is allowed to push
canPush := false
var canPush bool
if ctx.opts.DeployKeyID != 0 {
canPush = !changedProtectedfiles && protectBranch.CanPush && (!protectBranch.EnableWhitelist || protectBranch.WhitelistDeployKeys)
} else {

View File

@@ -34,7 +34,7 @@ func Processes(ctx *context.PrivateContext) {
var processes []*process_module.Process
goroutineCount := int64(0)
processCount := 0
var processCount int
var err error
if stacktraces {
processes, processCount, goroutineCount, err = process_module.GetManager().ProcessStacktraces(flat, noSystem)

View File

@@ -142,7 +142,7 @@ func ServCommand(ctx *context.PrivateContext) {
if repo_model.IsErrRepoNotExist(err) {
repoExist = false
for _, verb := range ctx.FormStrings("verb") {
if "git-upload-pack" == verb {
if verb == "git-upload-pack" {
// User is fetching/cloning a non-existent repository
log.Warn("Failed authentication attempt (cannot find repository: %s/%s) from %s", results.OwnerName, results.RepoName, ctx.RemoteAddr())
ctx.JSON(http.StatusNotFound, private.ErrServCommand{

View File

@@ -101,7 +101,7 @@ func UnadoptedRepos(ctx *context.Context) {
ctx.ServerError("ListUnadoptedRepositories", err)
}
ctx.Data["Dirs"] = repoNames
pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
pager := context.NewPagination(count, opts.PageSize, opts.Page, 5)
pager.SetDefaultParams(ctx)
pager.AddParam(ctx, "search", "search")
ctx.Data["Page"] = pager

View File

@@ -168,7 +168,7 @@ func newAccessTokenResponse(ctx stdContext.Context, grant *auth.OAuth2Grant, ser
GrantID: grant.ID,
Counter: grant.Counter,
Type: oauth2.TypeRefreshToken,
RegisteredClaims: jwt.RegisteredClaims{ // nolint
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(refreshExpirationDate),
},
}

View File

@@ -71,7 +71,7 @@ func feedActionsToFeedItems(ctx *context.Context, actions models.ActionList) (it
for _, act := range actions {
act.LoadActUser()
content, desc, title := "", "", ""
var content, desc, title string
link := &feeds.Link{Href: act.GetCommentLink()}
@@ -246,7 +246,7 @@ func feedActionsToFeedItems(ctx *context.Context, actions models.ActionList) (it
Content: content,
})
}
return
return items, err
}
// GetFeedType return if it is a feed request and altered name and feed type.

View File

@@ -80,7 +80,7 @@ func Branches(ctx *context.Context) {
}
ctx.Data["Branches"] = branches
ctx.Data["DefaultBranchBranch"] = defaultBranchBranch
pager := context.NewPagination(int(branchesCount), setting.Git.BranchesRangeSize, page, 5)
pager := context.NewPagination(branchesCount, setting.Git.BranchesRangeSize, page, 5)
pager.SetDefaultParams(ctx)
ctx.Data["Page"] = pager

View File

@@ -116,7 +116,7 @@ func getBlobForEntry(ctx *context.Context) (blob *git.Blob, lastModified time.Ti
}
blob = entry.Blob()
return
return blob, lastModified
}
// SingleDownload download a file by repos path

View File

@@ -70,9 +70,7 @@ func TestGetClosestParentWithFiles(t *testing.T) {
gitRepo, _ := git.OpenRepository(git.DefaultContext, repo.RepoPath())
defer gitRepo.Close()
commit, _ := gitRepo.GetBranchCommit(branch)
expectedTreePath := ""
expectedTreePath = "" // Should return the root dir, empty string, since there are no subdirs in this repo
var expectedTreePath string // Should return the root dir, empty string, since there are no subdirs in this repo
for _, deletedFile := range []string{
"dir1/dir2/dir3/file.txt",
"file.txt",

View File

@@ -105,7 +105,7 @@ func Projects(ctx *context.Context) {
numPages := 0
if count > 0 {
numPages = int((int(count) - 1) / setting.UI.IssuePagingNum)
numPages = (int(count) - 1/setting.UI.IssuePagingNum)
}
pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, numPages)

View File

@@ -353,7 +353,7 @@ func renderReadmeFile(ctx *context.Context, readmeFile *namedBlob, readmeTreelin
if markupType := markup.Type(readmeFile.name); markupType != "" {
ctx.Data["IsMarkup"] = true
ctx.Data["MarkupType"] = string(markupType)
ctx.Data["MarkupType"] = markupType
var result strings.Builder
err := markup.Render(&markup.RenderContext{
Ctx: ctx,

View File

@@ -347,7 +347,7 @@ func Repos(ctx *context.Context) {
ctx.Data["Repos"] = repos
}
ctx.Data["Owner"] = ctxUser
pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
pager := context.NewPagination(count, opts.PageSize, opts.Page, 5)
pager.SetDefaultParams(ctx)
ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplSettingsRepositories)

View File

@@ -138,7 +138,7 @@ func Routes() *web.Route {
// redirect default favicon to the path of the custom favicon with a default as a fallback
routes.Get("/favicon.ico", func(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, path.Join(setting.StaticURLPrefix, "/assets/img/favicon.png"), 301)
http.Redirect(w, req, path.Join(setting.StaticURLPrefix, "/assets/img/favicon.png"), http.StatusMovedPermanently)
})
common := []interface{}{}
@@ -1121,7 +1121,7 @@ func RegisterRoutes(m *web.Route) {
}
repo.MustBeNotEmpty(ctx)
return
return cancel
})
m.Group("/pulls/{index}", func() {