3 <!--TODO: Generate this file from the documentation in golang/org/x/tools/go/analysis/passes and golang/org/x/tools/go/lsp/source/options.go.-->
5 This document describes the analyzers that `gopls` uses inside the editor.
7 A value of `true` means that the analyzer is enabled by default and a value of `false` means it is disabled by default.
9 Details about how to enable/disable these analyses can be found [here](settings.md#analyses).
13 Below is the list of general analyzers that are used in `go vet`.
17 report mismatches between assembly files and Go declarations
19 Default value: `true`.
23 check for useless assignments
25 This checker reports assignments of the form `x = x` or `a[i] = a[i]`.
26 These are almost always useless, and even when they aren't they are
29 Default value: `true`.
33 check for common mistakes using the sync/atomic package
35 The atomic checker looks for assignment statements of the form:
37 `x = atomic.AddUint64(&x, 1)`
41 Default value: `true`.
45 check for non-64-bits-aligned arguments to sync/atomic functions
47 Default value: `true`.
51 check for common mistakes involving boolean operators
53 Default value: `true`.
57 check that +build tags are well-formed and correctly located
59 Default value: `true`.
63 detect some violations of the cgo pointer passing rules
65 Check for invalid cgo pointer passing.
66 This looks for code that uses cgo to call C code passing values
67 whose types are almost always invalid according to the cgo pointer
69 Specifically, it warns about attempts to pass a Go chan, map, func,
70 or slice to C, either directly, or via a pointer, array, or struct.
72 Default value: `true`.
76 check for unkeyed composite literals
78 This analyzer reports a diagnostic for composite literals of struct
79 types imported from another package that do not use the field-keyed
80 syntax. Such literals are fragile because the addition of a new field
81 (even if unexported) to the struct will cause compilation to fail.
84 `err = &net.DNSConfigError{err}`
86 should be replaced by:
87 `err = &net.DNSConfigError{Err: err}`
89 Default value: `true`.
93 check for locks erroneously passed by value
95 Inadvertently copying a value containing a lock, such as sync.Mutex or
96 sync.WaitGroup, may cause both copies to malfunction. Generally such
97 values should be referred to through a pointer.
99 Default value: `true`.
103 report passing non-pointer or non-error values to errors.As
105 The errorsas analysis reports calls to errors.As where the type
106 of the second argument is not a pointer to a type implementing error.
108 Default value: `true`.
112 check for mistakes using HTTP responses
114 A common mistake when using the net/http package is to defer a function
115 call to close the http.Response Body before checking the error that
116 determines whether the response is valid:
119 resp, err := http.Head(url)
120 defer resp.Body.Close()
124 // (defer statement belongs here)
127 This checker helps uncover latent nil dereference bugs by reporting a
128 diagnostic for such mistakes.
130 Default value: `true`.
134 check references to loop variables from within nested functions
136 This analyzer checks for references to loop variables from within a
137 function literal inside the loop body. It checks only instances where
138 the function literal is called in a defer or go statement that is the
139 last statement in the loop body, as otherwise we would need whole
144 for i, v := range s {
146 println(i, v) // not what you might expect
151 See: https://golang.org/doc/go_faq.html#closures_and_goroutines
153 Default value: `true`.
157 check cancel func returned by context.WithCancel is called
159 The cancellation function returned by context.WithCancel, WithTimeout,
160 and WithDeadline must be called or the new context will remain live
161 until its parent context is cancelled.
162 (The background context is never cancelled.)
164 Default value: `true`.
168 check for useless comparisons between functions and nil
170 A useless comparison is one like f == nil as opposed to f() == nil.
172 Default value: `true`.
176 check consistency of Printf format strings and arguments
178 The check applies to known functions (for example, those in package fmt)
179 as well as any detected wrappers of known functions.
181 A function that wants to avail itself of printf checking but is not
182 found by this analyzer's heuristics (for example, due to use of
183 dynamic calls) can insert a bogus call:
187 _ = fmt.Sprintf(format, args...) // enable printf checking
191 The -funcs flag specifies a comma-separated list of names of additional
192 known formatting functions or methods. If the name contains a period,
193 it must denote a specific function using one of the following forms:
198 (*dir/pkg.Type).Method
201 Otherwise the name is interpreted as a case-insensitive unqualified
202 identifier such as "errorf". Either way, if a listed name ends in f, the
203 function is assumed to be Printf-like, taking a format string before the
204 argument list. Otherwise it is assumed to be Print-like, taking a list
205 of arguments with no format string.
207 Default value: `true`.
211 check for shifts that equal or exceed the width of the integer
213 Default value: `true`.
217 check signature of methods of well-known interfaces
219 Sometimes a type may be intended to satisfy an interface but may fail to
220 do so because of a mistake in its method signature.
221 For example, the result of this WriteTo method should be (int64, error),
222 not error, to satisfy io.WriterTo:
225 type myWriterTo struct{...}
226 func (myWriterTo) WriteTo(w io.Writer) error { ... }
229 This check ensures that each method whose name matches one of several
230 well-known interface methods from the standard library has the correct
231 signature for that interface.
233 Checked method names include:
234 Format GobEncode GobDecode MarshalJSON MarshalXML
235 Peek ReadByte ReadFrom ReadRune Scan Seek
236 UnmarshalJSON UnreadByte UnreadRune WriteByte
239 Default value: `true`.
243 check that struct field tags conform to reflect.StructTag.Get
245 Also report certain struct tags (json, xml) used with unexported fields.
247 Default value: `true`.
251 check for common mistaken usages of tests and examples
253 The tests checker walks Test, Benchmark and Example functions checking
254 malformed names, wrong signatures and examples documenting non-existent
257 Please see the documentation for package testing in golang.org/pkg/testing
258 for the conventions that are enforced for Tests, Benchmarks, and Examples.
260 Default value: `true`.
264 report passing non-pointer or non-interface values to unmarshal
266 The unmarshal analysis reports calls to functions such as json.Unmarshal
267 in which the argument type is not a pointer or an interface.
269 Default value: `true`.
273 check for unreachable code
275 The unreachable analyzer finds statements that execution can never reach
276 because they are preceded by an return statement, a call to panic, an
277 infinite loop, or similar constructs.
279 Default value: `true`.
283 check for invalid conversions of uintptr to unsafe.Pointer
285 The unsafeptr analyzer reports likely incorrect uses of unsafe.Pointer
286 to convert integers to pointers. A conversion from uintptr to
287 unsafe.Pointer is invalid if it implies that there is a uintptr-typed
288 word in memory that holds a pointer value, because that word will be
289 invisible to stack copying and to the garbage collector.
291 Default value: `true`.
295 check for unused results of calls to some functions
297 Some functions like fmt.Errorf return a result and have no side effects,
298 so it is always a mistake to discard the result. This analyzer reports
299 calls to certain functions in which the result of the call is ignored.
301 The set of functions may be controlled using flags.
303 Default value: `true`.
307 Below is the list of analyzers that are used by `gopls`.
309 ### **deepequalerrors**
311 check for calls of reflect.DeepEqual on error values
313 The deepequalerrors checker looks for calls of the form:
316 reflect.DeepEqual(err1, err2)
319 where err1 and err2 are errors. Using reflect.DeepEqual to compare
320 errors is discouraged.
322 Default value: `true`.
326 suggested fixes for "wrong number of return values (want %d, got %d)"
328 This checker provides suggested fixes for type errors of the
329 type "wrong number of return values (want %d, got %d)". For example:
331 func m() (int, string, *bool, error) {
337 func m() (int, string, *bool, error) {
338 return 0, "", nil, nil
342 This functionality is similar to [goreturns](https://github.com/sqs/goreturns).
344 Default value: `false`.
348 suggested fixes for "no new vars on left side of :="
350 This checker provides suggested fixes for type errors of the
351 type "no new vars on left side of :=". For example:
362 Default value: `false`.
364 ### **noresultvalues**
366 suggested fixes for "no result values expected"
368 This checker provides suggested fixes for type errors of the
369 type "no result values expected". For example:
371 func z() { return nil }
378 Default value: `true`.
380 ### **simplifycompositelit**
382 check for composite literal simplifications
384 An array, slice, or map composite literal of the form:
388 will be simplified to:
393 This is one of the simplifications that "gofmt -s" applies.
395 Default value: `true`.
397 ### **simplifyrange**
399 check for range statement simplifications
403 for x, _ = range v {...}
405 will be simplified to:
407 for x = range v {...}
412 for _ = range v {...}
414 will be simplified to:
419 This is one of the simplifications that "gofmt -s" applies.
421 Default value: `true`.
423 ### **simplifyslice**
425 check for slice simplifications
427 A slice expression of the form:
431 will be simplified to:
436 This is one of the simplifications that "gofmt -s" applies.
438 Default value: `true`.
442 check the argument type of sort.Slice
444 sort.Slice requires an argument of a slice type. Check that
445 the interface{} value passed to sort.Slice is actually a slice.
447 Default value: `true`.
449 ### **testinggoroutine**
451 report calls to (*testing.T).Fatal from goroutines started by a test.
453 Functions that abruptly terminate a test, such as the Fatal, Fatalf, FailNow, and
454 Skip{,f,Now} methods of *testing.T, must be called from the test goroutine itself.
455 This checker detects calls to these functions that occur within a goroutine
456 started by the test. For example:
459 func TestFoo(t *testing.T) {
461 t.Fatal("oops") // error: (*T).Fatal called from non-test goroutine
466 Default value: `true`.
468 ### **undeclaredname**
470 suggested fixes for "undeclared name: <>"
472 This checker provides suggested fixes for type errors of the
473 type `undeclared name: <>`. It will insert a new statement:
476 Default value: `false`.
480 check for unused parameters of functions
482 The unusedparams analyzer checks functions to see if there are
483 any parameters that are not being used.
485 To reduce false positives it ignores:
487 - parameters that do not have a name or are underscored
488 - functions in test files
489 - functions with empty bodies or those with just a return stmt
491 Default value: `false`.