x/tools/go/analysis: -fix creates unnecessary import decls #71647
Labels
Analysis
Issues related to static analysis (vet, x/tools/go/analysis)
NeedsFix
The path to resolution is known, but the work has not been done.
Refactoring
Issues related to refactoring tools
Milestone
Here is a fairly typical diff hunk from an analyzer, as applied by the -fix flag. Observe that it creates a new import decl, even though there's an existing one that it could add to. The original patch was computed by analysisinternal.AddImport, but applying gofmt (as -fix does) didn't smooth the two declarations into one; you need goimports for that.
AddImport could do a better job here. Perhaps it could always insert the new imports inside the top of any existing import decl; this may improve the final result, especially for std imports, which will be sorted. However, it may not be better (though nor is it worse) for non-std imports, which often appear in a second or later group separate by a blank line. In any case, there is room for improvement.
cc: @jba
The text was updated successfully, but these errors were encountered: