Skip to content

proposal: x/tools/go/analysis/analysistest: support expectations for related information #75800

@tcard

Description

@tcard

Proposal Details

At the moment, want comments let you express expectations for diagnostic messages and facts:

// want "diag" "diag2" x:"fact1" x:"fact2" y:"fact3"

But there is no equivalent for other diagnostics: category, URL, suggested fixes, and related information. The only way to check those is through the analysistest.Result returned from analysistest.Run, which defeats the purpose of the analysistest package, as it doesn't much improve on just using the base analysis/checker package.

To keep it minimal, this proposal is restricted to expectations about related information diagnostics.

I propose to extend the want comment format such that, after each message expectation pattern, a related section may follow, with one or more expectations to check against the list of related informations for the diagnostics matched by the preceding message expectation.

The format of each related information expectation is:

expectation = [ Pos ":" ] Message ;
Pos = absolutePos | relativePos ;
relativePos = [ "+" | "-" ] line ;
absolutePos = [ filename ":" ] line [ ":" column ] ;

filename = text ;   # matched as a suffix of token.Position.Filename
line = int ;        # matched exactly with token.Position.Line
column = int ;      # matched exactly with token.Position.Column
Message = pattern ; # between `` or "", just like the base message patterns

Each capitalized nonterminal is matched against the corresponding field in analysis.RelatedInformation.

Some extra rules:

  • When Pos is specified, and a absolutePos, but Pos.file is unspecified, it is assumed to be the current file path.
  • When Pos is a relativePos, line is interpreted as relative to the comment's file path and line.
  • Otherwise, unspecified parts don't impose any constraint on the related information.
  • Each expectation must be matched with at least one related information, and, once matched, it isn't considered for matching with other related informations. This is similar to how base diagnostics expectations behave.
  • It is OK for some related information not to be matched with any expectation. This is in contrast with base diagnostics, which must all be matched with an expectation.

Examples

(in foo.go:23) // want "diag" related 20:"extra"

Or equivalently:

(in foo.go:23) // want "diag" related -3:"extra"

Matches:

foo.go:23: my diag
    foo.go:20:10: extra info # Matches line and message
    bar.go:23:15: not matched # OK not to be expected
foo.go:23: my diag
    foo.go:20:25: extra info # Matches line, any column is fine

Doesn't match:

foo.go:23: my diag
    bar.go:23: not matched
    # Missing matching related information
foo.go:23: my diag
    foo.go:9999: extra info  # Different line
foo.go:23: my diag
    bar.go:20: extra info  # Different file

(in foo.go:23) // want "diag" related "extra"

Doesn't match:

foo.go:23: my diag
    foo.go:20: extra info
    foo.go:10: more extra info # Expectation is "spent" on the first related information

(in foo.go:23) // want "diag" related "extra" "extra"

Matches:

foo.go:23: my diag
    foo.go:20: extra info
    foo.go:10: more extra info # Each related information matches an expectation

Doesn't match:

foo.go:23: my diag
    foo.go:20: extra info
   # Missing related information for one of the expectations

(in foo.go:23) // want "diag" related bar.go:20:"extra"

Matches:

foo.go:23: my diag
    bar.go:20:2-30:3: extra info # Matches file, line, and message

Doesn't match:

foo.go:23: my diag
    foo.go:20:2-30:3: extra info # Different file

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions