Skip to content

Commit

Permalink
Add some more cases to lint (PR feedback)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirk Faust committed Sep 14, 2023
1 parent de3b67b commit 4b5873b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
30 changes: 30 additions & 0 deletions rule/unchecked-type-assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,34 @@ func (w *lintUnchekedTypeAssertion) requireNoTypeAssert(expr ast.Expr) {
}
}

func (w *lintUnchekedTypeAssertion) handleIfStmt(n *ast.IfStmt) {
ifCondition, ok := n.Cond.(*ast.BinaryExpr)
if !ok {
return
}

w.requireNoTypeAssert(ifCondition.X)
w.requireNoTypeAssert(ifCondition.Y)
}

func (w *lintUnchekedTypeAssertion) requireBinaryExpressionWithoutTypeAssertion(expr ast.Expr) {
binaryExpr, ok := expr.(*ast.BinaryExpr)
if ok {
w.requireNoTypeAssert(binaryExpr.X)
w.requireNoTypeAssert(binaryExpr.Y)
}
}

func (w *lintUnchekedTypeAssertion) handleCaseClause(n *ast.CaseClause) {
for _, expr := range n.List {
w.requireNoTypeAssert(expr)
w.requireBinaryExpressionWithoutTypeAssertion(expr)
}
}

func (w *lintUnchekedTypeAssertion) handleSwitch(n *ast.SwitchStmt) {
w.requireNoTypeAssert(n.Tag)
w.requireBinaryExpressionWithoutTypeAssertion(n.Tag)
}

func (w *lintUnchekedTypeAssertion) handleAssignment(n *ast.AssignStmt) {
Expand Down Expand Up @@ -144,6 +170,10 @@ func (w *lintUnchekedTypeAssertion) Visit(node ast.Node) ast.Visitor {
w.handleReturn(n)
case *ast.AssignStmt:
w.handleAssignment(n)
case *ast.IfStmt:
w.handleIfStmt(n)
case *ast.CaseClause:
w.handleCaseClause(n)
}

return w
Expand Down
50 changes: 47 additions & 3 deletions testdata/unchecked-type-assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,51 @@ func handleTypeSwitchWithAssignment() {
}
}

func handleTypeSwitchReturn() {
// Should not be a lint
return foo.(type)
func handleTypeComparison() {
if foo.(int) == 1 { // MATCH /type cast result is unchecked in foo.(int) - type assertion will panic if not matched/
return
}
}

func handleTypeComparisonReverse() {
if foo.(int) == 1 { // MATCH /type cast result is unchecked in foo.(int) - type assertion will panic if not matched/
return
}
}

func handleTypeAssignmentComparison() {
var value any
value = 42 // int

if v := value.(int); v == 42 { // MATCH /type cast result is unchecked in value.(int) - type assertion will panic if not matched/
fmt.Printf("Value is an integer: %d\n", v)
}
}

func handleSwitchComparison() {
switch foo.(int) == 1 { // MATCH /type cast result is unchecked in foo.(int) - type assertion will panic if not matched/
case true:
case false:
}
}

func handleSwitchComparisonReverse() {
switch 1 == foo.(int) { // MATCH /type cast result is unchecked in foo.(int) - type assertion will panic if not matched/
case true:
case false:
}
}

func handleInnerSwitchAssertion() {
switch {
case foo.(int) == 1: // MATCH /type cast result is unchecked in foo.(int) - type assertion will panic if not matched/
case bar.(int) == 1: // MATCH /type cast result is unchecked in bar.(int) - type assertion will panic if not matched/
}
}

func handleInnerSwitchAssertionReverse() {
switch {
case 1 == foo.(int): // MATCH /type cast result is unchecked in foo.(int) - type assertion will panic if not matched/
case 1 == bar.(int): // MATCH /type cast result is unchecked in bar.(int) - type assertion will panic if not matched/
}
}

0 comments on commit 4b5873b

Please sign in to comment.