mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-11 21:48:41 +00:00
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:
@@ -145,7 +145,7 @@ func applyOp2(op operator, n1, n2 Num) Num {
|
||||
f2, _ := util.ToFloat64(n2.Value)
|
||||
return applyOp2Generic(op, f1, f2)
|
||||
}
|
||||
return applyOp2Generic(op, n1.Value.(int64), n2.Value.(int64))
|
||||
return applyOp2Generic(op, n1.Value.(int64), n2.Value.(int64)) //nolint:forcetypeassert // castFloat64 above already ruled out float
|
||||
}
|
||||
|
||||
func toOp(v any) (operator, error) {
|
||||
@@ -321,13 +321,13 @@ func fnSum(nums []Num) Num {
|
||||
if castFloat64(nums) {
|
||||
var sum float64
|
||||
for _, num := range nums {
|
||||
sum += num.Value.(float64)
|
||||
sum += num.Value.(float64) //nolint:forcetypeassert // castFloat64 reported every value is float64
|
||||
}
|
||||
return Num{sum}
|
||||
}
|
||||
var sum int64
|
||||
for _, num := range nums {
|
||||
sum += num.Value.(int64)
|
||||
sum += num.Value.(int64) //nolint:forcetypeassert // castFloat64 ruled out float, so every value is int64
|
||||
}
|
||||
return Num{sum}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func tokens(s string) (a []any) {
|
||||
@@ -21,7 +22,9 @@ func tokens(s string) (a []any) {
|
||||
func TestEval(t *testing.T) {
|
||||
n, err := Expr(0, "/", 0.0)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, math.IsNaN(n.Value.(float64)))
|
||||
nan, ok := n.Value.(float64)
|
||||
require.True(t, ok)
|
||||
assert.True(t, math.IsNaN(nan))
|
||||
|
||||
_, err = Expr(nil)
|
||||
assert.ErrorContains(t, err, "unsupported token type")
|
||||
|
||||
@@ -166,9 +166,9 @@ func newScopedTemplateSet(all *template.Template, name string) (*scopedTemplateS
|
||||
var collectErr error // only need to collect the one error
|
||||
collectTemplates = func(nodes []parse.Node) {
|
||||
for _, node := range nodes {
|
||||
if node.Type() == parse.NodeTemplate {
|
||||
nodeTemplate := node.(*parse.TemplateNode)
|
||||
subName := nodeTemplate.Name
|
||||
switch node := node.(type) {
|
||||
case *parse.TemplateNode:
|
||||
subName := node.Name
|
||||
if ts.htmlTemplates[subName] == nil {
|
||||
subTmpl := all.Lookup(subName)
|
||||
if subTmpl == nil {
|
||||
@@ -185,26 +185,22 @@ func newScopedTemplateSet(all *template.Template, name string) (*scopedTemplateS
|
||||
collectTemplates(subTmpl.Tree.Root.Nodes)
|
||||
}
|
||||
}
|
||||
} else if node.Type() == parse.NodeList {
|
||||
nodeList := node.(*parse.ListNode)
|
||||
collectTemplates(nodeList.Nodes)
|
||||
} else if node.Type() == parse.NodeIf {
|
||||
nodeIf := node.(*parse.IfNode)
|
||||
collectTemplates(nodeIf.BranchNode.List.Nodes)
|
||||
if nodeIf.BranchNode.ElseList != nil {
|
||||
collectTemplates(nodeIf.BranchNode.ElseList.Nodes)
|
||||
case *parse.ListNode:
|
||||
collectTemplates(node.Nodes)
|
||||
case *parse.IfNode:
|
||||
collectTemplates(node.BranchNode.List.Nodes)
|
||||
if node.BranchNode.ElseList != nil {
|
||||
collectTemplates(node.BranchNode.ElseList.Nodes)
|
||||
}
|
||||
} else if node.Type() == parse.NodeRange {
|
||||
nodeRange := node.(*parse.RangeNode)
|
||||
collectTemplates(nodeRange.BranchNode.List.Nodes)
|
||||
if nodeRange.BranchNode.ElseList != nil {
|
||||
collectTemplates(nodeRange.BranchNode.ElseList.Nodes)
|
||||
case *parse.RangeNode:
|
||||
collectTemplates(node.BranchNode.List.Nodes)
|
||||
if node.BranchNode.ElseList != nil {
|
||||
collectTemplates(node.BranchNode.ElseList.Nodes)
|
||||
}
|
||||
} else if node.Type() == parse.NodeWith {
|
||||
nodeWith := node.(*parse.WithNode)
|
||||
collectTemplates(nodeWith.BranchNode.List.Nodes)
|
||||
if nodeWith.BranchNode.ElseList != nil {
|
||||
collectTemplates(nodeWith.BranchNode.ElseList.Nodes)
|
||||
case *parse.WithNode:
|
||||
collectTemplates(node.BranchNode.List.Nodes)
|
||||
if node.BranchNode.ElseList != nil {
|
||||
collectTemplates(node.BranchNode.ElseList.Nodes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,10 @@ func NewRenderUtils(ctx reqctx.RequestContext) *RenderUtils {
|
||||
return &RenderUtils{ctx: ctx, avatarUtils: NewAvatarUtils(ctx)}
|
||||
}
|
||||
|
||||
func (ut *RenderUtils) locale() translation.Locale {
|
||||
return ut.ctx.Value(translation.ContextKey).(translation.Locale) //nolint:forcetypeassert // the render context always carries a locale
|
||||
}
|
||||
|
||||
// RenderCommitMessage renders commit message title (only title)
|
||||
func (ut *RenderUtils) RenderCommitMessage(msg string, repo *repo.Repository) template.HTML {
|
||||
msgLine := strings.TrimSpace(msg)
|
||||
@@ -98,7 +102,7 @@ func (ut *RenderUtils) RenderIssueSimpleTitle(text string) template.HTML {
|
||||
}
|
||||
|
||||
func (ut *RenderUtils) RenderLabel(label *issues_model.Label) template.HTML {
|
||||
locale := ut.ctx.Value(translation.ContextKey).(translation.Locale)
|
||||
locale := ut.locale()
|
||||
var extraCSSClasses string
|
||||
textColor := util.ContrastColor(label.Color)
|
||||
labelScope := label.ExclusiveScope()
|
||||
@@ -279,7 +283,7 @@ func (ut *RenderUtils) RenderUnicodeEscapeToggleButton(escapeStatus *charset.Esc
|
||||
if escapeStatus == nil || !escapeStatus.Escaped {
|
||||
return ""
|
||||
}
|
||||
locale := ut.ctx.Value(translation.ContextKey).(translation.Locale)
|
||||
locale := ut.locale()
|
||||
var title template.HTML
|
||||
if escapeStatus.HasAmbiguous {
|
||||
title += locale.Tr("repo.ambiguous_runes_line")
|
||||
@@ -376,7 +380,7 @@ func (ut *RenderUtils) AvatarStackPushCommit(pushCommit *repository.PushCommit)
|
||||
|
||||
// AvatarStackWithNames renders the avatar stack plus a label: `name` / `a and b` / `N people` (opens popup).
|
||||
func (ut *RenderUtils) AvatarStackWithNames(data *user_model.AvatarStackData) template.HTML {
|
||||
locale := ut.ctx.Value(translation.ContextKey).(translation.Locale)
|
||||
locale := ut.locale()
|
||||
participants := data.Participants
|
||||
|
||||
var b htmlutil.HTMLBuilder
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"gitea.dev/modules/htmlutil"
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/svg"
|
||||
"gitea.dev/modules/translation"
|
||||
"gitea.dev/modules/util"
|
||||
)
|
||||
|
||||
@@ -35,7 +34,7 @@ func (ut *RenderUtils) RenderTimelineEventBadge(c *issues_model.Comment) templat
|
||||
|
||||
func (ut *RenderUtils) RenderTimelineEventComment(c *issues_model.Comment, createdStr template.HTML) template.HTML {
|
||||
if c.Type == issues_model.CommentTypeChangeTitle {
|
||||
locale := ut.ctx.Value(translation.ContextKey).(translation.Locale)
|
||||
locale := ut.locale()
|
||||
isToggle, isWip := commentTimelineEventIsWipToggle(c)
|
||||
if !isToggle {
|
||||
return locale.Tr("repo.issues.change_title_at", ut.RenderEmoji(c.OldTitle), ut.RenderEmoji(c.NewTitle), createdStr)
|
||||
|
||||
Reference in New Issue
Block a user