mirror of
https://github.com/golang/go
synced 2024-11-18 18:04:46 -07:00
internal/lsp: allow fine-grained control over vet checks
This change adds an "experimentalDisabledAnalyses" configuration to the "gopls" configuration. A user can specify a list of excluded analyses by analyzer name. Fixes golang/go#31717 Change-Id: I4b162fcd61ecfcef5c926bd0e96f182748a7721d Reviewed-on: https://go-review.googlesource.com/c/tools/+/179920 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
parent
bca362e842
commit
fe937a7521
@ -28,7 +28,7 @@ func (s *Server) Diagnostics(ctx context.Context, v source.View, uri span.URI) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
reports, err := source.Diagnostics(ctx, v, gof)
|
||||
reports, err := source.Diagnostics(ctx, v, gof, s.disabledAnalyses)
|
||||
if err != nil {
|
||||
s.session.Logger().Errorf(ctx, "failed to compute diagnostics for %s: %v", gof.URI(), err)
|
||||
return
|
||||
|
@ -188,6 +188,15 @@ func (s *Server) processConfig(view source.View, config interface{}) error {
|
||||
if noDocsOnHover, ok := c["noDocsOnHover"].(bool); ok {
|
||||
s.noDocsOnHover = noDocsOnHover
|
||||
}
|
||||
// Check if the user has explicitly disabled any analyses.
|
||||
if disabledAnalyses, ok := c["experimentalDisabledAnalyses"].([]interface{}); ok {
|
||||
s.disabledAnalyses = make(map[string]struct{})
|
||||
for _, a := range disabledAnalyses {
|
||||
if a, ok := a.(string); ok {
|
||||
s.disabledAnalyses[a] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -68,7 +68,7 @@ func (r *runner) Diagnostics(t *testing.T, data tests.Diagnostics) {
|
||||
if !ok {
|
||||
t.Fatalf("%s is not a Go file: %v", uri, err)
|
||||
}
|
||||
results, err := source.Diagnostics(context.Background(), v, gof)
|
||||
results, err := source.Diagnostics(context.Background(), v, gof, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -79,6 +79,7 @@ type Server struct {
|
||||
configurationSupported bool
|
||||
dynamicConfigurationSupported bool
|
||||
preferredContentFormat protocol.MarkupKind
|
||||
disabledAnalyses map[string]struct{}
|
||||
|
||||
textDocumentSyncKind protocol.TextDocumentSyncKind
|
||||
|
||||
|
@ -51,7 +51,7 @@ const (
|
||||
SeverityError
|
||||
)
|
||||
|
||||
func Diagnostics(ctx context.Context, v View, f GoFile) (map[span.URI][]Diagnostic, error) {
|
||||
func Diagnostics(ctx context.Context, v View, f GoFile, disabledAnalyses map[string]struct{}) (map[span.URI][]Diagnostic, error) {
|
||||
pkg := f.GetPackage(ctx)
|
||||
if pkg == nil {
|
||||
return singleDiagnostic(f.URI(), "%s is not part of a package", f.URI()), nil
|
||||
@ -70,7 +70,7 @@ func Diagnostics(ctx context.Context, v View, f GoFile) (map[span.URI][]Diagnost
|
||||
// Run diagnostics for the package that this URI belongs to.
|
||||
if !diagnostics(ctx, v, pkg, reports) {
|
||||
// If we don't have any list, parse, or type errors, run analyses.
|
||||
if err := analyses(ctx, v, pkg, reports); err != nil {
|
||||
if err := analyses(ctx, v, pkg, disabledAnalyses, reports); err != nil {
|
||||
v.Session().Logger().Errorf(ctx, "failed to run analyses for %s: %v", f.URI(), err)
|
||||
}
|
||||
}
|
||||
@ -126,9 +126,9 @@ func diagnostics(ctx context.Context, v View, pkg Package, reports map[span.URI]
|
||||
return len(diags) != 0
|
||||
}
|
||||
|
||||
func analyses(ctx context.Context, v View, pkg Package, reports map[span.URI][]Diagnostic) error {
|
||||
func analyses(ctx context.Context, v View, pkg Package, disabledAnalyses map[string]struct{}, reports map[span.URI][]Diagnostic) error {
|
||||
// Type checking and parsing succeeded. Run analyses.
|
||||
if err := runAnalyses(ctx, v, pkg, func(a *analysis.Analyzer, diag analysis.Diagnostic) error {
|
||||
if err := runAnalyses(ctx, v, pkg, disabledAnalyses, func(a *analysis.Analyzer, diag analysis.Diagnostic) error {
|
||||
r := span.NewRange(v.Session().Cache().FileSet(), diag.Pos, diag.End)
|
||||
s, err := r.Span()
|
||||
if err != nil {
|
||||
@ -234,9 +234,10 @@ func singleDiagnostic(uri span.URI, format string, a ...interface{}) map[span.UR
|
||||
}
|
||||
}
|
||||
|
||||
func runAnalyses(ctx context.Context, v View, pkg Package, report func(a *analysis.Analyzer, diag analysis.Diagnostic) error) error {
|
||||
func runAnalyses(ctx context.Context, v View, pkg Package, disabledAnalyses map[string]struct{}, report func(a *analysis.Analyzer, diag analysis.Diagnostic) error) error {
|
||||
// The traditional vet suite:
|
||||
analyzers := []*analysis.Analyzer{
|
||||
var analyzers []*analysis.Analyzer
|
||||
for _, a := range []*analysis.Analyzer{
|
||||
asmdecl.Analyzer,
|
||||
assign.Analyzer,
|
||||
atomic.Analyzer,
|
||||
@ -259,6 +260,11 @@ func runAnalyses(ctx context.Context, v View, pkg Package, report func(a *analys
|
||||
unreachable.Analyzer,
|
||||
unsafeptr.Analyzer,
|
||||
unusedresult.Analyzer,
|
||||
} {
|
||||
if _, ok := disabledAnalyses[a.Name]; ok {
|
||||
continue
|
||||
}
|
||||
analyzers = append(analyzers, a)
|
||||
}
|
||||
|
||||
roots, err := analyze(ctx, v, []Package{pkg}, analyzers)
|
||||
|
@ -55,7 +55,7 @@ func (r *runner) Diagnostics(t *testing.T, data tests.Diagnostics) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
results, err := source.Diagnostics(context.Background(), r.view, f.(source.GoFile))
|
||||
results, err := source.Diagnostics(context.Background(), r.view, f.(source.GoFile), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user