-
Notifications
You must be signed in to change notification settings - Fork 25
/
R009.go
76 lines (60 loc) · 2.22 KB
/
R009.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
package R009
import (
"go/ast"
"github.com/bflad/gopaniccheck/passes/logpaniccallexpr"
"github.com/bflad/gopaniccheck/passes/logpanicfcallexpr"
"github.com/bflad/gopaniccheck/passes/logpaniclncallexpr"
"github.com/bflad/gopaniccheck/passes/paniccallexpr"
"github.com/bflad/tfproviderlint/passes/commentignore"
"golang.org/x/tools/go/analysis"
)
const Doc = `check for Go panic usage
The R009 analyzer reports usage of Go panics, which should be avoided.
Any errors should be surfaced to Terraform, which will display them in the
user interface and ensures any necessary state actions (e.g. cleanup) are
performed as expected.`
const analyzerName = "R009"
var Analyzer = &analysis.Analyzer{
Name: analyzerName,
Doc: Doc,
Requires: []*analysis.Analyzer{
commentignore.Analyzer,
logpaniccallexpr.Analyzer,
logpanicfcallexpr.Analyzer,
logpaniclncallexpr.Analyzer,
paniccallexpr.Analyzer,
},
Run: run,
}
func run(pass *analysis.Pass) (interface{}, error) {
ignorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer)
logPanicCallExprs := pass.ResultOf[logpaniccallexpr.Analyzer].([]*ast.CallExpr)
logPanicfCallExprs := pass.ResultOf[logpanicfcallexpr.Analyzer].([]*ast.CallExpr)
logPaniclnCallExprs := pass.ResultOf[logpaniclncallexpr.Analyzer].([]*ast.CallExpr)
panicCallExprs := pass.ResultOf[paniccallexpr.Analyzer].([]*ast.CallExpr)
for _, logPanicCallExpr := range logPanicCallExprs {
if ignorer.ShouldIgnore(analyzerName, logPanicCallExpr) {
continue
}
pass.Reportf(logPanicCallExpr.Pos(), "%s: avoid log.Panic() usage", analyzerName)
}
for _, logPanicfCallExpr := range logPanicfCallExprs {
if ignorer.ShouldIgnore(analyzerName, logPanicfCallExpr) {
continue
}
pass.Reportf(logPanicfCallExpr.Pos(), "%s: avoid log.Panicf() usage", analyzerName)
}
for _, logPaniclnCallExpr := range logPaniclnCallExprs {
if ignorer.ShouldIgnore(analyzerName, logPaniclnCallExpr) {
continue
}
pass.Reportf(logPaniclnCallExpr.Pos(), "%s: avoid log.Panicln() usage", analyzerName)
}
for _, panicCallExpr := range panicCallExprs {
if ignorer.ShouldIgnore(analyzerName, panicCallExpr) {
continue
}
pass.Reportf(panicCallExpr.Pos(), "%s: avoid panic() usage", analyzerName)
}
return nil, nil
}