-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathstaticcheck_common.go
186 lines (153 loc) · 4.82 KB
/
staticcheck_common.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package golinters
import (
"strings"
"unicode"
"golang.org/x/tools/go/analysis"
"honnef.co/go/tools/analysis/lint"
scconfig "honnef.co/go/tools/config"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/logutils"
)
var debugf = logutils.Debug(logutils.DebugKeyMegacheck)
func getGoVersion(settings *config.StaticCheckSettings) string {
var goVersion string
if settings != nil {
goVersion = settings.GoVersion
}
if goVersion != "" {
return goVersion
}
return "1.17"
}
func setupStaticCheckAnalyzers(src []*lint.Analyzer, goVersion string, checks []string) []*analysis.Analyzer {
var names []string
for _, a := range src {
names = append(names, a.Analyzer.Name)
}
filter := filterAnalyzerNames(names, checks)
var ret []*analysis.Analyzer
for _, a := range src {
if filter[a.Analyzer.Name] {
setAnalyzerGoVersion(a.Analyzer, goVersion)
ret = append(ret, a.Analyzer)
}
}
return ret
}
func setAnalyzerGoVersion(a *analysis.Analyzer, goVersion string) {
if v := a.Flags.Lookup("go"); v != nil {
if err := v.Value.Set(goVersion); err != nil {
debugf("Failed to set go version: %s", err)
}
}
}
func commonStaticCheckConfig(settings *config.CommonStaticCheckSettings) *scconfig.Config {
var cfg *scconfig.Config
if settings == nil || len(settings.Checks) == 0 {
return &scconfig.Config{
Checks: []string{"*"}, // override for compatibility reason. Must drop in the next major version.
}
}
cfg = &scconfig.Config{
Checks: settings.Checks,
}
cfg.Checks = normalizeList(cfg.Checks)
return cfg
}
func staticCheckConfig(settings *config.StaticCheckSettings) *scconfig.Config {
var cfg *scconfig.Config
if settings == nil || !settings.HasConfiguration() {
return &scconfig.Config{
Checks: []string{"*"}, // override for compatibility reason. Must drop in the next major version.
Initialisms: scconfig.DefaultConfig.Initialisms,
DotImportWhitelist: scconfig.DefaultConfig.DotImportWhitelist,
HTTPStatusCodeWhitelist: scconfig.DefaultConfig.HTTPStatusCodeWhitelist,
}
}
cfg = &scconfig.Config{
Checks: settings.Checks,
Initialisms: settings.Initialisms,
DotImportWhitelist: settings.DotImportWhitelist,
HTTPStatusCodeWhitelist: settings.HTTPStatusCodeWhitelist,
}
if len(cfg.Checks) == 0 {
cfg.Checks = append(cfg.Checks, "*") // override for compatibility reason. Must drop in the next major version.
}
if len(cfg.Initialisms) == 0 {
cfg.Initialisms = append(cfg.Initialisms, scconfig.DefaultConfig.Initialisms...)
}
if len(cfg.DotImportWhitelist) == 0 {
cfg.DotImportWhitelist = append(cfg.DotImportWhitelist, scconfig.DefaultConfig.DotImportWhitelist...)
}
if len(cfg.HTTPStatusCodeWhitelist) == 0 {
cfg.HTTPStatusCodeWhitelist = append(cfg.HTTPStatusCodeWhitelist, scconfig.DefaultConfig.HTTPStatusCodeWhitelist...)
}
cfg.Checks = normalizeList(cfg.Checks)
cfg.Initialisms = normalizeList(cfg.Initialisms)
cfg.DotImportWhitelist = normalizeList(cfg.DotImportWhitelist)
cfg.HTTPStatusCodeWhitelist = normalizeList(cfg.HTTPStatusCodeWhitelist)
return cfg
}
// https://github.com/dominikh/go-tools/blob/9bf17c0388a65710524ba04c2d821469e639fdc2/lintcmd/lint.go#L437-L477
//
//nolint:gocritic // Keep the original source code.
func filterAnalyzerNames(analyzers []string, checks []string) map[string]bool {
allowedChecks := map[string]bool{}
for _, check := range checks {
b := true
if len(check) > 1 && check[0] == '-' {
b = false
check = check[1:]
}
if check == "*" || check == "all" {
// Match all
for _, c := range analyzers {
allowedChecks[c] = b
}
} else if strings.HasSuffix(check, "*") {
// Glob
prefix := check[:len(check)-1]
isCat := strings.IndexFunc(prefix, func(r rune) bool { return unicode.IsNumber(r) }) == -1
for _, a := range analyzers {
idx := strings.IndexFunc(a, func(r rune) bool { return unicode.IsNumber(r) })
if isCat {
// Glob is S*, which should match S1000 but not SA1000
cat := a[:idx]
if prefix == cat {
allowedChecks[a] = b
}
} else {
// Glob is S1*
if strings.HasPrefix(a, prefix) {
allowedChecks[a] = b
}
}
}
} else {
// Literal check name
allowedChecks[check] = b
}
}
return allowedChecks
}
// https://github.com/dominikh/go-tools/blob/9bf17c0388a65710524ba04c2d821469e639fdc2/config/config.go#L95-L116
func normalizeList(list []string) []string {
if len(list) > 1 {
nlist := make([]string, 0, len(list))
nlist = append(nlist, list[0])
for i, el := range list[1:] {
if el != list[i] {
nlist = append(nlist, el)
}
}
list = nlist
}
for _, el := range list {
if el == "inherit" {
// This should never happen, because the default config
// should not use "inherit"
panic(`unresolved "inherit"`)
}
}
return list
}