| 1 | // Copyright 2025 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package driverutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/ |
| 6 | |
| 7 | // This file defines the validation of SuggestedFixes. |
| 8 | |
| 9 | import ( |
| 10 | "cmpscip-go gomod github.com/golang/go/src go1.25.0 cmp/" |
| 11 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 12 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 13 | "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/" |
| 14 | |
| 15 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 16 | ) |
| 17 | |
| 18 | // ValidateFixes validates the set of fixes for a single diagnostic. |
| 19 | // Any error indicates a bug in the originating analyzer. |
| 20 | // |
| 21 | // It updates fixes so that fixes[*].End.IsValid(). |
| 22 | // |
| 23 | // It may be used as part of an analysis driver implementation. |
| 24 | func ValidateFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/ValidateFixes().(fset *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#, a *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#, fixes []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.SuggestedFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#) error { |
| 25 | fixMessages := make(map[string]bool) |
| 26 | for i := range fixes { |
| 27 | fix := &fixes[i] |
| 28 | if fixMessages[fix.Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#Message.] { |
| 29 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("analyzer %q suggests two fixes with same Message (%s)", a.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name., fix.Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#Message.) |
| 30 | } |
| 31 | fixMessages[fix.Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#Message.] = true |
| 32 | if err := validateFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/validateFix().(fset, fix); err != nil { |
| 33 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("analyzer %q suggests invalid fix (%s): %v", a.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name., fix.Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#Message., err) |
| 34 | } |
| 35 | } |
| 36 | return nil |
| 37 | } |
| 38 | |
| 39 | // validateFix validates a single fix. |
| 40 | // Any error indicates a bug in the originating analyzer. |
| 41 | // |
| 42 | // It updates fix so that fix.End.IsValid(). |
| 43 | func validateFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/validateFix().(fset *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#, fix *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.SuggestedFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#) error { |
| 44 | |
| 45 | // Stably sort edits by Pos. This ordering puts insertions |
| 46 | // (end = start) before deletions (end > start) at the same |
| 47 | // point, but uses a stable sort to preserve the order of |
| 48 | // multiple insertions at the same point. |
| 49 | slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.SortStableFuncscip-go gomod github.com/golang/go/src go1.25.0 slices/SortStableFunc().(fix.TextEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#TextEdits., func(x, y analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#) int { |
| 50 | if sign := cmpscip-go gomod github.com/golang/go/src go1.25.0 cmp/.Comparescip-go gomod github.com/golang/go/src go1.25.0 cmp/Compare().(x.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos., y.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.); sign != 0 { |
| 51 | return sign |
| 52 | } |
| 53 | return cmpscip-go gomod github.com/golang/go/src go1.25.0 cmp/.Comparescip-go gomod github.com/golang/go/src go1.25.0 cmp/Compare().(x.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End., y.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.) |
| 54 | }) |
| 55 | |
| 56 | var prev *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit# |
| 57 | for i := range fix.TextEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#TextEdits. { |
| 58 | edit := &fix.TextEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#TextEdits.[i] |
| 59 | |
| 60 | // Validate edit individually. |
| 61 | start := edit.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos. |
| 62 | file := fset.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(start) |
| 63 | if file == nil { |
| 64 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("no token.File for TextEdit.Pos (%v)", edit.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.) |
| 65 | } |
| 66 | fileEnd := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#(file.Basescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Base().() + file.Sizescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Size().()) |
| 67 | if end := edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.; end.IsValidscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#IsValid().() { |
| 68 | if end < start { |
| 69 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("TextEdit.Pos (%v) > TextEdit.End (%v)", edit.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos., edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.) |
| 70 | } |
| 71 | endFile := fset.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(end) |
| 72 | if endFile != file && end < fileEnd+10 { |
| 73 | // Relax the checks below in the special case when the end position |
| 74 | // is only slightly beyond EOF, as happens when End is computed |
| 75 | // (as in ast.{Struct,Interface}Type) rather than based on |
| 76 | // actual token positions. In such cases, truncate end to EOF. |
| 77 | // |
| 78 | // This is a workaround for #71659; see: |
| 79 | // https://github.com/golang/go/issues/71659#issuecomment-2651606031 |
| 80 | // A better fix would be more faithful recording of token |
| 81 | // positions (or their absence) in the AST. |
| 82 | edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End. = fileEnd |
| 83 | continue |
| 84 | } |
| 85 | if endFile == nil { |
| 86 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("no token.File for TextEdit.End (%v; File(start).FileEnd is %d)", end, file.Basescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Base().()+file.Sizescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Size().()) |
| 87 | } |
| 88 | if endFile != file { |
| 89 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("edit #%d spans files (%v and %v)", |
| 90 | i, file.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Position().(edit.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.), endFile.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Position().(edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.)) |
| 91 | } |
| 92 | } else { |
| 93 | edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End. = start // update the SuggestedFix |
| 94 | } |
| 95 | if eof := fileEnd; edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End. > eof { |
| 96 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("end is (%v) beyond end of file (%v)", edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End., eof) |
| 97 | } |
| 98 | |
| 99 | // Validate the sequence of edits: |
| 100 | // properly ordered, no overlapping deletions |
| 101 | if prev != nil && edit.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos. < prev.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End. { |
| 102 | xpos := fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(prev.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.) |
| 103 | xend := fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(prev.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.) |
| 104 | ypos := fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(edit.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.) |
| 105 | yend := fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.) |
| 106 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("overlapping edits to %s (%d:%d-%d:%d and %d:%d-%d:%d)", |
| 107 | xpos.Filenamescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Filename., |
| 108 | xpos.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line., xpos.Columnscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Column., |
| 109 | xend.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line., xend.Columnscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Column., |
| 110 | ypos.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line., ypos.Columnscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Column., |
| 111 | yend.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line., yend.Columnscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Column., |
| 112 | ) |
| 113 | } |
| 114 | prev = edit |
| 115 | } |
| 116 | |
| 117 | return nil |
| 118 | } |