-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlRowsClose_checker.go
144 lines (127 loc) · 3.48 KB
/
sqlRowsClose_checker.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package checkers
import (
"go/ast"
"go/types"
"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
)
func init() {
var info lintpack.CheckerInfo
info.Name = "sqlRowsClose"
info.Tags = []string{"diagnostic", "experimental"}
info.Summary = "Detects uses of *sql.Rows without call Close method"
info.Before = `
rows, _ := db.Query( /**/ )
for rows.Next {
}`
info.After = `
rows, _ := db.Query( /**/ )
for rows.Next {
}
rows.Close()`
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
return astwalk.WalkerForFuncDecl(&sqlRowsCloseChecker{ctx: ctx})
})
}
type sqlRowsCloseChecker struct {
astwalk.WalkHandler
ctx *lintpack.CheckerContext
}
// Warning if sql.Rows local variables (including function parameters):
// 1. Not using as parameter in other functions call;
// 2. Not returning in functions results;
// 3. Not call Close method for variable;
func (c *sqlRowsCloseChecker) VisitFuncDecl(decl *ast.FuncDecl) {
const rowsTypePTR = "*database/sql.Rows"
localVars := []types.Object{}
returnVars := []types.Object{}
closeVars := []types.Object{}
for _, b := range decl.Body.List {
switch b := b.(type) {
case *ast.AssignStmt:
// Detect local vars with sql.Rows types
if b.Lhs != nil {
for _, l := range b.Lhs {
if c.typeString(l) == rowsTypePTR {
localVars = append(localVars, c.getType(l))
}
}
}
case *ast.ReturnStmt:
// Detect return vars with sql.Rows types
if b.Results != nil && len(b.Results) > 0 {
for _, r := range b.Results {
if c.typeString(r) == rowsTypePTR {
returnVars = append(returnVars, c.getType(r))
}
}
}
case *ast.ExprStmt:
if sel := c.getCloseSelectorExpr(b.X); sel != nil {
closeVars = append(closeVars, c.getType(sel.X))
}
case *ast.DeferStmt:
// Detect call Close for sql.Rows variables over defer declaration
// is it `defer rowsVar.Close()`
if b, ok := b.Call.Fun.(*ast.SelectorExpr); ok {
funcName := qualifiedName(b.Sel)
if funcName == "Close" {
closeVars = append(closeVars, c.getType(b.X))
}
}
// looking for `rowsVar.Close()` inside `defer func() { ... }()`
if f, ok := b.Call.Fun.(*ast.FuncLit); ok {
if f.Body == nil || f.Body.List == nil {
continue
}
for _, s := range f.Body.List {
expr, ok := s.(*ast.ExprStmt)
if !ok {
continue
}
ss := c.getCloseSelectorExpr(expr.X)
if ss != nil {
closeVars = append(closeVars, c.getType(ss.X))
}
}
}
}
}
// Check local variables
for _, l := range localVars {
// If local variable present in return or Close present - PASS
if !c.varInList(l, returnVars) && !c.varInList(l, closeVars) {
c.ctx.Warn(l.Parent(), "local variable db.Rows have not Close call")
}
}
}
func (c *sqlRowsCloseChecker) getType(x ast.Node) types.Object {
return c.ctx.TypesInfo.ObjectOf(identOf(x))
}
func (c *sqlRowsCloseChecker) getCloseSelectorExpr(x ast.Node) *ast.SelectorExpr {
call, ok := x.(*ast.CallExpr)
if !ok {
return nil
}
if bb, ok := call.Fun.(*ast.SelectorExpr); ok {
// Detect call Close for sql.Rows variables
if qualifiedName(bb.Sel) == "Close" {
return bb
}
}
return nil
}
func (c *sqlRowsCloseChecker) typeString(x ast.Expr) string {
if typ := c.ctx.TypesInfo.TypeOf(x); typ != nil {
return typ.String()
}
return ""
}
func (c *sqlRowsCloseChecker) varInList(v types.Object, list []types.Object) bool {
for _, r := range list {
if v == r {
return true
}
}
return false
}