1
0
mirror of https://github.com/golang/go synced 2024-11-18 13:14:47 -07:00

internal/lsp: hide analysis diagnostics from generated files

Don't show non-vet analyses when they appear in generated files. Vet
analyzers will give useful reports even in generated files.

Fixes golang/go#38467

Change-Id: I0e628760b386553932de4cf1f5ba39784a205b53
Reviewed-on: https://go-review.googlesource.com/c/tools/+/230597
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Rebecca Stambler 2020-04-28 15:16:33 -04:00
parent 0c9eba77bc
commit e4881b2459
6 changed files with 61 additions and 34 deletions

View File

@ -294,6 +294,10 @@ func analyses(ctx context.Context, snapshot Snapshot, reports map[FileIdentity][
if onlyDeletions(e.SuggestedFixes) {
tags = append(tags, protocol.Unnecessary)
}
// Don't show non-vet analysis diagnostics for generated files.
if !isVetAnalyzer(e.Category) && IsGenerated(ctx, snapshot, e.URI) {
continue
}
if err := addReports(snapshot, reports, e.URI, &Diagnostic{
Range: e.Range,
Message: e.Message,

View File

@ -530,32 +530,7 @@ func typeErrorAnalyzers() map[string]Analyzer {
}
func defaultAnalyzers() map[string]Analyzer {
return map[string]Analyzer{
// The traditional vet suite:
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},
m := map[string]Analyzer{
// Non-vet analyzers
deepequalerrors.Analyzer.Name: {Analyzer: deepequalerrors.Analyzer, enabled: true},
sortslice.Analyzer.Name: {Analyzer: sortslice.Analyzer, enabled: true},
@ -567,4 +542,39 @@ func defaultAnalyzers() map[string]Analyzer {
simplifyrange.Analyzer.Name: {Analyzer: simplifyrange.Analyzer, enabled: true, HighConfidence: true},
simplifyslice.Analyzer.Name: {Analyzer: simplifyslice.Analyzer, enabled: true, HighConfidence: true},
}
for k, v := range vetAnalyzers {
m[k] = v
}
return m
}
func isVetAnalyzer(name string) bool {
_, ok := vetAnalyzers[name]
return ok
}
var vetAnalyzers = map[string]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},
}

View File

@ -0,0 +1,12 @@
package generated2
import "fmt"
// Code generated by generator.go. DO NOT EDIT.
func _() {
for i := range []string{} {
_ = i
}
fmt.Printf("%s") //@diag("fmt.Printf(\"%s\")", "printf", "Printf format %s reads arg #1, but call has 0 args", "warning")
}

View File

@ -7,7 +7,7 @@ DeepCompletionsCount = 5
FuzzyCompletionsCount = 8
RankedCompletionsCount = 120
CaseSensitiveCompletionsCount = 4
DiagnosticsCount = 43
DiagnosticsCount = 44
FoldingRangesCount = 2
FormatCount = 6
ImportCount = 8

View File

@ -808,7 +808,7 @@ func checkData(t *testing.T, data *Data) {
}))
got := buf.String()
if want != got {
t.Errorf("test summary does not match, want\n%s\ngot:\n%s", want, got)
t.Errorf("test summary does not match:\n%s", Diff(want, got))
}
}

View File

@ -175,13 +175,13 @@ func DiffDiagnostics(uri span.URI, want, got []*source.Diagnostic) string {
for i, w := range want {
g := got[i]
if w.Message != g.Message {
return summarizeDiagnostics(i, uri, want, got, "incorrect Message got %v want %v", g.Message, w.Message)
return summarizeDiagnostics(i, uri, want, got, "incorrect Message:\n%s", Diff(w.Message, g.Message))
}
if w.Severity != g.Severity {
return summarizeDiagnostics(i, uri, want, got, "incorrect Severity got %v want %v", g.Severity, w.Severity)
}
if w.Source != g.Source {
return summarizeDiagnostics(i, uri, want, got, "incorrect Source got %v want %v", g.Source, w.Source)
return summarizeDiagnostics(i, uri, want, got, "incorrect Source:\n%s", Diff(w.Source, g.Source))
}
// Don't check the range on the badimport test.
if strings.Contains(uri.Filename(), "badimport") {
@ -207,14 +207,15 @@ func summarizeDiagnostics(i int, uri span.URI, want, got []*source.Diagnostic, r
}
fmt.Fprint(msg, " because of ")
fmt.Fprintf(msg, reason, args...)
fmt.Fprint(msg, ":\nexpected:\n")
w := &bytes.Buffer{}
for _, d := range want {
fmt.Fprintf(msg, " %s:%v: %s\n", uri, d.Range, d.Message)
fmt.Fprintf(w, " %s:%v: %s %s %s\n", uri, d.Range, d.Source, d.Message, d.Severity)
}
fmt.Fprintf(msg, "got:\n")
g := &bytes.Buffer{}
for _, d := range got {
fmt.Fprintf(msg, " %s:%v: %s\n", uri, d.Range, d.Message)
fmt.Fprintf(g, " %s:%v: %s %s %s\n", uri, d.Range, d.Source, d.Message, d.Severity)
}
fmt.Fprintf(msg, "\nFull diff:\n%s", Diff(w.String(), g.String()))
return msg.String()
}