Debian Codegraph @

gopls/internal/settings/analysis.go

raw symbols SCIP
1 // Copyright 2024 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 "log"
9
10 "golang.org/x/tools/go/analysis"
11 "golang.org/x/tools/go/analysis/passes/atomicalign"
12 "golang.org/x/tools/go/analysis/passes/deepequalerrors"
13 "golang.org/x/tools/go/analysis/passes/fieldalignment"
14 "golang.org/x/tools/go/analysis/passes/inline"
15 "golang.org/x/tools/go/analysis/passes/modernize"
16 "golang.org/x/tools/go/analysis/passes/nilness"
17 "golang.org/x/tools/go/analysis/passes/scannererr"
18 "golang.org/x/tools/go/analysis/passes/shadow"
19 "golang.org/x/tools/go/analysis/passes/sortslice"
20 "golang.org/x/tools/go/analysis/passes/sqlrowserr"
21 "golang.org/x/tools/go/analysis/passes/unusedwrite"
22 "golang.org/x/tools/go/analysis/suite/fix"
23 "golang.org/x/tools/go/analysis/suite/vet"
24 "golang.org/x/tools/gopls/internal/analysis/deprecated"
25 "golang.org/x/tools/gopls/internal/analysis/embeddirective"
26 "golang.org/x/tools/gopls/internal/analysis/errorsastypeshadow"
27 "golang.org/x/tools/gopls/internal/analysis/fillreturns"
28 "golang.org/x/tools/gopls/internal/analysis/infertypeargs"
29 "golang.org/x/tools/gopls/internal/analysis/maprange"
30 "golang.org/x/tools/gopls/internal/analysis/nonewvars"
31 "golang.org/x/tools/gopls/internal/analysis/noresultvalues"
32 "golang.org/x/tools/gopls/internal/analysis/recursiveiter"
33 "golang.org/x/tools/gopls/internal/analysis/simplifycompositelit"
34 "golang.org/x/tools/gopls/internal/analysis/simplifyrange"
35 "golang.org/x/tools/gopls/internal/analysis/simplifyslice"
36 "golang.org/x/tools/gopls/internal/analysis/unusedfunc"
37 "golang.org/x/tools/gopls/internal/analysis/unusedparams"
38 "golang.org/x/tools/gopls/internal/analysis/unusedvariable"
39 "golang.org/x/tools/gopls/internal/analysis/writestring"
40 "golang.org/x/tools/gopls/internal/analysis/yield"
41 "golang.org/x/tools/gopls/internal/protocol"
42 "honnef.co/go/tools/analysis/lint"
43 )
44
45 // AllAnalyzers holds the list of Analyzers available to all gopls
46 // sessions, independent of build version. It is the source from which
47 // gopls/doc/analyzers.md is generated.
48 var AllAnalyzers = initAnalyzers()
49
50 // Analyzer augments an [analysis.Analyzer] with additional LSP configuration.
51 //
52 // Analyzers are immutable, since they are shared across multiple LSP sessions.
53 type Analyzer struct {
54 analyzer *analysis.Analyzer
55 staticcheck *lint.RawDocumentation // only for staticcheck analyzers
56 nonDefault bool // (sense is negated so we can mostly omit it)
57 actionKinds []protocol.CodeActionKind
58 severity protocol.DiagnosticSeverity
59 tags []protocol.DiagnosticTag
60 }
61
62 // Analyzer returns the [analysis.Analyzer] that this Analyzer wraps.
63 func (a *Analyzer) Analyzer() *analysis.Analyzer { return a.analyzer }
64
65 // Enabled reports whether the analyzer is enabled by the options.
66 // This value can be configured per-analysis in user settings.
67 func (a *Analyzer) Enabled(o *Options) bool {
68 // An explicit setting by name takes precedence.
69 if v, found := o.Analyses[a.Analyzer().Name]; found {
70 return v
71 }
72 if a.staticcheck != nil {
73 // An explicit staticcheck={true,false} setting
74 // enables/disables all staticcheck analyzers.
75 if o.StaticcheckProvided {
76 return o.Staticcheck
77 }
78 // Respect staticcheck's off-by-default options too.
79 // (This applies to only a handful of analyzers.)
80 if a.staticcheck.NonDefault {
81 return false
82 }
83 }
84 // Respect gopls' default setting.
85 return !a.nonDefault
86 }
87
88 // ActionKinds is the set of kinds of code action this analyzer produces.
89 //
90 // If left unset, it defaults to QuickFix.
91 // TODO(rfindley): revisit.
92 func (a *Analyzer) ActionKinds() []protocol.CodeActionKind { return a.actionKinds }
93
94 // Severity is the severity set for diagnostics reported by this analyzer.
95 // The default severity is SeverityWarning.
96 //
97 // While the LSP spec does not specify how severity should be used, here are
98 // some guiding heuristics:
99 // - Error: for parse and type errors, which would stop the build.
100 // - Warning: for analyzer diagnostics reporting likely bugs.
101 // - Info: for analyzer diagnostics that do not indicate bugs, but may
102 // suggest inaccurate or superfluous code.
103 // - Hint: for analyzer diagnostics that do not indicate mistakes, but offer
104 // simplifications or modernizations. By their nature, hints should
105 // generally carry quick fixes.
106 //
107 // The difference between Info and Hint is particularly subtle. Importantly,
108 // Hint diagnostics do not appear in the Problems tab in VS Code, so they are
109 // less intrusive than Info diagnostics. The rule of thumb is this: use Info if
110 // the diagnostic is not a bug, but the author probably didn't mean to write
111 // the code that way. Use Hint if the diagnostic is not a bug and the author
112 // intended to write the code that way, but there is a simpler or more modern
113 // way to express the same logic. An 'unused' diagnostic is Info level, since
114 // the author probably didn't mean to check in unreachable code. A 'modernize'
115 // or 'deprecated' diagnostic is Hint level, since the author intended to write
116 // the code that way, but now there is a better way.
117 func (a *Analyzer) Severity() protocol.DiagnosticSeverity {
118 if a.severity == 0 {
119 return protocol.SeverityWarning
120 }
121 return a.severity
122 }
123
124 // Tags is extra tags (unnecessary, deprecated, etc) for diagnostics
125 // reported by this analyzer.
126 func (a *Analyzer) Tags() []protocol.DiagnosticTag { return a.tags }
127
128 // String returns the name of this analyzer.
129 func (a *Analyzer) String() string { return a.analyzer.String() }
130
131 func initAnalyzers() (res []*Analyzer) {
132 seen := make(map[*analysis.Analyzer]bool)
133
134 // Start with the traditional vet and fix suites.
135 for _, suite := range []struct {
136 name string
137 analyzers []*analysis.Analyzer
138 severity protocol.DiagnosticSeverity
139 }{
140 {"fix.Suite", fix.Suite, protocol.SeverityHint},
141 {"vet.Suite", vet.Suite, protocol.SeverityWarning},
142 } {
143 for _, a := range suite.analyzers {
144 // De-duplicate, since the suites overlap.
145 if !seen[a] {
146 seen[a] = true
147 res = append(res, &Analyzer{analyzer: a, severity: suite.severity})
148 }
149 }
150 }
151
152 // set inline -lazy_edit mode
153 if err := inline.Analyzer.Flags.Set("lazy_edits", "true"); err != nil {
154 log.Fatalf("setting inline -lazy_edits flag: %v", err)
155 }
156
157 // See [Analyzer.Severity] for guidance on setting analyzer severity below.
158 for _, a := range []*Analyzer{
159 // not suitable for vet.Suite:
160 // - some (nilness, yield) use go/ssa; see #59714.
161 // - others don't meet the "frequency" criterion;
162 // see GOROOT/src/cmd/vet/README.
163 {analyzer: atomicalign.Analyzer},
164 {analyzer: deprecated.Analyzer,
165 severity: protocol.SeverityHint,
166 tags: []protocol.DiagnosticTag{protocol.Deprecated},
167 },
168 {analyzer: deepequalerrors.Analyzer},
169 {analyzer: nilness.Analyzer}, // uses go/ssa
170 {analyzer: yield.Analyzer}, // uses go/ssa
171 {analyzer: sortslice.Analyzer},
172 {analyzer: embeddirective.Analyzer},
173 {analyzer: scannererr.Analyzer}, // to appear in cmd/[email protected]
174 {analyzer: sqlrowserr.Analyzer}, // to appear in cmd/[email protected]
175 {analyzer: recursiveiter.Analyzer}, // under evaluation
176 {analyzer: errorsastypeshadow.Analyzer}, // under evaluation
177 {analyzer: writestring.Analyzer}, // under evaluation
178
179 // disabled due to high false positives
180 {analyzer: shadow.Analyzer, severity: protocol.SeverityHint, nonDefault: true}, // very noisy
181 {analyzer: fieldalignment.Analyzer, severity: protocol.SeverityHint, nonDefault: true}, // #67762, #76237
182
183 // simplifiers and modernizers (beyond fix.Suite)
184 //
185 // These analyzers offer mere style fixes on correct code,
186 // thus they will never appear in cmd/vet or cmd/fix and
187 // their severity level is "information".
188 //
189 // gofmt -s suite
190 {
191 analyzer: simplifycompositelit.Analyzer,
192 actionKinds: []protocol.CodeActionKind{protocol.SourceFixAll, protocol.QuickFix},
193 severity: protocol.SeverityInformation,
194 },
195 {
196 analyzer: simplifyrange.Analyzer,
197 actionKinds: []protocol.CodeActionKind{protocol.SourceFixAll, protocol.QuickFix},
198 severity: protocol.SeverityInformation,
199 },
200 {
201 analyzer: simplifyslice.Analyzer,
202 actionKinds: []protocol.CodeActionKind{protocol.SourceFixAll, protocol.QuickFix},
203 severity: protocol.SeverityInformation,
204 },
205 // other simplifiers
206 {analyzer: infertypeargs.Analyzer, severity: protocol.SeverityInformation},
207 {analyzer: maprange.Analyzer, severity: protocol.SeverityHint},
208 {analyzer: unusedparams.Analyzer, severity: protocol.SeverityInformation},
209 {analyzer: unusedfunc.Analyzer, severity: protocol.SeverityInformation},
210 {analyzer: unusedwrite.Analyzer, severity: protocol.SeverityInformation}, // uses go/ssa
211 // modernizers not included in modernize.Suite (nor fix.Suite)
212 {analyzer: modernize.AppendClippedAnalyzer, nonDefault: true}, // not nil-preserving
213 {analyzer: modernize.BLoopAnalyzer}, // may skew benchmark results, see golang/go#74967
214 {analyzer: modernize.FmtAppendfAnalyzer}, // makes code less clear, see golang/go#77581
215 {analyzer: modernize.SlicesDeleteAnalyzer, nonDefault: true}, // not nil-preserving
216
217 // type-error analyzers
218 // These analyzers enrich go/types errors with suggested fixes.
219 // Since they exist only to attach their fixes to type errors, their
220 // severity is irrelevant.
221 {analyzer: fillreturns.Analyzer},
222 {analyzer: nonewvars.Analyzer},
223 {analyzer: noresultvalues.Analyzer},
224 {analyzer: unusedvariable.Analyzer},
225 } {
226 if seen[a.analyzer] {
227 log.Fatalf("duplicate analyzer: %q", a.analyzer.Name)
228 }
229 seen[a.analyzer] = true
230 res = append(res, a)
231 }
232
233 return append(res, staticcheckAnalyzers()...)
234 }