forked from nishanths/exhaustive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.go
More file actions
147 lines (128 loc) · 4.26 KB
/
map.go
File metadata and controls
147 lines (128 loc) · 4.26 KB
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
145
146
147
package sdmexhaustive
import (
"fmt"
"go/ast"
"go/types"
"regexp"
"golang.org/x/tools/go/analysis"
)
// mapConfig is configuration for mapChecker.
type mapConfig struct {
explicit bool
checkGenerated bool
ignoreConstant *regexp.Regexp // can be nil
ignoreType *regexp.Regexp // can be nil
packageDirective packageDirective
}
// mapChecker returns a node visitor that checks for exhaustiveness of
// map literals for the supplied pass, and reports diagnostics. The
// node visitor expects only *ast.CompositeLit nodes.
func mapChecker(pass *analysis.Pass, cfg mapConfig, generated boolCache, comments commentCache) nodeVisitor {
return func(n ast.Node, push bool, stack []ast.Node) (bool, string) {
if !push {
return true, resultNotPush
}
file := stack[0].(*ast.File)
if !cfg.checkGenerated && generated.get(file) {
return false, resultGeneratedFile
}
lit := n.(*ast.CompositeLit)
mapType, ok := pass.TypesInfo.Types[lit.Type].Type.(*types.Map)
if !ok {
namedType, ok2 := pass.TypesInfo.Types[lit.Type].Type.(*types.Named)
if !ok2 {
return true, resultNotMapLiteral
}
mapType, ok = namedType.Underlying().(*types.Map)
if !ok {
return true, resultNotMapLiteral
}
}
if len(lit.Elts) == 0 {
return false, resultEmptyMapLiteral
}
fileComments := comments.get(pass.Fset, file)
var relatedComments []*ast.CommentGroup
for i := range stack {
// iterate over stack in the reverse order (from inner
// node to outer node)
node := stack[len(stack)-1-i]
switch node.(type) {
// need to check comments associated with following nodes,
// because logic of ast package doesn't associate comment
// with *ast.CompositeLit as required.
case *ast.CompositeLit, // stack[len(stack)-1]
*ast.ReturnStmt, // return ...
*ast.IndexExpr, // map[enum]...{...}[key]
*ast.CallExpr, // myfunc(map...)
*ast.UnaryExpr, // &map...
*ast.AssignStmt, // variable assignment (without var keyword)
*ast.DeclStmt, // var declaration, parent of *ast.GenDecl
*ast.GenDecl, // var declaration, parent of *ast.ValueSpec
*ast.ValueSpec: // var declaration
relatedComments = append(relatedComments, fileComments[node]...)
continue
default:
// stop iteration on the first inappropriate node
break
}
}
directives, err := parseDirectives(relatedComments)
if err != nil {
pass.Report(makeInvalidDirectiveDiagnostic(lit, err))
}
enforced := directives.has(enforceDirective) || cfg.packageDirective == packageDirectiveEnforce
ignored := directives.has(ignoreDirective) || cfg.packageDirective == packageDirectiveIgnore
if ignored && !directives.has(enforceDirective) {
if !cfg.explicit || enforced {
// Skip checking of this map literal due to ignore
// directive (statement-level or package-level). A
// per-statement enforce directive overrides package-level
// ignore. Still return true because there may be nested
// map literals that are not to be ignored.
return true, resultIgnoreComment
}
}
if cfg.explicit && !enforced {
return true, resultNoEnforceComment
}
es, ok := composingEnumTypes(pass, mapType.Key())
if !ok || len(es) == 0 {
return true, resultEnumTypes
}
var checkl checklist
checkl.ignoreConstant(cfg.ignoreConstant)
checkl.ignoreType(cfg.ignoreType)
for _, e := range es {
checkl.add(e.typ, e.members, pass.Pkg == e.typ.Pkg())
}
analyzeMapLiteral(lit, pass.TypesInfo, checkl.found)
if len(checkl.remaining()) == 0 {
return true, resultEnumMembersAccounted
}
pass.Report(makeMapDiagnostic(lit, dedupEnumTypes(toEnumTypes(es)), checkl.remaining()))
return true, resultReportedDiagnostic
}
}
func analyzeMapLiteral(lit *ast.CompositeLit, info *types.Info, each func(constantValue)) {
for _, e := range lit.Elts {
expr, ok := e.(*ast.KeyValueExpr)
if !ok {
continue
}
if val, ok := exprConstVal(expr.Key, info); ok {
each(val)
}
}
}
func makeMapDiagnostic(lit *ast.CompositeLit, enumTypes []enumType, missing map[member]struct{}) analysis.Diagnostic {
return analysis.Diagnostic{
Pos: lit.Pos(),
End: lit.End(),
Message: fmt.Sprintf(
"missing keys in map of key type %s: %s",
diagnosticEnumTypes(enumTypes),
diagnosticGroups(groupify(missing, enumTypes)),
),
}
}