-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyzer.go
67 lines (61 loc) · 1.73 KB
/
analyzer.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
// Package requiredfield implements a linter
// that checks for required fields during struct initialization.
//
// Fields can be marked as required by adding a comment in the form
// "// required" next to the field, optionally followed by a description.
// For example:
//
// type T struct {
// A string // required
// B int // required: must be positive
// C bool // required because reasons
// }
//
// The analyzer will report an error when an instance of the struct is
// initialized without setting one or more of the required fields explicitly.
// For example:
//
// T{A: "foo"} // error: missing required fields: B, C
//
// The explicit value can be the zero value of the field type,
// but it must be set explicitly.
//
// T{A: "foo", B: 0, C: false}
package requiredfield
import (
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
// Analyzer implements the requiredfield linter.
//
// See package documentation for details.
var Analyzer = &analysis.Analyzer{
Name: "requiredfield",
Doc: "check for required fields during struct initialization",
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
FactTypes: []analysis.Fact{
new(isRequiredField),
new(hasRequiredFields),
},
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
f := finder{
Fset: pass.Fset,
Info: pass.TypesInfo,
ExportObjectFact: pass.ExportObjectFact,
Reportf: pass.Reportf,
}
f.Find(inspect)
e := enforcer{
Info: pass.TypesInfo,
ImportObjectFact: pass.ImportObjectFact,
Reportf: pass.Reportf,
}
e.Enforce(inspect)
return nil, nil
}