Skip to content

Commit 244cc10

Browse files
committed
Support any
1 parent 7fe9237 commit 244cc10

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

forcetypeassert.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package forcetypeassert
22

33
import (
44
"go/ast"
5+
"go/types"
56
"reflect"
67

78
"golang.org/x/tools/go/analysis"
@@ -42,7 +43,9 @@ func (p *Panicable) At(i int) ast.Node {
4243

4344
const Doc = "forcetypeassert is finds type assertions which did forcely"
4445

45-
func run(pass *analysis.Pass) (interface{}, error) {
46+
var anyTyp = types.Universe.Lookup("any").Type()
47+
48+
func run(pass *analysis.Pass) (any, error) {
4649
inspect, _ := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
4750
result := &Panicable{m: make(map[ast.Node]bool)}
4851

@@ -62,7 +65,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
6265
case *ast.ValueSpec:
6366
return checkValueSpec(pass, result, n)
6467
case *ast.TypeAssertExpr:
65-
if n.Type != nil {
68+
if n.Type != nil && !isAny(pass, n.Type) {
6669
result.m[n] = true
6770
result.nodes = append(result.nodes, n)
6871
pass.Reportf(n.Pos(), "type assertion must be checked")
@@ -76,6 +79,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
7679
return result, nil
7780
}
7881

82+
func isAny(pass *analysis.Pass, expr ast.Expr) bool {
83+
return types.Identical(pass.TypesInfo.TypeOf(expr), anyTyp)
84+
}
85+
7986
func checkAssignStmt(pass *analysis.Pass, result *Panicable, n *ast.AssignStmt) bool {
8087
tae := findTypeAssertion(n.Rhs)
8188
if tae == nil {
@@ -87,7 +94,7 @@ func checkAssignStmt(pass *analysis.Pass, result *Panicable, n *ast.AssignStmt)
8794
case len(n.Rhs) > 1:
8895
pass.Reportf(n.Pos(), "right hand must be only type assertion")
8996
return false
90-
case len(n.Lhs) != 2 && tae.Type != nil:
97+
case len(n.Lhs) != 2 && tae.Type != nil && !isAny(pass, tae.Type):
9198
result.m[n] = true
9299
result.nodes = append(result.nodes, n)
93100
pass.Reportf(n.Pos(), "type assertion must be checked")
@@ -110,7 +117,7 @@ func checkValueSpec(pass *analysis.Pass, result *Panicable, n *ast.ValueSpec) bo
110117
case len(n.Values) > 1:
111118
pass.Reportf(n.Pos(), "right hand must be only type assertion")
112119
return false
113-
case len(n.Names) != 2 && tae.Type != nil:
120+
case len(n.Names) != 2 && tae.Type != nil && !isAny(pass, tae.Type):
114121
result.m[n] = true
115122
result.nodes = append(result.nodes, n)
116123
pass.Reportf(n.Pos(), "type assertion must be checked")

testdata/src/a/a.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package a
22

33
import "fmt"
44

5-
var _, _ = ((interface{})(nil)).(string) // OK
6-
var _ = ((interface{})(nil)).(string) // want `type assertion must be checked`
7-
var _, _ = ((interface{})(nil)).(string), true // want `right hand must be only type assertion`
5+
var _, _ = any(nil).(string) // OK
6+
var _ = any(nil).(any) // OK for issue#17
7+
var _ = any(nil).(string) // want `type assertion must be checked`
8+
var _, _ = any(nil).(string), true // want `right hand must be only type assertion`
89

910
func f() {
10-
var i interface{} = "hello"
11+
var i any = "hello"
1112

1213
_ = i.(string) // want `type assertion must be checked`
1314

testdata/src/b/b.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package b
22

33
import "fmt"
44

5-
var _, _ = ((interface{})(nil)).(string) // OK
6-
var _ = ((interface{})(nil)).(string) // want `panicable`
7-
var _, _ = ((interface{})(nil)).(string), true // OK
5+
var _, _ = any(nil).(string) // OK
6+
var _ = any(nil).(string) // want `panicable`
7+
var _, _ = any(nil).(string), true // OK
88

99
func f() {
10-
var i interface{} = "hello"
10+
var i any = "hello"
1111

1212
_ = i.(string) // want `panicable`
1313

0 commit comments

Comments
 (0)