Skip to content

Commit

Permalink
Also handle type assertions in channel send (PR feedback)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirk Faust committed Sep 14, 2023
1 parent 4b5873b commit aacc34a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 6 additions & 0 deletions rule/unchecked-type-assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ func (w *lintUnchekedTypeAssertion) handleRange(n *ast.RangeStmt) {
w.requireNoTypeAssert(n.X)
}

func (w *lintUnchekedTypeAssertion) handleChannelSend(n *ast.SendStmt) {
w.requireNoTypeAssert(n.Value)
}

func (w *lintUnchekedTypeAssertion) Visit(node ast.Node) ast.Visitor {
switch n := node.(type) {
case *ast.RangeStmt:
Expand All @@ -174,6 +178,8 @@ func (w *lintUnchekedTypeAssertion) Visit(node ast.Node) ast.Visitor {
w.handleIfStmt(n)
case *ast.CaseClause:
w.handleCaseClause(n)
case *ast.SendStmt:
w.handleChannelSend(n)
}

return w
Expand Down
8 changes: 7 additions & 1 deletion testdata/unchecked-type-assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func handleTypeComparison() {
}

func handleTypeComparisonReverse() {
if foo.(int) == 1 { // MATCH /type cast result is unchecked in foo.(int) - type assertion will panic if not matched/
if 1 == foo.(int) { // MATCH /type cast result is unchecked in foo.(int) - type assertion will panic if not matched/
return
}
}
Expand Down Expand Up @@ -100,3 +100,9 @@ func handleInnerSwitchAssertionReverse() {
case 1 == bar.(int): // MATCH /type cast result is unchecked in bar.(int) - type assertion will panic if not matched/
}
}

func handleChannelWrite() {
c := make(chan any)
var a any = "foo"
c <- a.(int) // MATCH /type cast result is unchecked in a.(int) - type assertion will panic if not matched/
}

0 comments on commit aacc34a

Please sign in to comment.