From 02a6ca6dc3c46a8e9d9b1bd5817e201aa8944bc4 Mon Sep 17 00:00:00 2001 From: Rohan Challa Date: Mon, 16 Mar 2020 11:49:00 -0400 Subject: [PATCH] internal/lsp: change disabledAnalyses setting to be general This change removes the disabledAnalyses setting and replaces it with a more general feature named "analyses". This will allow users to opt in as well as opt out of analyzers that they do not find useful. This also updates some documentation to show users what analyzers gopls is using and which are enabled by default. Change-Id: Id3239b4c4c9667e834f262c889270d14fdba0f93 Reviewed-on: https://go-review.googlesource.com/c/tools/+/223662 Run-TryBot: Rohan Challa TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- gopls/doc/analyzers.md | 350 +++++++++++++++++++++++++++++ gopls/doc/settings.md | 18 +- gopls/internal/hooks/analysis.go | 6 +- internal/lsp/cache/pkg.go | 6 +- internal/lsp/source/diagnostics.go | 16 +- internal/lsp/source/options.go | 89 ++++---- internal/lsp/source/view.go | 7 + 7 files changed, 442 insertions(+), 50 deletions(-) create mode 100644 gopls/doc/analyzers.md diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md new file mode 100644 index 000000000..f5dd8468f --- /dev/null +++ b/gopls/doc/analyzers.md @@ -0,0 +1,350 @@ +# Analyzers + + + +This document describes the analyzers that `gopls` uses inside the editor. + +A value of `true` means that the analyzer is enabled by default and a value of `false` means it is disabled by default. + +Details about how to enable/disable these analyses can be found [here](settings.md#analyses). + +## Go vet suite + +Below is the list of general analyzers that are used in `go vet`. + +### **asmdecl** + +report mismatches between assembly files and Go declarations + +Default value: `true`. + +### **assign** + +check for useless assignments + +This checker reports assignments of the form `x = x` or `a[i] = a[i]`. +These are almost always useless, and even when they aren't they are +usually a mistake. + +Default value: `true`. + +### **atomic** + +check for common mistakes using the sync/atomic package + +The atomic checker looks for assignment statements of the form: + +`x = atomic.AddUint64(&x, 1)` + +which are not atomic. + +Default value: `true`. + +### **atomicalign** + +check for non-64-bits-aligned arguments to sync/atomic functions + +Default value: `true`. + +### **bools** + +check for common mistakes involving boolean operators + +Default value: `true`. + +### **buildtag** + +check that +build tags are well-formed and correctly located + +Default value: `true`. + +### **cgocall** + +detect some violations of the cgo pointer passing rules + +Check for invalid cgo pointer passing. +This looks for code that uses cgo to call C code passing values +whose types are almost always invalid according to the cgo pointer +sharing rules. +Specifically, it warns about attempts to pass a Go chan, map, func, +or slice to C, either directly, or via a pointer, array, or struct. + +Default value: `true`. + +### **composite** + +check for unkeyed composite literals + +This analyzer reports a diagnostic for composite literals of struct +types imported from another package that do not use the field-keyed +syntax. Such literals are fragile because the addition of a new field +(even if unexported) to the struct will cause compilation to fail. + +As an example, +`err = &net.DNSConfigError{err}` + +should be replaced by: +`err = &net.DNSConfigError{Err: err}` + +Default value: `true`. + +### **copylock** + +check for locks erroneously passed by value + +Inadvertently copying a value containing a lock, such as sync.Mutex or +sync.WaitGroup, may cause both copies to malfunction. Generally such +values should be referred to through a pointer. + +Default value: `true`. + +### **errorsas** + +report passing non-pointer or non-error values to errors.As + +The errorsas analysis reports calls to errors.As where the type +of the second argument is not a pointer to a type implementing error. + +Default value: `true`. + +### **httpresponse** + +check for mistakes using HTTP responses + +A common mistake when using the net/http package is to defer a function +call to close the http.Response Body before checking the error that +determines whether the response is valid: + +```go +resp, err := http.Head(url) +defer resp.Body.Close() +if err != nil { + log.Fatal(err) +} +// (defer statement belongs here) +``` + +This checker helps uncover latent nil dereference bugs by reporting a +diagnostic for such mistakes. + +Default value: `true`. + +### **loopclosure** + +check references to loop variables from within nested functions + +This analyzer checks for references to loop variables from within a +function literal inside the loop body. It checks only instances where +the function literal is called in a defer or go statement that is the +last statement in the loop body, as otherwise we would need whole +program analysis. + +For example: +```go +for i, v := range s { + go func() { + println(i, v) // not what you might expect + }() +} +``` + +See: https://golang.org/doc/go_faq.html#closures_and_goroutines + +Default value: `true`. + +### **lostcancel** + +check cancel func returned by context.WithCancel is called + +The cancellation function returned by context.WithCancel, WithTimeout, +and WithDeadline must be called or the new context will remain live +until its parent context is cancelled. +(The background context is never cancelled.) + +Default value: `true`. + +### **nilfunc** + +check for useless comparisons between functions and nil + +A useless comparison is one like f == nil as opposed to f() == nil. + +Default value: `true`. + +### **printf** + +check consistency of Printf format strings and arguments + +The check applies to known functions (for example, those in package fmt) +as well as any detected wrappers of known functions. + +A function that wants to avail itself of printf checking but is not +found by this analyzer's heuristics (for example, due to use of +dynamic calls) can insert a bogus call: + +```go +if false { + _ = fmt.Sprintf(format, args...) // enable printf checking +} +``` + +The -funcs flag specifies a comma-separated list of names of additional +known formatting functions or methods. If the name contains a period, +it must denote a specific function using one of the following forms: + +``` + dir/pkg.Function + dir/pkg.Type.Method + (*dir/pkg.Type).Method +``` + +Otherwise the name is interpreted as a case-insensitive unqualified +identifier such as "errorf". Either way, if a listed name ends in f, the +function is assumed to be Printf-like, taking a format string before the +argument list. Otherwise it is assumed to be Print-like, taking a list +of arguments with no format string. + +Default value: `true`. + +### **shift** + +check for shifts that equal or exceed the width of the integer + +Default value: `true`. + +### **stdmethods** + +check signature of methods of well-known interfaces + +Sometimes a type may be intended to satisfy an interface but may fail to +do so because of a mistake in its method signature. +For example, the result of this WriteTo method should be (int64, error), +not error, to satisfy io.WriterTo: + +```go + type myWriterTo struct{...} + func (myWriterTo) WriteTo(w io.Writer) error { ... } +``` + +This check ensures that each method whose name matches one of several +well-known interface methods from the standard library has the correct +signature for that interface. + +Checked method names include: + Format GobEncode GobDecode MarshalJSON MarshalXML + Peek ReadByte ReadFrom ReadRune Scan Seek + UnmarshalJSON UnreadByte UnreadRune WriteByte + WriteTo + +Default value: `true`. + +### **structtag** + +check that struct field tags conform to reflect.StructTag.Get + +Also report certain struct tags (json, xml) used with unexported fields. + +Default value: `true`. + +### **tests** + +check for common mistaken usages of tests and examples + +The tests checker walks Test, Benchmark and Example functions checking +malformed names, wrong signatures and examples documenting non-existent +identifiers. + +Please see the documentation for package testing in golang.org/pkg/testing +for the conventions that are enforced for Tests, Benchmarks, and Examples. + +Default value: `true`. + +### **unmarshal** + +report passing non-pointer or non-interface values to unmarshal + +The unmarshal analysis reports calls to functions such as json.Unmarshal +in which the argument type is not a pointer or an interface. + +Default value: `true`. + +### **unreachable** + +check for unreachable code + +The unreachable analyzer finds statements that execution can never reach +because they are preceded by an return statement, a call to panic, an +infinite loop, or similar constructs. + +Default value: `true`. + +### **unsafeptr** + +check for invalid conversions of uintptr to unsafe.Pointer + +The unsafeptr analyzer reports likely incorrect uses of unsafe.Pointer +to convert integers to pointers. A conversion from uintptr to +unsafe.Pointer is invalid if it implies that there is a uintptr-typed +word in memory that holds a pointer value, because that word will be +invisible to stack copying and to the garbage collector. + +Default value: `true`. + +### **unusedresult** + +check for unused results of calls to some functions + +Some functions like fmt.Errorf return a result and have no side effects, +so it is always a mistake to discard the result. This analyzer reports +calls to certain functions in which the result of the call is ignored. + +The set of functions may be controlled using flags. + +Default value: `true`. + +## gopls suite + +Below is the list of analyzers that are used by `gopls`. + +### **deepequalerrors** + +check for calls of reflect.DeepEqual on error values + +The deepequalerrors checker looks for calls of the form: + +```go + reflect.DeepEqual(err1, err2) +``` + +where err1 and err2 are errors. Using reflect.DeepEqual to compare +errors is discouraged. + +Default value: `true`. + +### **sortslice** + +check the argument type of sort.Slice + +sort.Slice requires an argument of a slice type. Check that +the interface{} value passed to sort.Slice is actually a slice. + +Default value: `true`. + +### **testinggoroutine** + +report calls to (*testing.T).Fatal from goroutines started by a test. + +Functions that abruptly terminate a test, such as the Fatal, Fatalf, FailNow, and +Skip{,f,Now} methods of *testing.T, must be called from the test goroutine itself. +This checker detects calls to these functions that occur within a goroutine +started by the test. For example: + +```go +func TestFoo(t *testing.T) { + go func() { + t.Fatal("oops") // error: (*T).Fatal called from non-test goroutine + }() +} +``` + +Default value: `true`. \ No newline at end of file diff --git a/gopls/doc/settings.md b/gopls/doc/settings.md index b12f8f8b8..8c3d62a89 100644 --- a/gopls/doc/settings.md +++ b/gopls/doc/settings.md @@ -50,7 +50,7 @@ Default: `false`. This controls where points documentation for given package in `textDocument/documentLink`. It might be one of: -* `"godoc.org"` +* `"godoc.org"` * `"pkg.go.dev"` If company chooses to use its own `godoc.org`, its address can be used as well. @@ -67,9 +67,21 @@ Default: `""`. The below settings are considered experimental. They may be deprecated or changed in the future. They are typically used to test experimental opt-in features or to disable features. -### **experimentalDisabledAnalyses** *array of strings* +### **analyses** *map[string]bool* -A list of the names of analysis passes that should be disabled. You can use this to turn off analyses that you feel are not useful in the editor. +Analyses specify analyses that the user would like to enable or disable. +A map of the names of analysis passes that should be enabled/disabled. +A full list of analyzers that gopls uses can be found [here](analyzers.md) + +Example Usage: +```json5 +... +"analyses": { + "unreachable": false, // Disable the unreachable analyzer. + "unusedparams": true // Enable the unusedparams analyzer. +} +... +``` ### **staticcheck** *boolean* diff --git a/gopls/internal/hooks/analysis.go b/gopls/internal/hooks/analysis.go index 629bdc866..f3388b4c5 100644 --- a/gopls/internal/hooks/analysis.go +++ b/gopls/internal/hooks/analysis.go @@ -14,17 +14,17 @@ import ( func updateAnalyzers(options *source.Options) { if options.StaticCheck { for _, a := range simple.Analyzers { - options.Analyzers[a.Name] = a + options.Analyzers[a.Name] = source.Analyzer{Analyzer: a} } for _, a := range staticcheck.Analyzers { // This check conflicts with the vet printf check (golang/go#34494). if a.Name == "SA5009" { continue } - options.Analyzers[a.Name] = a + options.Analyzers[a.Name] = source.Analyzer{Analyzer: a} } for _, a := range stylecheck.Analyzers { - options.Analyzers[a.Name] = a + options.Analyzers[a.Name] = source.Analyzer{Analyzer: a} } } } diff --git a/internal/lsp/cache/pkg.go b/internal/lsp/cache/pkg.go index db32834c1..b408b0bdf 100644 --- a/internal/lsp/cache/pkg.go +++ b/internal/lsp/cache/pkg.go @@ -130,7 +130,11 @@ func (s *snapshot) FindAnalysisError(ctx context.Context, pkgID, analyzerName, m if !ok { return nil, errors.Errorf("unexpected analyzer: %s", analyzerName) } - act, err := s.actionHandle(ctx, packageID(pkgID), analyzer) + if !analyzer.Enabled { + return nil, errors.Errorf("disabled analyzer: %s", analyzerName) + } + + act, err := s.actionHandle(ctx, packageID(pkgID), analyzer.Analyzer) if err != nil { return nil, err } diff --git a/internal/lsp/source/diagnostics.go b/internal/lsp/source/diagnostics.go index b6ca43c8c..525347fdf 100644 --- a/internal/lsp/source/diagnostics.go +++ b/internal/lsp/source/diagnostics.go @@ -124,7 +124,7 @@ func Diagnostics(ctx context.Context, snapshot Snapshot, ph PackageHandle, missi return nil, warn, ctx.Err() } // If we don't have any list, parse, or type errors, run analyses. - if err := analyses(ctx, snapshot, reports, ph, snapshot.View().Options().DisabledAnalyses); err != nil { + if err := analyses(ctx, snapshot, reports, ph); err != nil { if ctx.Err() != nil { return nil, warn, ctx.Err() } @@ -269,13 +269,19 @@ func missingModulesDiagnostics(ctx context.Context, snapshot Snapshot, reports m return nil } -func analyses(ctx context.Context, snapshot Snapshot, reports map[FileIdentity][]Diagnostic, ph PackageHandle, disabledAnalyses map[string]struct{}) error { +func analyses(ctx context.Context, snapshot Snapshot, reports map[FileIdentity][]Diagnostic, ph PackageHandle) error { var analyzers []*analysis.Analyzer - for _, a := range snapshot.View().Options().Analyzers { - if _, ok := disabledAnalyses[a.Name]; ok { + for name, a := range snapshot.View().Options().Analyzers { + if enabled, ok := snapshot.View().Options().UserEnabledAnalyses[name]; ok { + if enabled { + analyzers = append(analyzers, a.Analyzer) + } continue } - analyzers = append(analyzers, a) + if !a.Enabled { + continue + } + analyzers = append(analyzers, a.Analyzer) } diagnostics, err := snapshot.Analyze(ctx, ph.ID(), analyzers) diff --git a/internal/lsp/source/options.go b/internal/lsp/source/options.go index 72c3729af..febc96a34 100644 --- a/internal/lsp/source/options.go +++ b/internal/lsp/source/options.go @@ -10,7 +10,6 @@ import ( "regexp" "time" - "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/asmdecl" "golang.org/x/tools/go/analysis/passes/assign" "golang.org/x/tools/go/analysis/passes/atomic" @@ -130,8 +129,17 @@ type UserOptions struct { // HoverKind specifies the format of the content for hover requests. HoverKind HoverKind - // DisabledAnalyses specify analyses that the user would like to disable. - DisabledAnalyses map[string]struct{} + // UserEnabledAnalyses specify analyses that the user would like to enable or disable. + // A map of the names of analysis passes that should be enabled/disabled. + // A full list of analyzers that gopls uses can be found [here](analyzers.md) + // + // Example Usage: + // ... + // "analyses": { + // "unreachable": false, // Disable the unreachable analyzer. + // "unusedparams": true // Enable the unusedparams analyzer. + // } + UserEnabledAnalyses map[string]bool // StaticCheck enables additional analyses from staticcheck.io. StaticCheck bool @@ -176,7 +184,7 @@ type Hooks struct { GoDiff bool ComputeEdits diff.ComputeEdits URLRegexp *regexp.Regexp - Analyzers map[string]*analysis.Analyzer + Analyzers map[string]Analyzer } type ExperimentalOptions struct { @@ -365,15 +373,17 @@ func (o *Options) set(name string, value interface{}) OptionResult { } o.LinkTarget = linkTarget - case "experimentalDisabledAnalyses": - disabledAnalyses, ok := value.([]interface{}) + case "analyses": + allAnalyses, ok := value.(map[string]interface{}) if !ok { - result.errorf("Invalid type %T for []string option %q", value, name) + result.errorf("Invalid type %T for map[string]interface{} option %q", value, name) break } - o.DisabledAnalyses = make(map[string]struct{}) - for _, a := range disabledAnalyses { - o.DisabledAnalyses[fmt.Sprint(a)] = struct{}{} + o.UserEnabledAnalyses = make(map[string]bool) + for a, enabled := range allAnalyses { + if enabled, ok := enabled.(bool); ok { + o.UserEnabledAnalyses[a] = enabled + } } case "staticcheck": @@ -394,6 +404,9 @@ func (o *Options) set(name string, value interface{}) OptionResult { result.setBool(&o.TempModfile) // Deprecated settings. + case "experimentalDisabledAnalyses": + result.State = OptionDeprecated + case "wantSuggestedFixes": result.State = OptionDeprecated @@ -464,36 +477,36 @@ func (r *OptionResult) setBool(b *bool) { } } -func defaultAnalyzers() map[string]*analysis.Analyzer { - return map[string]*analysis.Analyzer{ +func defaultAnalyzers() map[string]Analyzer { + return map[string]Analyzer{ // The traditional vet suite: - asmdecl.Analyzer.Name: asmdecl.Analyzer, - assign.Analyzer.Name: assign.Analyzer, - atomic.Analyzer.Name: atomic.Analyzer, - atomicalign.Analyzer.Name: atomicalign.Analyzer, - bools.Analyzer.Name: bools.Analyzer, - buildtag.Analyzer.Name: buildtag.Analyzer, - cgocall.Analyzer.Name: cgocall.Analyzer, - composite.Analyzer.Name: composite.Analyzer, - copylock.Analyzer.Name: copylock.Analyzer, - errorsas.Analyzer.Name: errorsas.Analyzer, - httpresponse.Analyzer.Name: httpresponse.Analyzer, - loopclosure.Analyzer.Name: loopclosure.Analyzer, - lostcancel.Analyzer.Name: lostcancel.Analyzer, - nilfunc.Analyzer.Name: nilfunc.Analyzer, - printf.Analyzer.Name: printf.Analyzer, - shift.Analyzer.Name: shift.Analyzer, - stdmethods.Analyzer.Name: stdmethods.Analyzer, - structtag.Analyzer.Name: structtag.Analyzer, - tests.Analyzer.Name: tests.Analyzer, - unmarshal.Analyzer.Name: unmarshal.Analyzer, - unreachable.Analyzer.Name: unreachable.Analyzer, - unsafeptr.Analyzer.Name: unsafeptr.Analyzer, - unusedresult.Analyzer.Name: unusedresult.Analyzer, + asmdecl.Analyzer.Name: {Analyzer: asmdecl.Analyzer, Enabled: true}, + assign.Analyzer.Name: {Analyzer: assign.Analyzer, Enabled: true}, + atomic.Analyzer.Name: {Analyzer: atomic.Analyzer, Enabled: true}, + atomicalign.Analyzer.Name: {Analyzer: atomicalign.Analyzer, Enabled: true}, + bools.Analyzer.Name: {Analyzer: bools.Analyzer, Enabled: true}, + buildtag.Analyzer.Name: {Analyzer: buildtag.Analyzer, Enabled: true}, + cgocall.Analyzer.Name: {Analyzer: cgocall.Analyzer, Enabled: true}, + composite.Analyzer.Name: {Analyzer: composite.Analyzer, Enabled: true}, + copylock.Analyzer.Name: {Analyzer: copylock.Analyzer, Enabled: true}, + errorsas.Analyzer.Name: {Analyzer: errorsas.Analyzer, Enabled: true}, + httpresponse.Analyzer.Name: {Analyzer: httpresponse.Analyzer, Enabled: true}, + loopclosure.Analyzer.Name: {Analyzer: loopclosure.Analyzer, Enabled: true}, + lostcancel.Analyzer.Name: {Analyzer: lostcancel.Analyzer, Enabled: true}, + nilfunc.Analyzer.Name: {Analyzer: nilfunc.Analyzer, Enabled: true}, + printf.Analyzer.Name: {Analyzer: printf.Analyzer, Enabled: true}, + shift.Analyzer.Name: {Analyzer: shift.Analyzer, Enabled: true}, + stdmethods.Analyzer.Name: {Analyzer: stdmethods.Analyzer, Enabled: true}, + structtag.Analyzer.Name: {Analyzer: structtag.Analyzer, Enabled: true}, + tests.Analyzer.Name: {Analyzer: tests.Analyzer, Enabled: true}, + unmarshal.Analyzer.Name: {Analyzer: unmarshal.Analyzer, Enabled: true}, + unreachable.Analyzer.Name: {Analyzer: unreachable.Analyzer, Enabled: true}, + unsafeptr.Analyzer.Name: {Analyzer: unsafeptr.Analyzer, Enabled: true}, + unusedresult.Analyzer.Name: {Analyzer: unusedresult.Analyzer, Enabled: true}, // Non-vet analyzers - deepequalerrors.Analyzer.Name: deepequalerrors.Analyzer, - sortslice.Analyzer.Name: sortslice.Analyzer, - testinggoroutine.Analyzer.Name: testinggoroutine.Analyzer, + deepequalerrors.Analyzer.Name: {Analyzer: deepequalerrors.Analyzer, Enabled: true}, + sortslice.Analyzer.Name: {Analyzer: sortslice.Analyzer, Enabled: true}, + testinggoroutine.Analyzer.Name: {Analyzer: testinggoroutine.Analyzer, Enabled: true}, } } diff --git a/internal/lsp/source/view.go b/internal/lsp/source/view.go index ae14c3fa1..15bc75d17 100644 --- a/internal/lsp/source/view.go +++ b/internal/lsp/source/view.go @@ -363,6 +363,13 @@ const ( UnknownKind ) +// Analyzer represents a go/analysis analyzer with some boolean properties +// that let the user know how to use the analyzer. +type Analyzer struct { + Analyzer *analysis.Analyzer + Enabled bool +} + // Package represents a Go package that has been type-checked. It maintains // only the relevant fields of a *go/packages.Package. type Package interface {