Skip to content

Commit b411d9d

Browse files
committed
fix(type assert): add more check for type assert
1 parent b65821b commit b411d9d

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

errcheck/errcheck.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -586,18 +586,29 @@ func (v *visitor) Visit(node ast.Node) ast.Visitor {
586586
for _, name := range vspec.Names {
587587
lhs = append(lhs, ast.Expr(name))
588588
}
589-
v.checkAssignment(lhs, vspec.Values)
589+
followed := v.checkAssignment(lhs, vspec.Values)
590+
if !followed {
591+
return nil
592+
}
590593
}
591594

592595
case *ast.AssignStmt:
593-
v.checkAssignment(stmt.Lhs, stmt.Rhs)
596+
followed := v.checkAssignment(stmt.Lhs, stmt.Rhs)
597+
if !followed {
598+
return nil
599+
}
600+
601+
case *ast.TypeAssertExpr:
602+
v.checkAssertExpr(stmt)
603+
return nil
594604

595605
default:
596606
}
597607
return v
598608
}
599609

600-
func (v *visitor) checkAssignment(lhs, rhs []ast.Expr) {
610+
func (v *visitor) checkAssignment(lhs, rhs []ast.Expr) (followed bool) {
611+
followed = true
601612
if len(rhs) == 1 {
602613
// single value on rhs; check against lhs identifiers
603614
if call, ok := rhs[0].(*ast.CallExpr); ok {
@@ -619,11 +630,11 @@ func (v *visitor) checkAssignment(lhs, rhs []ast.Expr) {
619630
}
620631
} else if assert, ok := rhs[0].(*ast.TypeAssertExpr); ok {
621632
if !v.asserts {
622-
return
633+
return false
623634
}
624635
if assert.Type == nil {
625636
// type switch
626-
return
637+
return false
627638
}
628639
if len(lhs) < 2 {
629640
// assertion result not read
@@ -632,6 +643,7 @@ func (v *visitor) checkAssignment(lhs, rhs []ast.Expr) {
632643
// assertion result ignored
633644
v.addErrorAtPosition(id.NamePos, nil)
634645
}
646+
return false
635647
}
636648
} else {
637649
// multiple value on rhs; in this case a call can't return
@@ -661,6 +673,21 @@ func (v *visitor) checkAssignment(lhs, rhs []ast.Expr) {
661673
}
662674
}
663675
}
676+
677+
return
678+
}
679+
680+
func (v *visitor) checkAssertExpr(expr *ast.TypeAssertExpr) bool {
681+
if !v.asserts {
682+
return false
683+
}
684+
if expr.Type == nil {
685+
// type switch
686+
return false
687+
}
688+
v.addErrorAtPosition(expr.Pos(), nil)
689+
690+
return false
664691
}
665692

666693
func isErrorType(t types.Type) bool {

errcheck/testdata/src/assert/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,21 @@ package assert
33
func main() {
44
var i interface{}
55
_ = i.(string) // want "unchecked error"
6+
7+
handleInterface(i.(string)) // want "unchecked error"
8+
9+
if i.(string) == "hello" { // want "unchecked error"
10+
//
11+
}
12+
13+
switch i.(type) {
14+
case string:
15+
case int:
16+
_ = i.(int) // want "unchecked error"
17+
case nil:
18+
}
19+
}
20+
21+
func handleInterface(i interface{}) string {
22+
return i.(string) // want "unchecked error"
623
}

0 commit comments

Comments
 (0)