Skip to content

Commit 94c3c49

Browse files
committedFeb 7, 2025
go/analysis/analysistest: RunWithSuggestedFix: assume valid fixes
Recent work has caused internal/checker to validate fixes at the moment they are reported, panicking if invalid, so we can simplify the logic here. Later we'll support three-way merging of fixes. Change-Id: I10cc582afbeb62308252979e6db37b7ed10ddddc Reviewed-on: https://go-review.googlesource.com/c/tools/+/647699 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jonathan Amsterdam <jba@google.com> Auto-Submit: Alan Donovan <adonovan@google.com>
1 parent 5f9967d commit 94c3c49

File tree

1 file changed

+11
-45
lines changed

1 file changed

+11
-45
lines changed
 

‎go/analysis/analysistest/analysistest.go

+11-45
Original file line numberDiff line numberDiff line change
@@ -174,62 +174,27 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns
174174
act := result.Action
175175

176176
// file -> message -> edits
177-
// TODO(adonovan): this mapping assumes fix.Messages are unique across analyzers.
177+
// TODO(adonovan): this mapping assumes fix.Messages are unique across analyzers,
178+
// whereas they are only unique within a given Diagnostic.
178179
fileEdits := make(map[*token.File]map[string][]diff.Edit)
179-
fileContents := make(map[*token.File][]byte)
180180

181-
// Validate edits, prepare the fileEdits map and read the file contents.
181+
// We may assume that fixes are validated upon creation in Pass.Report.
182+
// Group fixes by file and message.
182183
for _, diag := range act.Diagnostics {
183184
for _, fix := range diag.SuggestedFixes {
184-
185185
// Assert that lazy fixes have a Category (#65578, #65087).
186186
if inTools && len(fix.TextEdits) == 0 && diag.Category == "" {
187187
t.Errorf("missing Diagnostic.Category for SuggestedFix without TextEdits (gopls requires the category for the name of the fix command")
188188
}
189189

190-
// TODO(adonovan): factor in common with go/analysis/internal/checker.validateEdits.
191-
192190
for _, edit := range fix.TextEdits {
193-
start, end := edit.Pos, edit.End
194-
if !end.IsValid() {
195-
end = start
196-
}
197-
// Validate the edit.
198-
if start > end {
199-
t.Errorf(
200-
"diagnostic for analysis %v contains Suggested Fix with malformed edit: pos (%v) > end (%v)",
201-
act.Analyzer.Name, start, end)
202-
continue
203-
}
204-
file := act.Package.Fset.File(start)
205-
if file == nil {
206-
t.Errorf("diagnostic for analysis %v contains Suggested Fix with malformed start position %v", act.Analyzer.Name, start)
207-
continue
208-
}
209-
endFile := act.Package.Fset.File(end)
210-
if endFile == nil {
211-
t.Errorf("diagnostic for analysis %v contains Suggested Fix with malformed end position %v", act.Analyzer.Name, end)
212-
continue
213-
}
214-
if file != endFile {
215-
t.Errorf(
216-
"diagnostic for analysis %v contains Suggested Fix with malformed spanning files %v and %v",
217-
act.Analyzer.Name, file.Name(), endFile.Name())
218-
continue
219-
}
220-
if _, ok := fileContents[file]; !ok {
221-
contents, err := os.ReadFile(file.Name())
222-
if err != nil {
223-
t.Errorf("error reading %s: %v", file.Name(), err)
224-
}
225-
fileContents[file] = contents
226-
}
191+
file := act.Package.Fset.File(edit.Pos)
227192
if _, ok := fileEdits[file]; !ok {
228193
fileEdits[file] = make(map[string][]diff.Edit)
229194
}
230195
fileEdits[file][fix.Message] = append(fileEdits[file][fix.Message], diff.Edit{
231-
Start: file.Offset(start),
232-
End: file.Offset(end),
196+
Start: file.Offset(edit.Pos),
197+
End: file.Offset(edit.End),
233198
New: string(edit.NewText),
234199
})
235200
}
@@ -238,9 +203,10 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns
238203

239204
for file, fixes := range fileEdits {
240205
// Get the original file contents.
241-
orig, ok := fileContents[file]
242-
if !ok {
243-
t.Errorf("could not find file contents for %s", file.Name())
206+
// TODO(adonovan): plumb pass.ReadFile.
207+
orig, err := os.ReadFile(file.Name())
208+
if err != nil {
209+
t.Errorf("error reading %s: %v", file.Name(), err)
244210
continue
245211
}
246212

0 commit comments

Comments
 (0)