-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathR017.go
58 lines (44 loc) · 1.34 KB
/
R017.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
package R017
import (
"go/ast"
"golang.org/x/tools/go/analysis"
"github.com/bflad/tfproviderlint/helper/astutils"
"github.com/bflad/tfproviderlint/passes/commentignore"
"github.com/bflad/tfproviderlint/passes/helper/schema/resourcedatasetidcallexpr"
)
const Doc = `check for (*schema.ResourceData).SetId() usage with unstable time.Now() value
Schema attributes should be stable across Terraform runs.`
const analyzerName = "R017"
var Analyzer = &analysis.Analyzer{
Name: analyzerName,
Doc: Doc,
Requires: []*analysis.Analyzer{
commentignore.Analyzer,
resourcedatasetidcallexpr.Analyzer,
},
Run: run,
}
func run(pass *analysis.Pass) (interface{}, error) {
ignorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer)
callExprs := pass.ResultOf[resourcedatasetidcallexpr.Analyzer].([]*ast.CallExpr)
for _, callExpr := range callExprs {
if ignorer.ShouldIgnore(analyzerName, callExpr) {
continue
}
if len(callExpr.Args) < 1 {
continue
}
ast.Inspect(callExpr.Args[0], func(n ast.Node) bool {
callExpr, ok := n.(*ast.CallExpr)
if !ok {
return true
}
if astutils.IsStdlibPackageFunc(callExpr.Fun, pass.TypesInfo, "time", "Now") {
pass.Reportf(callExpr.Pos(), "%s: schema attributes should be stable across Terraform runs", analyzerName)
return false
}
return true
})
}
return nil, nil
}