Skip to content

proposal: x/tools/go/analysis: add file content to Pass #40110

@stamblerre

Description

@stamblerre

go/analysis has support for suggested fixes, which allow analyzer authors to provide suggested edits to the file along with diagnostic warnings. In most cases, these work by replacing single AST nodes with new content, so the author does not have to consider the original content of the file.

However, more complicated fixes need to insert new lines or propose more complex edits. In such cases, it's often necessary to access the original content of the file. In the fillstruct analyzer, we do this by running go/printer.Fprint, which is expensive and doesn't necessarily produce the exact content of the file, but it's a good enough approximation. Progress on the gofumpt (#39805, CL 240118) analyzer is currently blocked because, to get correct diffs, it needs to access the original content of the file.

My proposal is that we add a field Content map[string][]byte to the analysis.Pass type. This will allow suggested fixes to look up the original content of a file by its name in order to generate accurate edits for suggested fixes.

An alternative would be that analyses that provide more complex suggested fixes should not use the go/analysis package, but that seems counterintuitive to me.

/cc @matloob @mvdan @joshbaum

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions