chore: enable forcetypeassert linter, fix issues (#38804)

Enable [`forcetypeassert`](https://github.com/gostaticanalysis/forcetypeassert)
linter to prevent unchecked type assertions. ~650 issues fixed, most
fixes were clean, some use `setting.PanicInDevOrTesting`.

The only behaviour changes are where code would previously send a 500 error
or panic, a 4xx error is now emitted.

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
silverwind
2026-08-09 12:25:06 +02:00
committed by GitHub
parent fac8bf2eca
commit 76a81b24f9
248 changed files with 1110 additions and 995 deletions
+3 -2
View File
@@ -122,8 +122,9 @@ func AccessLogger() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
start := time.Now()
next.ServeHTTP(w, req)
recorder.record(start, w.(ResponseWriter), req)
respWriter := WrapResponseWriter(w)
next.ServeHTTP(respWriter, req)
recorder.record(start, respWriter, req)
})
}
}
+3 -2
View File
@@ -20,6 +20,7 @@ import (
"gitea.dev/modules/git"
"gitea.dev/modules/httpcache"
"gitea.dev/modules/log"
"gitea.dev/modules/reqctx"
"gitea.dev/modules/setting"
"gitea.dev/modules/util"
"gitea.dev/modules/web"
@@ -56,7 +57,7 @@ func (ctx *APIContext) TokenCanAccessRepo(repo *repo_model.Repository) bool {
func init() {
web.RegisterResponseStatusProvider[*APIContext](func(req *http.Request) web_types.ResponseStatusProvider {
return req.Context().Value(apiContextKey).(*APIContext)
return GetAPIContext(req)
})
}
@@ -185,7 +186,7 @@ var apiContextKey = apiContextKeyType{}
// GetAPIContext returns a context for API routes
func GetAPIContext(req *http.Request) *APIContext {
return req.Context().Value(apiContextKey).(*APIContext)
return reqctx.MustContextValue[*APIContext](req.Context(), apiContextKey)
}
func genAPILinks(curURL *url.URL, total int64, pageSize, curPage int) []string {
+2 -2
View File
@@ -65,10 +65,10 @@ type Context struct {
func init() {
web.RegisterResponseStatusProvider[*Base](func(req *http.Request) web_types.ResponseStatusProvider {
return req.Context().Value(BaseContextKey).(*Base)
return reqctx.MustContextValue[*Base](req.Context(), BaseContextKey)
})
web.RegisterResponseStatusProvider[*Context](func(req *http.Request) web_types.ResponseStatusProvider {
return req.Context().Value(WebContextKey).(*Context)
return reqctx.MustContextValue[*Context](req.Context(), WebContextKey)
})
}
+2 -2
View File
@@ -29,11 +29,11 @@ func NewTemplateContext(ctx context.Context, req *http.Request) TemplateContext
}
func (c TemplateContext) req() *http.Request {
return c["_req"].(*http.Request)
return c["_req"].(*http.Request) //nolint:forcetypeassert // must exist
}
func (c TemplateContext) parentContext() context.Context {
return c["_ctx"].(context.Context)
return c["_ctx"].(context.Context) //nolint:forcetypeassert // must exist
}
func (c TemplateContext) Deadline() (deadline time.Time, ok bool) {
+3 -2
View File
@@ -13,6 +13,7 @@ import (
"gitea.dev/modules/log"
"gitea.dev/modules/private"
"gitea.dev/modules/process"
"gitea.dev/modules/reqctx"
"gitea.dev/modules/web"
web_types "gitea.dev/modules/web/types"
)
@@ -27,7 +28,7 @@ type PrivateContext struct {
func init() {
web.RegisterResponseStatusProvider[*PrivateContext](func(req *http.Request) web_types.ResponseStatusProvider {
return req.Context().Value(privateContextKey).(*PrivateContext)
return GetPrivateContext(req)
})
}
@@ -67,7 +68,7 @@ type privateContextKeyType struct{}
var privateContextKey privateContextKeyType
func GetPrivateContext(req *http.Request) *PrivateContext {
return req.Context().Value(privateContextKey).(*PrivateContext)
return reqctx.MustContextValue[*PrivateContext](req.Context(), privateContextKey)
}
func PrivateContexter() func(http.Handler) http.Handler {
+3 -3
View File
@@ -193,8 +193,8 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
willSign, signKey, _, err := asymkey_service.SignCRUDAction(ctx, doer, targetGitRepo, refName.String())
wontSignReason := ""
if asymkey_service.IsErrWontSign(err) {
wontSignReason = string(err.(*asymkey_service.ErrWontSign).Reason)
if errWontSign, ok := err.(*asymkey_service.ErrWontSign); ok {
wontSignReason = string(errWontSign.Reason)
} else if err != nil {
return nil, err
}
@@ -964,7 +964,7 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
ctx.Repo.RefFullName = repoRefFullName(refType, refShortName)
isRenamedBranch, has := ctx.Data["IsRenamedBranch"].(bool)
if isRenamedBranch && has {
renamedBranchName := ctx.Data["RenamedBranchName"].(string)
renamedBranchName := ctx.Data["RenamedBranchName"].(string) //nolint:forcetypeassert // must exist
ctx.Flash.Info(ctx.Tr("repo.branch.renamed", refShortName, renamedBranchName))
link := setting.AppSubURL + strings.Replace(ctx.Req.URL.EscapedPath(), util.PathEscapeSegments(refShortName), util.PathEscapeSegments(renamedBranchName), 1)
ctx.Redirect(link)