-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlpassctxcheck.go
51 lines (45 loc) · 1.17 KB
/
sqlpassctxcheck.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package sqlpassctxcheck
import (
"fmt"
"github.com/gostaticanalysis/analysisutil"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/buildssa"
)
var Analyzer = &analysis.Analyzer{
Name: "sqlpassctxcheck",
Doc: "check for sql module method call without ctx",
Run: run,
Requires: []*analysis.Analyzer{
buildssa.Analyzer,
},
}
func run(pass *analysis.Pass) (interface{}, error) {
s, ok := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA)
if !ok {
return nil, fmt.Errorf("failed to get SSA")
}
restrictedMethods := getRestrictedMethodFuncList(pass)
pass.Report = analysisutil.ReportWithoutIgnore(pass)
for _, sf := range s.SrcFuncs {
for _, b := range sf.Blocks {
for _, instr := range b.Instrs {
for _, m := range restrictedMethods {
if analysisutil.Called(instr, nil, m.method) {
pass.Reportf(
instr.Pos(),
"use (*%s.%s).%s instead of (*%s.%s).%s",
m.definition.PackagePath,
m.definition.ReceiverType,
m.definition.AlternateMethodName,
m.definition.PackagePath,
m.definition.ReceiverType,
m.definition.MethodName,
)
break
}
}
}
}
}
return nil, nil
}