Enable more revive linter rules (#30608)

Noteable additions:

- `redefines-builtin-id` forbid variable names that shadow go builtins
- `empty-lines` remove unnecessary empty lines that `gofumpt` does not
remove for some reason
- `superfluous-else` eliminate more superfluous `else` branches

Rules are also sorted alphabetically and I cleaned up various parts of
`.golangci.yml`.

(cherry picked from commit 74f0c84fa4245a20ce6fb87dac1faf2aeeded2a2)

Conflicts:
	.golangci.yml
	apply the linter recommendations to Forgejo code as well
This commit is contained in:
silverwind 2024-04-22 13:48:42 +02:00 committed by Earl Warren
parent 31b608a1e9
commit 12b199c5e5
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
79 changed files with 130 additions and 193 deletions

View file

@ -144,7 +144,6 @@ func ArtifactContexter() func(next http.Handler) http.Handler {
var task *actions.ActionTask
if err == nil {
task, err = actions.GetTaskByID(req.Context(), tID)
if err != nil {
log.Error("Error runner api getting task by ID: %v", err)

View file

@ -144,12 +144,12 @@ func UploadPackageFile(ctx *context.Context) {
return
}
upload, close, err := ctx.UploadStream()
upload, needToClose, err := ctx.UploadStream()
if err != nil {
apiError(ctx, http.StatusInternalServerError, err)
return
}
if close {
if needToClose {
defer upload.Close()
}

View file

@ -310,12 +310,12 @@ func uploadFile(ctx *context.Context, fileFilter container.Set[string], fileKey
return
}
upload, close, err := ctx.UploadStream()
upload, needToClose, err := ctx.UploadStream()
if err != nil {
apiError(ctx, http.StatusBadRequest, err)
return
}
if close {
if needToClose {
defer upload.Close()
}

View file

@ -174,12 +174,12 @@ func EnumeratePackages(ctx *context.Context) {
}
func UploadPackageFile(ctx *context.Context) {
upload, close, err := ctx.UploadStream()
upload, needToClose, err := ctx.UploadStream()
if err != nil {
apiError(ctx, http.StatusInternalServerError, err)
return
}
if close {
if needToClose {
defer upload.Close()
}

View file

@ -385,9 +385,9 @@ func EndUploadBlob(ctx *context.Context) {
}
return
}
close := true
doClose := true
defer func() {
if close {
if doClose {
uploader.Close()
}
}()
@ -427,7 +427,7 @@ func EndUploadBlob(ctx *context.Context) {
apiError(ctx, http.StatusInternalServerError, err)
return
}
close = false
doClose = false
if err := container_service.RemoveBlobUploadByID(ctx, uploader.ID); err != nil {
apiError(ctx, http.StatusInternalServerError, err)

View file

@ -151,12 +151,12 @@ func UploadBinaryPackageFile(ctx *context.Context) {
}
func uploadPackageFile(ctx *context.Context, compositeKey string, properties map[string]string) {
upload, close, err := ctx.UploadStream()
upload, needToClose, err := ctx.UploadStream()
if err != nil {
apiError(ctx, http.StatusBadRequest, err)
return
}
if close {
if needToClose {
defer upload.Close()
}

View file

@ -127,12 +127,12 @@ func UploadPackageFile(ctx *context.Context) {
return
}
upload, close, err := ctx.UploadStream()
upload, needToClose, err := ctx.UploadStream()
if err != nil {
apiError(ctx, http.StatusInternalServerError, err)
return
}
if close {
if needToClose {
defer upload.Close()
}

View file

@ -90,12 +90,12 @@ func UploadPackage(ctx *context.Context) {
return
}
upload, close, err := ctx.UploadStream()
upload, needToClose, err := ctx.UploadStream()
if err != nil {
apiError(ctx, http.StatusInternalServerError, err)
return
}
if close {
if needToClose {
defer upload.Close()
}

View file

@ -154,12 +154,12 @@ func resolvePackage(ctx *context.Context, ownerID int64, name, version string) (
}
func UploadPackage(ctx *context.Context) {
upload, close, err := ctx.UploadStream()
upload, needToClose, err := ctx.UploadStream()
if err != nil {
apiError(ctx, http.StatusInternalServerError, err)
return
}
if close {
if needToClose {
defer upload.Close()
}

View file

@ -594,13 +594,13 @@ func UploadSymbolPackage(ctx *context.Context) {
func processUploadedFile(ctx *context.Context, expectedType nuget_module.PackageType) (*nuget_module.Package, *packages_module.HashedBuffer, []io.Closer) {
closables := make([]io.Closer, 0, 2)
upload, close, err := ctx.UploadStream()
upload, needToClose, err := ctx.UploadStream()
if err != nil {
apiError(ctx, http.StatusBadRequest, err)
return nil, nil, closables
}
if close {
if needToClose {
closables = append(closables, upload)
}

View file

@ -117,12 +117,12 @@ func GetRepositoryFile(ctx *context.Context) {
}
func UploadPackageFile(ctx *context.Context) {
upload, close, err := ctx.UploadStream()
upload, needToClose, err := ctx.UploadStream()
if err != nil {
apiError(ctx, http.StatusInternalServerError, err)
return
}
if close {
if needToClose {
defer upload.Close()
}

View file

@ -197,12 +197,12 @@ func DownloadPackageFile(ctx *context.Context) {
// UploadPackageFile adds a file to the package. If the package does not exist, it gets created.
func UploadPackageFile(ctx *context.Context) {
upload, close, err := ctx.UploadStream()
upload, needToClose, err := ctx.UploadStream()
if err != nil {
apiError(ctx, http.StatusBadRequest, err)
return
}
if close {
if needToClose {
defer upload.Close()
}

View file

@ -217,7 +217,6 @@ func SearchIssues(ctx *context.APIContext) {
var includedAnyLabels []int64
{
labels := ctx.FormTrim("labels")
var includedLabelNames []string
if len(labels) > 0 {

View file

@ -180,7 +180,6 @@ func ListPushMirrors(ctx *context.APIContext) {
if err == nil {
responsePushMirrors = append(responsePushMirrors, m)
}
}
ctx.SetLinkHeader(len(responsePushMirrors), utils.GetListOptions(ctx).PageSize)
ctx.SetTotalCountHeader(count)

View file

@ -1058,7 +1058,6 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
isSameRepo = true
headUser = ctx.Repo.Owner
headBranch = headInfos[0]
} else if len(headInfos) == 2 {
headUser, err = user_model.GetUserByName(ctx, headInfos[0])
if err != nil {
@ -1072,7 +1071,6 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
headBranch = headInfos[1]
// The head repository can also point to the same repo
isSameRepo = ctx.Repo.Owner.ID == headUser.ID
} else {
ctx.NotFound()
return nil, nil, nil, nil, "", ""

View file

@ -875,7 +875,6 @@ func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions
}
if ctx.Repo.Repository.Owner.IsOrganization() && len(opts.TeamReviewers) > 0 {
teamReviewers := make([]*organization.Team, 0, len(opts.TeamReviewers))
for _, t := range opts.TeamReviewers {
var teamReviewer *organization.Team

View file

@ -1094,7 +1094,6 @@ func updateMirror(ctx *context.APIContext, opts api.EditRepoOption) error {
// update MirrorInterval
if opts.MirrorInterval != nil {
// MirrorInterval should be a duration
interval, err := time.ParseDuration(*opts.MirrorInterval)
if err != nil {

View file

@ -478,7 +478,6 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error)
func findWikiRepoCommit(ctx *context.APIContext) (*git.Repository, *git.Commit) {
wikiRepo, err := gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
if err != nil {
if git.IsErrNotExist(err) || err.Error() == "no such file or directory" {
ctx.NotFound(err)
} else {

View file

@ -259,7 +259,6 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
UserMsg: fmt.Sprintf("branch %s is protected from force push", branchName),
})
return
}
}

View file

@ -689,7 +689,6 @@ func ArtifactsDownloadView(ctx *context_module.Context) {
writer := zip.NewWriter(ctx.Resp)
defer writer.Close()
for _, art := range artifacts {
f, err := storage.ActionsArtifacts.Open(art.StoragePath)
if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())

View file

@ -927,7 +927,6 @@ func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleFiles
}
}
}
}
if template.Ref != "" && !strings.HasPrefix(template.Ref, "refs/") { // Assume that the ref intended is always a branch - for tags users should use refs/tags/<ref>
@ -1680,7 +1679,6 @@ func ViewIssue(ctx *context.Context) {
if comment.ProjectID > 0 && comment.Project == nil {
comment.Project = ghostProject
}
} else if comment.Type == issues_model.CommentTypeAssignees || comment.Type == issues_model.CommentTypeReviewRequest {
if err = comment.LoadAssigneeUserAndTeam(ctx); err != nil {
ctx.ServerError("LoadAssigneeUserAndTeam", err)
@ -2605,7 +2603,6 @@ func SearchIssues(ctx *context.Context) {
var includedAnyLabels []int64
{
labels := ctx.FormTrim("labels")
var includedLabelNames []string
if len(labels) > 0 {
@ -2993,7 +2990,6 @@ func NewComment(ctx *context.Context) {
if (ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) || (ctx.IsSigned && issue.IsPoster(ctx.Doer.ID))) &&
(form.Status == "reopen" || form.Status == "close") &&
!(issue.IsPull && issue.PullRequest.HasMerged) {
// Duplication and conflict check should apply to reopen pull request.
var pr *issues_model.PullRequest

View file

@ -670,7 +670,6 @@ func PrepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.C
}
if pb != nil && pb.EnableStatusCheck {
var missingRequiredChecks []string
for _, requiredContext := range pb.StatusCheckContexts {
contextFound := false
@ -873,7 +872,6 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
// Validate the given commit sha to show (if any passed)
if willShowSpecifiedCommit || willShowSpecifiedCommitRange {
foundStartCommit := len(specifiedStartCommit) == 0
foundEndCommit := len(specifiedEndCommit) == 0
@ -1185,7 +1183,6 @@ func UpdatePullRequest(ctx *context.Context) {
ctx.Flash.Error(flashError)
ctx.Redirect(issue.Link())
return
}
ctx.Flash.Error(err.Error())
ctx.Redirect(issue.Link())

View file

@ -302,7 +302,6 @@ func UpdateViewedFiles(ctx *context.Context) {
updatedFiles := make(map[string]pull_model.ViewedState, len(data.Files))
for file, viewed := range data.Files {
// Only unviewed and viewed are possible, has-changed can not be set from the outside
state := pull_model.Unviewed
if viewed {

View file

@ -352,7 +352,6 @@ func loadLatestCommitData(ctx *context.Context, latestCommit *git.Commit) bool {
// or of directory if not in root directory.
ctx.Data["LatestCommit"] = latestCommit
if latestCommit != nil {
verification := asymkey_model.ParseCommitWithSignature(ctx, latestCommit)
if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) {

View file

@ -102,23 +102,11 @@ func WebfingerQuery(ctx *context.Context) {
default:
ctx.Error(http.StatusNotFound)
return
}
case 4:
//nolint:gocritic
if parts[3] == "teams" {
ctx.Error(http.StatusNotFound)
return
} else {
ctx.Error(http.StatusNotFound)
return
}
default:
ctx.Error(http.StatusNotFound)
return
}
default: