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
+1 -1
View File
@@ -153,7 +153,7 @@ func SystemStatus(ctx *context.Context) {
// DashboardPost run an admin operation
func DashboardPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.AdminDashboardForm)
form := web.GetForm[*forms.AdminDashboardForm](ctx)
ctx.Data["Title"] = ctx.Tr("admin.dashboard")
ctx.Data["PageIsAdminDashboard"] = true
updateSystemStatus()
+14 -18
View File
@@ -235,7 +235,7 @@ func parseSSPIConfig(ctx *context.Context, form forms.AuthenticationForm) (*sspi
// NewAuthSourcePost response for adding an auth source
func NewAuthSourcePost(ctx *context.Context) {
form := *web.GetForm(ctx).(*forms.AuthenticationForm)
form := *web.GetForm[*forms.AuthenticationForm](ctx)
ctx.Data["Title"] = ctx.Tr("admin.auths.new")
ctx.Data["PageIsAdminAuthentications"] = true
@@ -268,8 +268,8 @@ func NewAuthSourcePost(ctx *context.Context) {
EmailDomain: form.PAMEmailDomain,
}
case auth.OAuth2:
config = parseOAuth2Config(form)
oauth2Config := config.(*oauth2.Source)
oauth2Config := parseOAuth2Config(form)
config = oauth2Config
if oauth2Config.Provider == "openidConnect" {
discoveryURL, err := url.Parse(oauth2Config.OpenIDConnectAutoDiscoveryURL)
if err != nil || (discoveryURL.Scheme != "http" && discoveryURL.Scheme != "https") {
@@ -310,13 +310,12 @@ func NewAuthSourcePost(ctx *context.Context) {
TwoFactorPolicy: form.TwoFactorPolicy,
Cfg: config,
}); err != nil {
if auth.IsErrSourceAlreadyExist(err) {
if errExist, ok := errors.AsType[auth.ErrSourceAlreadyExist](err); ok {
ctx.Data["Err_Name"] = true
ctx.RenderWithErrDeprecated(ctx.Tr("admin.auths.login_source_exist", err.(auth.ErrSourceAlreadyExist).Name), tplAuthNew, form)
} else if oauth2.IsErrOpenIDConnectInitialize(err) {
ctx.RenderWithErrDeprecated(ctx.Tr("admin.auths.login_source_exist", errExist.Name), tplAuthNew, form)
} else if errInit, ok := err.(oauth2.ErrOpenIDConnectInitialize); ok {
ctx.Data["Err_DiscoveryURL"] = true
unwrapped := err.(oauth2.ErrOpenIDConnectInitialize).Unwrap()
ctx.RenderWithErrDeprecated(ctx.Tr("admin.auths.unable_to_initialize_openid", unwrapped), tplAuthNew, form)
ctx.RenderWithErrDeprecated(ctx.Tr("admin.auths.unable_to_initialize_openid", errInit.Unwrap()), tplAuthNew, form)
} else {
ctx.ServerError("auth.CreateSource", err)
}
@@ -348,12 +347,9 @@ func EditAuthSource(ctx *context.Context) {
ctx.Data["HasTLS"] = source.HasTLS()
if source.IsOAuth2() {
type Named interface {
Name() string
}
oauth2Source := auth.MustSourceCfg[*oauth2.Source](source)
for _, provider := range oauth2providers {
if provider.Name() == source.Cfg.(Named).Name() {
if provider.Name() == oauth2Source.Name() {
ctx.Data["CurrentOAuth2Provider"] = provider
break
}
@@ -365,7 +361,7 @@ func EditAuthSource(ctx *context.Context) {
// EditAuthSourcePost response for editing auth source
func EditAuthSourcePost(ctx *context.Context) {
form := *web.GetForm(ctx).(*forms.AuthenticationForm)
form := *web.GetForm[*forms.AuthenticationForm](ctx)
ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
ctx.Data["PageIsAdminAuthentications"] = true
@@ -398,8 +394,8 @@ func EditAuthSourcePost(ctx *context.Context) {
EmailDomain: form.PAMEmailDomain,
}
case auth.OAuth2:
config = parseOAuth2Config(form)
oauth2Config := config.(*oauth2.Source)
oauth2Config := parseOAuth2Config(form)
config = oauth2Config
if oauth2Config.Provider == "openidConnect" {
discoveryURL, err := url.Parse(oauth2Config.OpenIDConnectAutoDiscoveryURL)
if err != nil || (discoveryURL.Scheme != "http" && discoveryURL.Scheme != "https") {
@@ -425,9 +421,9 @@ func EditAuthSourcePost(ctx *context.Context) {
source.Cfg = config
source.TwoFactorPolicy = form.TwoFactorPolicy
if err := auth.UpdateSource(ctx, source); err != nil {
if auth.IsErrSourceAlreadyExist(err) {
if errExist, ok := errors.AsType[auth.ErrSourceAlreadyExist](err); ok {
ctx.Data["Err_Name"] = true
ctx.RenderWithErrDeprecated(ctx.Tr("admin.auths.login_source_exist", err.(auth.ErrSourceAlreadyExist).Name), tplAuthEdit, form)
ctx.RenderWithErrDeprecated(ctx.Tr("admin.auths.login_source_exist", errExist.Name), tplAuthEdit, form)
} else if oauth2.IsErrOpenIDConnectInitialize(err) {
ctx.Flash.Error(err.Error(), true)
ctx.Data["Err_DiscoveryURL"] = true
+3 -4
View File
@@ -54,7 +54,7 @@ func NewBadge(ctx *context.Context) {
// NewBadgePost response for adding a new badge
func NewBadgePost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.AdminCreateBadgeForm)
form := web.GetForm[*forms.AdminCreateBadgeForm](ctx)
if ctx.HasError() {
ctx.JSONError(ctx.GetErrMsg())
@@ -100,12 +100,11 @@ func ViewBadge(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("admin.badges.details")
ctx.Data["PageIsAdminBadges"] = true
prepareBadgeInfo(ctx)
badge := prepareBadgeInfo(ctx)
if ctx.Written() {
return
}
badge := ctx.Data["Badge"].(*user_model.Badge)
opts := &user_model.GetBadgeUsersOptions{
ListOptions: db.ListOptions{
Page: 1,
@@ -143,7 +142,7 @@ func EditBadgePost(ctx *context.Context) {
return
}
form := web.GetForm(ctx).(*forms.AdminEditBadgeForm)
form := web.GetForm[*forms.AdminEditBadgeForm](ctx)
if ctx.HasError() {
ctx.JSONError(ctx.GetErrMsg())
return
+12 -9
View File
@@ -103,7 +103,7 @@ func NewUser(ctx *context.Context) {
// NewUserPost response for adding a new user
func NewUserPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.AdminCreateUserForm)
form := web.GetForm[*forms.AdminCreateUserForm](ctx)
ctx.Data["Title"] = ctx.Tr("admin.users.new_account")
ctx.Data["PageIsAdminUsers"] = true
ctx.Data["DefaultUserVisibilityMode"] = setting.Service.DefaultUserVisibilityMode
@@ -171,6 +171,9 @@ func NewUserPost(ctx *context.Context) {
}
if err := user_model.AdminCreateUser(ctx, u, &user_model.Meta{}, overwriteDefault); err != nil {
var errNameReserved db.ErrNameReserved
var errNamePatternNotAllowed db.ErrNamePatternNotAllowed
var errNameCharsNotAllowed db.ErrNameCharsNotAllowed
switch {
case user_model.IsErrUserAlreadyExist(err):
ctx.Data["Err_UserName"] = true
@@ -181,15 +184,15 @@ func NewUserPost(ctx *context.Context) {
case user_model.IsErrEmailInvalid(err), user_model.IsErrEmailCharIsNotSupported(err):
ctx.Data["Err_Email"] = true
ctx.RenderWithErrDeprecated(ctx.Tr("form.email_invalid"), tplUserNew, &form)
case db.IsErrNameReserved(err):
case errors.As(err, &errNameReserved):
ctx.Data["Err_UserName"] = true
ctx.RenderWithErrDeprecated(ctx.Tr("user.form.name_reserved", err.(db.ErrNameReserved).Name), tplUserNew, &form)
case db.IsErrNamePatternNotAllowed(err):
ctx.RenderWithErrDeprecated(ctx.Tr("user.form.name_reserved", errNameReserved.Name), tplUserNew, &form)
case errors.As(err, &errNamePatternNotAllowed):
ctx.Data["Err_UserName"] = true
ctx.RenderWithErrDeprecated(ctx.Tr("user.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tplUserNew, &form)
case db.IsErrNameCharsNotAllowed(err):
ctx.RenderWithErrDeprecated(ctx.Tr("user.form.name_pattern_not_allowed", errNamePatternNotAllowed.Pattern), tplUserNew, &form)
case errors.As(err, &errNameCharsNotAllowed):
ctx.Data["Err_UserName"] = true
ctx.RenderWithErrDeprecated(ctx.Tr("user.form.name_chars_not_allowed", err.(db.ErrNameCharsNotAllowed).Name), tplUserNew, &form)
ctx.RenderWithErrDeprecated(ctx.Tr("user.form.name_chars_not_allowed", errNameCharsNotAllowed.Name), tplUserNew, &form)
default:
ctx.ServerError("CreateUser", err)
}
@@ -336,7 +339,7 @@ func EditUserPost(ctx *context.Context) {
return
}
form := web.GetForm(ctx).(*forms.AdminEditUserForm)
form := web.GetForm[*forms.AdminEditUserForm](ctx)
if ctx.HasError() {
ctx.HTML(http.StatusOK, tplUserEdit)
return
@@ -522,7 +525,7 @@ func AvatarPost(ctx *context.Context) {
return
}
form := web.GetForm(ctx).(*forms.AvatarForm)
form := web.GetForm[*forms.AvatarForm](ctx)
if err := user_setting.UpdateAvatarSetting(ctx, form, u); err != nil {
ctx.Flash.Error(err.Error())
} else {