@@ -24,38 +24,78 @@ func run(pass *analysis.Pass) (interface{}, error) {
24
24
25
25
nodeFilter := []ast.Node {
26
26
(* ast .AssignStmt )(nil ),
27
+ (* ast .ValueSpec )(nil ),
27
28
}
28
29
29
30
inspect .Preorder (nodeFilter , func (n ast.Node ) {
30
31
switch n := n .(type ) {
31
32
case * ast.AssignStmt :
32
- if ! hasTypeAssertion (n .Rhs ) {
33
- return
34
- }
35
- // if right hand has 2 or more values, assign statement can't assert boolean value which describes type assertion is succeeded
36
- if len (n .Rhs ) > 1 {
37
- pass .Reportf (n .Pos (), "right hand must be only type assertion" )
38
- return
39
- }
40
- if len (n .Lhs ) == 2 {
41
- return
42
- }
43
-
44
- tae , ok := n .Rhs [0 ].(* ast.TypeAssertExpr )
45
- if ! ok {
46
- pass .Reportf (n .Pos (), "right hand is not TypeAssertion" )
47
- return
48
- }
49
- if tae .Type == nil {
50
- return
51
- }
52
- pass .Reportf (n .Pos (), "type assertion must be checked" )
33
+ checkAssignStmt (pass , n )
34
+ case * ast.ValueSpec :
35
+ checkValueSpec (pass , n )
53
36
}
54
37
})
55
38
56
39
return nil , nil
57
40
}
58
41
42
+ func checkAssignStmt (pass * analysis.Pass , n * ast.AssignStmt ) {
43
+ if ! hasTypeAssertion (n .Rhs ) {
44
+ return
45
+ }
46
+
47
+ // if right hand has 2 or more values, assign statement can't assert boolean value which describes type assertion is succeeded
48
+ if len (n .Rhs ) > 1 {
49
+ pass .Reportf (n .Pos (), "right hand must be only type assertion" )
50
+ return
51
+ }
52
+
53
+ if len (n .Lhs ) == 2 {
54
+ return
55
+ }
56
+
57
+ tae , ok := n .Rhs [0 ].(* ast.TypeAssertExpr )
58
+ if ! ok {
59
+ pass .Reportf (n .Pos (), "right hand is not TypeAssertion" )
60
+ return
61
+ }
62
+
63
+ if tae .Type == nil {
64
+ return
65
+ }
66
+
67
+ pass .Reportf (n .Pos (), "type assertion must be checked" )
68
+ }
69
+
70
+ func checkValueSpec (pass * analysis.Pass , n * ast.ValueSpec ) {
71
+ if ! hasTypeAssertion (n .Values ) {
72
+ return
73
+ }
74
+
75
+ // if right hand has 2 or more values, assign statement can't assert boolean value which describes type assertion is succeeded
76
+ if len (n .Values ) > 1 {
77
+ pass .Reportf (n .Pos (), "right hand must be only type assertion" )
78
+ return
79
+ }
80
+
81
+ if len (n .Names ) == 2 {
82
+ return
83
+ }
84
+
85
+ tae , ok := n .Values [0 ].(* ast.TypeAssertExpr )
86
+ if ! ok {
87
+ pass .Reportf (n .Pos (), "right hand is not TypeAssertion" )
88
+ return
89
+ }
90
+
91
+ if tae .Type == nil {
92
+ return
93
+ }
94
+
95
+ pass .Reportf (n .Pos (), "type assertion must be checked" )
96
+
97
+ }
98
+
59
99
func hasTypeAssertion (exprs []ast.Expr ) bool {
60
100
for _ , node := range exprs {
61
101
_ , ok := node .(* ast.TypeAssertExpr )
0 commit comments