Skip to content

Commit 525acd1

Browse files
committed
go/analysis/internal/analysisflags: add RelatedInformation JSON
This change adds RelatedInformation to the JSON output of the analysis tools. (None of the analyzers we maintain currently uses this field.) Also, tighten up the doc comments. Fixes golang/go#64548 Change-Id: I7087858083c1eec917e00fe840c2a5aa8eb2e898 Reviewed-on: https://go-review.googlesource.com/c/tools/+/556820 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent c7ccb51 commit 525acd1

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

go/analysis/diagnostic.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ type Diagnostic struct {
3131
// see https://pkg.go.dev/net/url#URL.ResolveReference.
3232
URL string
3333

34-
// SuggestedFixes contains suggested fixes for a diagnostic
35-
// which can be used to perform edits to a file that address
36-
// the diagnostic.
37-
//
38-
// Diagnostics should not contain SuggestedFixes that overlap.
39-
SuggestedFixes []SuggestedFix // optional
34+
// SuggestedFixes is an optional list of fixes to address the
35+
// problem described by the diagnostic, each one representing
36+
// an alternative strategy; at most one may be applied.
37+
SuggestedFixes []SuggestedFix
4038

41-
Related []RelatedInformation // optional
39+
// Related contains optional secondary positions and messages
40+
// related to the primary diagnostic.
41+
Related []RelatedInformation
4242
}
4343

4444
// RelatedInformation contains information related to a diagnostic.
@@ -55,8 +55,7 @@ type RelatedInformation struct {
5555
// user can choose to apply to their code. Usually the SuggestedFix is
5656
// meant to fix the issue flagged by the diagnostic.
5757
//
58-
// TextEdits for a SuggestedFix should not overlap,
59-
// nor contain edits for other packages.
58+
// The TextEdits must not overlap, nor contain edits for other packages.
6059
type SuggestedFix struct {
6160
// A description for this suggested fix to be shown to a user deciding
6261
// whether to accept it.

go/analysis/internal/analysisflags/flags.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,24 @@ type JSONSuggestedFix struct {
362362
Edits []JSONTextEdit `json:"edits"`
363363
}
364364

365-
// A JSONDiagnostic can be used to encode and decode analysis.Diagnostics to and
366-
// from JSON.
367-
// TODO(matloob): Should the JSON diagnostics contain ranges?
368-
// If so, how should they be formatted?
365+
// A JSONDiagnostic describes the JSON schema of an analysis.Diagnostic.
366+
//
367+
// TODO(matloob): include End position if present.
369368
type JSONDiagnostic struct {
370-
Category string `json:"category,omitempty"`
371-
Posn string `json:"posn"`
372-
Message string `json:"message"`
373-
SuggestedFixes []JSONSuggestedFix `json:"suggested_fixes,omitempty"`
369+
Category string `json:"category,omitempty"`
370+
Posn string `json:"posn"` // e.g. "file.go:line:column"
371+
Message string `json:"message"`
372+
SuggestedFixes []JSONSuggestedFix `json:"suggested_fixes,omitempty"`
373+
Related []JSONRelatedInformation `json:"related,omitempty"`
374+
}
375+
376+
// A JSONRelated describes a secondary position and message related to
377+
// a primary diagnostic.
378+
//
379+
// TODO(adonovan): include End position if present.
380+
type JSONRelatedInformation struct {
381+
Posn string `json:"posn"` // e.g. "file.go:line:column"
382+
Message string `json:"message"`
374383
}
375384

376385
// Add adds the result of analysis 'name' on package 'id'.
@@ -401,11 +410,19 @@ func (tree JSONTree) Add(fset *token.FileSet, id, name string, diags []analysis.
401410
Edits: edits,
402411
})
403412
}
413+
var related []JSONRelatedInformation
414+
for _, r := range f.Related {
415+
related = append(related, JSONRelatedInformation{
416+
Posn: fset.Position(r.Pos).String(),
417+
Message: r.Message,
418+
})
419+
}
404420
jdiag := JSONDiagnostic{
405421
Category: f.Category,
406422
Posn: fset.Position(f.Pos).String(),
407423
Message: f.Message,
408424
SuggestedFixes: fixes,
425+
Related: related,
409426
}
410427
diagnostics = append(diagnostics, jdiag)
411428
}

0 commit comments

Comments
 (0)