@@ -174,62 +174,27 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns
174
174
act := result .Action
175
175
176
176
// 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.
178
179
fileEdits := make (map [* token.File ]map [string ][]diff.Edit )
179
- fileContents := make (map [* token.File ][]byte )
180
180
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.
182
183
for _ , diag := range act .Diagnostics {
183
184
for _ , fix := range diag .SuggestedFixes {
184
-
185
185
// Assert that lazy fixes have a Category (#65578, #65087).
186
186
if inTools && len (fix .TextEdits ) == 0 && diag .Category == "" {
187
187
t .Errorf ("missing Diagnostic.Category for SuggestedFix without TextEdits (gopls requires the category for the name of the fix command" )
188
188
}
189
189
190
- // TODO(adonovan): factor in common with go/analysis/internal/checker.validateEdits.
191
-
192
190
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 )
227
192
if _ , ok := fileEdits [file ]; ! ok {
228
193
fileEdits [file ] = make (map [string ][]diff.Edit )
229
194
}
230
195
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 ),
233
198
New : string (edit .NewText ),
234
199
})
235
200
}
@@ -238,9 +203,10 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns
238
203
239
204
for file , fixes := range fileEdits {
240
205
// 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 )
244
210
continue
245
211
}
246
212
0 commit comments