@@ -16,50 +16,20 @@ package rules
16
16
17
17
import (
18
18
"go/ast"
19
- "go/types"
20
- "regexp"
21
19
22
20
gas "github.com/GoASTScanner/gas/core"
23
21
)
24
22
25
23
type WeakRand struct {
26
24
gas.MetaData
27
- pattern * regexp. Regexp
25
+ funcName string
28
26
packagePath string
29
27
}
30
28
31
- func matchFuncCall (n ast.Node , c * gas.Context ) (types.Object , * ast.Ident ) {
32
- call , ok := n .(* ast.CallExpr )
33
- if ! ok {
34
- return nil , nil
35
- }
36
-
37
- sel , ok := call .Fun .(* ast.SelectorExpr )
38
- if ! ok {
39
- return nil , nil
40
- }
41
-
42
- id , ok := sel .X .(* ast.Ident )
43
- if ! ok {
44
- return nil , nil
45
- }
46
-
47
- return c .Info .ObjectOf (id ), sel .Sel
48
- }
49
-
50
29
func (w * WeakRand ) Match (n ast.Node , c * gas.Context ) (* gas.Issue , error ) {
51
- o , f := matchFuncCall (n , c )
52
-
53
- if o == nil || f == nil {
54
- return nil , nil
55
- }
56
-
57
- pkg , ok := o .(* types.PkgName )
58
- if ! ok {
59
- return nil , nil
60
- }
30
+ node , _ := gas .MatchCallByPackage (n , c , w .packagePath , w .funcName )
61
31
62
- if pkg . Imported (). Path () == w . packagePath && w . pattern . MatchString ( f . String ()) {
32
+ if node != nil {
63
33
return gas .NewIssue (c , n , w .What , w .Severity , w .Confidence ), nil
64
34
}
65
35
@@ -68,7 +38,7 @@ func (w *WeakRand) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
68
38
69
39
func NewWeakRandCheck (conf map [string ]interface {}) (r gas.Rule , n ast.Node ) {
70
40
r = & WeakRand {
71
- pattern : regexp . MustCompile ( `^ Read$` ) ,
41
+ funcName : " Read" ,
72
42
packagePath : "math/rand" ,
73
43
MetaData : gas.MetaData {
74
44
Severity : gas .High ,
0 commit comments