Debian Codegraph @

gopls/internal/settings/staticcheck.go

raw symbols SCIP
1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package settings
6
7 import (
8 "fmt"
9 "log"
10
11 "golang.org/x/tools/go/analysis"
12 "golang.org/x/tools/gopls/internal/protocol"
13 "honnef.co/go/tools/analysis/lint"
14 "honnef.co/go/tools/quickfix"
15 "honnef.co/go/tools/quickfix/qf1001"
16 "honnef.co/go/tools/quickfix/qf1002"
17 "honnef.co/go/tools/quickfix/qf1003"
18 "honnef.co/go/tools/quickfix/qf1004"
19 "honnef.co/go/tools/quickfix/qf1005"
20 "honnef.co/go/tools/quickfix/qf1006"
21 "honnef.co/go/tools/quickfix/qf1007"
22 "honnef.co/go/tools/quickfix/qf1008"
23 "honnef.co/go/tools/quickfix/qf1009"
24 "honnef.co/go/tools/quickfix/qf1010"
25 "honnef.co/go/tools/quickfix/qf1011"
26 "honnef.co/go/tools/quickfix/qf1012"
27 "honnef.co/go/tools/simple"
28 "honnef.co/go/tools/simple/s1000"
29 "honnef.co/go/tools/simple/s1001"
30 "honnef.co/go/tools/simple/s1002"
31 "honnef.co/go/tools/simple/s1003"
32 "honnef.co/go/tools/simple/s1004"
33 "honnef.co/go/tools/simple/s1005"
34 "honnef.co/go/tools/simple/s1006"
35 "honnef.co/go/tools/simple/s1007"
36 "honnef.co/go/tools/simple/s1008"
37 "honnef.co/go/tools/simple/s1009"
38 "honnef.co/go/tools/simple/s1010"
39 "honnef.co/go/tools/simple/s1011"
40 "honnef.co/go/tools/simple/s1012"
41 "honnef.co/go/tools/simple/s1016"
42 "honnef.co/go/tools/simple/s1017"
43 "honnef.co/go/tools/simple/s1018"
44 "honnef.co/go/tools/simple/s1019"
45 "honnef.co/go/tools/simple/s1020"
46 "honnef.co/go/tools/simple/s1021"
47 "honnef.co/go/tools/simple/s1023"
48 "honnef.co/go/tools/simple/s1024"
49 "honnef.co/go/tools/simple/s1025"
50 "honnef.co/go/tools/simple/s1028"
51 "honnef.co/go/tools/simple/s1029"
52 "honnef.co/go/tools/simple/s1030"
53 "honnef.co/go/tools/simple/s1031"
54 "honnef.co/go/tools/simple/s1032"
55 "honnef.co/go/tools/simple/s1033"
56 "honnef.co/go/tools/simple/s1034"
57 "honnef.co/go/tools/simple/s1035"
58 "honnef.co/go/tools/simple/s1036"
59 "honnef.co/go/tools/simple/s1037"
60 "honnef.co/go/tools/simple/s1038"
61 "honnef.co/go/tools/simple/s1039"
62 "honnef.co/go/tools/simple/s1040"
63 "honnef.co/go/tools/staticcheck"
64 "honnef.co/go/tools/staticcheck/sa1000"
65 "honnef.co/go/tools/staticcheck/sa1001"
66 "honnef.co/go/tools/staticcheck/sa1002"
67 "honnef.co/go/tools/staticcheck/sa1003"
68 "honnef.co/go/tools/staticcheck/sa1004"
69 "honnef.co/go/tools/staticcheck/sa1005"
70 "honnef.co/go/tools/staticcheck/sa1006"
71 "honnef.co/go/tools/staticcheck/sa1007"
72 "honnef.co/go/tools/staticcheck/sa1008"
73 "honnef.co/go/tools/staticcheck/sa1010"
74 "honnef.co/go/tools/staticcheck/sa1011"
75 "honnef.co/go/tools/staticcheck/sa1012"
76 "honnef.co/go/tools/staticcheck/sa1013"
77 "honnef.co/go/tools/staticcheck/sa1014"
78 "honnef.co/go/tools/staticcheck/sa1015"
79 "honnef.co/go/tools/staticcheck/sa1016"
80 "honnef.co/go/tools/staticcheck/sa1017"
81 "honnef.co/go/tools/staticcheck/sa1018"
82 "honnef.co/go/tools/staticcheck/sa1019"
83 "honnef.co/go/tools/staticcheck/sa1020"
84 "honnef.co/go/tools/staticcheck/sa1021"
85 "honnef.co/go/tools/staticcheck/sa1023"
86 "honnef.co/go/tools/staticcheck/sa1024"
87 "honnef.co/go/tools/staticcheck/sa1025"
88 "honnef.co/go/tools/staticcheck/sa1026"
89 "honnef.co/go/tools/staticcheck/sa1027"
90 "honnef.co/go/tools/staticcheck/sa1028"
91 "honnef.co/go/tools/staticcheck/sa1029"
92 "honnef.co/go/tools/staticcheck/sa1030"
93 "honnef.co/go/tools/staticcheck/sa1031"
94 "honnef.co/go/tools/staticcheck/sa1032"
95 "honnef.co/go/tools/staticcheck/sa2000"
96 "honnef.co/go/tools/staticcheck/sa2001"
97 "honnef.co/go/tools/staticcheck/sa2002"
98 "honnef.co/go/tools/staticcheck/sa2003"
99 "honnef.co/go/tools/staticcheck/sa3000"
100 "honnef.co/go/tools/staticcheck/sa3001"
101 "honnef.co/go/tools/staticcheck/sa4000"
102 "honnef.co/go/tools/staticcheck/sa4001"
103 "honnef.co/go/tools/staticcheck/sa4003"
104 "honnef.co/go/tools/staticcheck/sa4004"
105 "honnef.co/go/tools/staticcheck/sa4005"
106 "honnef.co/go/tools/staticcheck/sa4006"
107 "honnef.co/go/tools/staticcheck/sa4008"
108 "honnef.co/go/tools/staticcheck/sa4009"
109 "honnef.co/go/tools/staticcheck/sa4010"
110 "honnef.co/go/tools/staticcheck/sa4011"
111 "honnef.co/go/tools/staticcheck/sa4012"
112 "honnef.co/go/tools/staticcheck/sa4013"
113 "honnef.co/go/tools/staticcheck/sa4014"
114 "honnef.co/go/tools/staticcheck/sa4015"
115 "honnef.co/go/tools/staticcheck/sa4016"
116 "honnef.co/go/tools/staticcheck/sa4017"
117 "honnef.co/go/tools/staticcheck/sa4018"
118 "honnef.co/go/tools/staticcheck/sa4019"
119 "honnef.co/go/tools/staticcheck/sa4020"
120 "honnef.co/go/tools/staticcheck/sa4021"
121 "honnef.co/go/tools/staticcheck/sa4022"
122 "honnef.co/go/tools/staticcheck/sa4023"
123 "honnef.co/go/tools/staticcheck/sa4024"
124 "honnef.co/go/tools/staticcheck/sa4025"
125 "honnef.co/go/tools/staticcheck/sa4026"
126 "honnef.co/go/tools/staticcheck/sa4027"
127 "honnef.co/go/tools/staticcheck/sa4028"
128 "honnef.co/go/tools/staticcheck/sa4029"
129 "honnef.co/go/tools/staticcheck/sa4030"
130 "honnef.co/go/tools/staticcheck/sa4031"
131 "honnef.co/go/tools/staticcheck/sa4032"
132 "honnef.co/go/tools/staticcheck/sa5000"
133 "honnef.co/go/tools/staticcheck/sa5001"
134 "honnef.co/go/tools/staticcheck/sa5002"
135 "honnef.co/go/tools/staticcheck/sa5003"
136 "honnef.co/go/tools/staticcheck/sa5004"
137 "honnef.co/go/tools/staticcheck/sa5005"
138 "honnef.co/go/tools/staticcheck/sa5007"
139 "honnef.co/go/tools/staticcheck/sa5008"
140 "honnef.co/go/tools/staticcheck/sa5009"
141 "honnef.co/go/tools/staticcheck/sa5010"
142 "honnef.co/go/tools/staticcheck/sa5011"
143 "honnef.co/go/tools/staticcheck/sa5012"
144 "honnef.co/go/tools/staticcheck/sa6000"
145 "honnef.co/go/tools/staticcheck/sa6001"
146 "honnef.co/go/tools/staticcheck/sa6002"
147 "honnef.co/go/tools/staticcheck/sa6003"
148 "honnef.co/go/tools/staticcheck/sa6005"
149 "honnef.co/go/tools/staticcheck/sa6006"
150 "honnef.co/go/tools/staticcheck/sa9001"
151 "honnef.co/go/tools/staticcheck/sa9002"
152 "honnef.co/go/tools/staticcheck/sa9003"
153 "honnef.co/go/tools/staticcheck/sa9004"
154 "honnef.co/go/tools/staticcheck/sa9005"
155 "honnef.co/go/tools/staticcheck/sa9006"
156 "honnef.co/go/tools/staticcheck/sa9007"
157 "honnef.co/go/tools/staticcheck/sa9008"
158 "honnef.co/go/tools/staticcheck/sa9009"
159 "honnef.co/go/tools/stylecheck"
160 "honnef.co/go/tools/stylecheck/st1000"
161 "honnef.co/go/tools/stylecheck/st1001"
162 "honnef.co/go/tools/stylecheck/st1003"
163 "honnef.co/go/tools/stylecheck/st1005"
164 "honnef.co/go/tools/stylecheck/st1006"
165 "honnef.co/go/tools/stylecheck/st1008"
166 "honnef.co/go/tools/stylecheck/st1011"
167 "honnef.co/go/tools/stylecheck/st1012"
168 "honnef.co/go/tools/stylecheck/st1013"
169 "honnef.co/go/tools/stylecheck/st1015"
170 "honnef.co/go/tools/stylecheck/st1016"
171 "honnef.co/go/tools/stylecheck/st1017"
172 "honnef.co/go/tools/stylecheck/st1018"
173 "honnef.co/go/tools/stylecheck/st1019"
174 "honnef.co/go/tools/stylecheck/st1020"
175 "honnef.co/go/tools/stylecheck/st1021"
176 "honnef.co/go/tools/stylecheck/st1022"
177 "honnef.co/go/tools/stylecheck/st1023"
178 )
179
180 func staticcheckAnalyzers() (res []*Analyzer) {
181
182 mapSeverity := func(severity lint.Severity) protocol.DiagnosticSeverity {
183 switch severity {
184 case lint.SeverityError:
185 return protocol.SeverityError
186 case lint.SeverityDeprecated:
187 // TODO(dh): in LSP, deprecated is a tag, not a severity.
188 // We'll want to support this once we enable SA5011.
189 return protocol.SeverityWarning
190 case lint.SeverityWarning:
191 return protocol.SeverityWarning
192 case lint.SeverityInfo:
193 return protocol.SeverityInformation
194 case lint.SeverityHint:
195 return protocol.SeverityHint
196 default:
197 return protocol.SeverityWarning
198 }
199 }
200
201 // We can't import buildir.Analyzer directly, so grab it from another analyzer.
202 buildir := sa1000.SCAnalyzer.Analyzer.Requires[0]
203 if buildir.Name != "buildir" {
204 panic("sa1000.Requires[0] is not buildir")
205 }
206
207 add := func(a *lint.Analyzer, dflt bool) {
208 // Assert that no analyzer that requires "buildir",
209 // even indirectly, is enabled by default.
210 if dflt {
211 var visit func(aa *analysis.Analyzer)
212 visit = func(aa *analysis.Analyzer) {
213 if aa == buildir {
214 log.Fatalf("%s requires buildir (perhaps indirectly) yet is enabled by default", a.Analyzer.Name)
215 }
216 for _, req := range aa.Requires {
217 visit(req)
218 }
219 }
220 visit(a.Analyzer)
221 }
222 res = append(res, &Analyzer{
223 analyzer: a.Analyzer,
224 staticcheck: a.Doc,
225 nonDefault: !dflt,
226 severity: mapSeverity(a.Doc.Severity),
227 })
228 }
229
230 type M = map[*lint.Analyzer]any // value = true|false|nil
231
232 addAll := func(suite string, upstream []*lint.Analyzer, config M) {
233 for _, a := range upstream {
234 v, ok := config[a]
235 if !ok {
236 panic(fmt.Sprintf("%s.Analyzers includes %s but config mapping does not; settings audit required", suite, a.Analyzer.Name))
237 }
238 if v != nil {
239 add(a, v.(bool))
240 }
241 }
242 }
243
244 // For each analyzer in the four suites provided by
245 // staticcheck, we provide a complete configuration, mapping
246 // it to a boolean, indicating whether it should be on by
247 // default in gopls, or nil to indicate explicitly that it has
248 // been excluded (e.g. because it is redundant with an
249 // existing vet analyzer such as printf, waitgroup, appends).
250 //
251 // This approach ensures that as suites grow, we make an
252 // affirmative decision, positive or negative, about adding
253 // new items.
254 //
255 // An analyzer may be off by default if:
256 // - it requires, even indirectly, "buildir", which is like
257 // buildssa but uses facts, making it expensive;
258 // - it has significant false positives;
259 // - it reports on non-problematic style issues;
260 // - its fixes are lossy (e.g. of comments) or not always sound;
261 // - it reports "maybes", not "definites" (e.g. sa9001).
262 // - it reports on harmless stylistic choices that may have
263 // been chosen deliberately for clarity or emphasis (e.g. s1005).
264 // - it makes deductions from build tags that are not true
265 // for all configurations.
266
267 addAll("simple", simple.Analyzers, M{
268 s1000.SCAnalyzer: true,
269 s1001.SCAnalyzer: true,
270 s1002.SCAnalyzer: false, // makes unsound deductions from build tags
271 s1003.SCAnalyzer: true,
272 s1004.SCAnalyzer: true,
273 s1005.SCAnalyzer: false, // not a correctness/style issue
274 s1006.SCAnalyzer: false, // makes unsound deductions from build tags
275 s1007.SCAnalyzer: true,
276 s1008.SCAnalyzer: false, // may lose important comments
277 s1009.SCAnalyzer: true,
278 s1010.SCAnalyzer: true,
279 s1011.SCAnalyzer: false, // requires buildir
280 s1012.SCAnalyzer: true,
281 s1016.SCAnalyzer: false, // may rely on coincidental structural subtyping
282 s1017.SCAnalyzer: true,
283 s1018.SCAnalyzer: true,
284 s1019.SCAnalyzer: true,
285 s1020.SCAnalyzer: true,
286 s1021.SCAnalyzer: false, // may lose important comments
287 s1023.SCAnalyzer: true,
288 s1024.SCAnalyzer: true,
289 s1025.SCAnalyzer: false, // requires buildir
290 s1028.SCAnalyzer: true,
291 s1029.SCAnalyzer: false, // requires buildir
292 s1030.SCAnalyzer: true, // (tentative: see docs,
293 s1031.SCAnalyzer: true,
294 s1032.SCAnalyzer: true,
295 s1033.SCAnalyzer: true,
296 s1034.SCAnalyzer: true,
297 s1035.SCAnalyzer: true,
298 s1036.SCAnalyzer: true,
299 s1037.SCAnalyzer: true,
300 s1038.SCAnalyzer: true,
301 s1039.SCAnalyzer: true,
302 s1040.SCAnalyzer: true,
303 })
304
305 addAll("stylecheck", stylecheck.Analyzers, M{
306 // These are all slightly too opinionated to be on by default.
307 st1000.SCAnalyzer: false,
308 st1001.SCAnalyzer: false,
309 st1003.SCAnalyzer: false,
310 st1005.SCAnalyzer: false,
311 st1006.SCAnalyzer: false,
312 st1008.SCAnalyzer: false,
313 st1011.SCAnalyzer: false,
314 st1012.SCAnalyzer: false,
315 st1013.SCAnalyzer: false,
316 st1015.SCAnalyzer: false,
317 st1016.SCAnalyzer: false,
318 st1017.SCAnalyzer: false,
319 st1018.SCAnalyzer: false,
320 st1019.SCAnalyzer: false,
321 st1020.SCAnalyzer: false,
322 st1021.SCAnalyzer: false,
323 st1022.SCAnalyzer: false,
324 st1023.SCAnalyzer: false,
325 })
326
327 // These are not bug fixes but code transformations: some
328 // reversible and value-neutral, of the kind typically listed
329 // on the VS Code's Refactor/Source Action/Quick Fix menus.
330 //
331 // TODO(adonovan): plumb these to the appropriate menu,
332 // as we do for code actions such as split/join lines.
333 addAll("quickfix", quickfix.Analyzers, M{
334 qf1001.SCAnalyzer: false, // not always a style improvement
335 qf1002.SCAnalyzer: true,
336 qf1003.SCAnalyzer: true,
337 qf1004.SCAnalyzer: true,
338 qf1005.SCAnalyzer: false, // not always a style improvement
339 qf1006.SCAnalyzer: false, // may lose important comments
340 qf1007.SCAnalyzer: false, // may lose important comments
341 qf1008.SCAnalyzer: false, // not always a style improvement
342 qf1009.SCAnalyzer: true,
343 qf1010.SCAnalyzer: true,
344 qf1011.SCAnalyzer: false, // not always a style improvement
345 qf1012.SCAnalyzer: true,
346 })
347
348 addAll("staticcheck", staticcheck.Analyzers, M{
349 sa1000.SCAnalyzer: false, // requires buildir
350 sa1001.SCAnalyzer: true,
351 sa1002.SCAnalyzer: false, // requires buildir
352 sa1003.SCAnalyzer: false, // requires buildir
353 sa1004.SCAnalyzer: true,
354 sa1005.SCAnalyzer: true,
355 sa1006.SCAnalyzer: nil, // redundant wrt 'printf'
356 sa1007.SCAnalyzer: false, // requires buildir
357 sa1008.SCAnalyzer: true,
358 sa1010.SCAnalyzer: false, // requires buildir
359 sa1011.SCAnalyzer: false, // requires buildir
360 sa1012.SCAnalyzer: true,
361 sa1013.SCAnalyzer: true,
362 sa1014.SCAnalyzer: false, // requires buildir
363 sa1015.SCAnalyzer: false, // requires buildir
364 sa1016.SCAnalyzer: true,
365 sa1017.SCAnalyzer: false, // requires buildir
366 sa1018.SCAnalyzer: false, // requires buildir
367 sa1019.SCAnalyzer: nil, // redundant wrt 'deprecated'
368 sa1020.SCAnalyzer: false, // requires buildir
369 sa1021.SCAnalyzer: false, // requires buildir
370 sa1023.SCAnalyzer: false, // requires buildir
371 sa1024.SCAnalyzer: false, // requires buildir
372 sa1025.SCAnalyzer: false, // requires buildir
373 sa1026.SCAnalyzer: false, // requires buildir
374 sa1027.SCAnalyzer: false, // requires buildir
375 sa1028.SCAnalyzer: false, // requires buildir
376 sa1029.SCAnalyzer: false, // requires buildir
377 sa1030.SCAnalyzer: false, // requires buildir
378 sa1031.SCAnalyzer: false, // requires buildir
379 sa1032.SCAnalyzer: false, // requires buildir
380 sa2000.SCAnalyzer: nil, // redundant wrt 'waitgroup'
381 sa2001.SCAnalyzer: true,
382 sa2002.SCAnalyzer: false, // requires buildir
383 sa2003.SCAnalyzer: false, // requires buildir
384 sa3000.SCAnalyzer: true,
385 sa3001.SCAnalyzer: true,
386 sa4000.SCAnalyzer: true,
387 sa4001.SCAnalyzer: true,
388 sa4003.SCAnalyzer: true,
389 sa4004.SCAnalyzer: true,
390 sa4005.SCAnalyzer: false, // requires buildir
391 sa4006.SCAnalyzer: false, // requires buildir
392 sa4008.SCAnalyzer: false, // requires buildir
393 sa4009.SCAnalyzer: false, // requires buildir
394 sa4010.SCAnalyzer: false, // requires buildir
395 sa4011.SCAnalyzer: true,
396 sa4012.SCAnalyzer: false, // requires buildir
397 sa4013.SCAnalyzer: true,
398 sa4014.SCAnalyzer: true,
399 sa4015.SCAnalyzer: false, // requires buildir
400 sa4016.SCAnalyzer: true,
401 sa4017.SCAnalyzer: false, // requires buildir
402 sa4018.SCAnalyzer: false, // requires buildir
403 sa4019.SCAnalyzer: true,
404 sa4020.SCAnalyzer: true,
405 sa4021.SCAnalyzer: nil, // redundant wrt 'appends'
406 sa4022.SCAnalyzer: true,
407 sa4023.SCAnalyzer: false, // requires buildir
408 sa4024.SCAnalyzer: true,
409 sa4025.SCAnalyzer: true,
410 sa4026.SCAnalyzer: true,
411 sa4027.SCAnalyzer: true,
412 sa4028.SCAnalyzer: true,
413 sa4029.SCAnalyzer: true,
414 sa4030.SCAnalyzer: true,
415 sa4031.SCAnalyzer: false, // requires buildir
416 sa4032.SCAnalyzer: true,
417 sa5000.SCAnalyzer: false, // requires buildir
418 sa5001.SCAnalyzer: true,
419 sa5002.SCAnalyzer: false, // makes unsound deductions from build tags
420 sa5003.SCAnalyzer: true,
421 sa5004.SCAnalyzer: true,
422 sa5005.SCAnalyzer: false, // requires buildir
423 sa5007.SCAnalyzer: false, // requires buildir
424 sa5008.SCAnalyzer: true,
425 sa5009.SCAnalyzer: nil, // requires buildir; redundant wrt 'printf' (#34494)
426 sa5010.SCAnalyzer: false, // requires buildir
427 sa5011.SCAnalyzer: false, // requires buildir
428 sa5012.SCAnalyzer: false, // requires buildir
429 sa6000.SCAnalyzer: false, // requires buildir
430 sa6001.SCAnalyzer: false, // requires buildir
431 sa6002.SCAnalyzer: false, // requires buildir
432 sa6003.SCAnalyzer: false, // requires buildir
433 sa6005.SCAnalyzer: true,
434 sa6006.SCAnalyzer: true,
435 sa9001.SCAnalyzer: false, // reports a "maybe" bug (low signal/noise)
436 sa9002.SCAnalyzer: true,
437 sa9003.SCAnalyzer: false, // requires buildir; NonDefault
438 sa9004.SCAnalyzer: true,
439 sa9005.SCAnalyzer: false, // requires buildir
440 sa9006.SCAnalyzer: true,
441 sa9007.SCAnalyzer: false, // requires buildir
442 sa9008.SCAnalyzer: false, // requires buildir
443 sa9009.SCAnalyzer: true,
444 })
445
446 return res
447 }