| 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 refactorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/ |
| 6 | |
| 7 | // This file defines operations for computing deletion edits. |
| 8 | |
| 9 | import ( |
| 10 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 11 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 12 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 13 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 14 | "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/" |
| 15 | |
| 16 | "golang.org/x/tools/go/ast/edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/" |
| 17 | "golang.org/x/tools/go/ast/inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/" |
| 18 | "golang.org/x/tools/internal/astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/" |
| 19 | "golang.org/x/tools/internal/typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/" |
| 20 | "golang.org/x/tools/internal/typesinternal/typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/" |
| 21 | ) |
| 22 | |
| 23 | // DeleteVar returns edits to delete the declaration of a variable or |
| 24 | // constant whose defining identifier is curId. |
| 25 | // |
| 26 | // It handles variants including: |
| 27 | // - GenDecl > ValueSpec versus AssignStmt; |
| 28 | // - RHS expression has effects, or not; |
| 29 | // - entire statement/declaration may be eliminated; |
| 30 | // and removes associated comments. |
| 31 | // |
| 32 | // If it cannot make the necessary edits, such as for a function |
| 33 | // parameter or result, it returns nil. |
| 34 | func DeleteVarscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/DeleteVar().(tokFile *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#, info *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Infoscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#, curId inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#) []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit# { |
| 35 | switch curId.ParentEdgeKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdgeKind().() { |
| 36 | case edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.ValueSpec_Namesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/ValueSpec_Names.: |
| 37 | return deleteVarFromValueSpecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/deleteVarFromValueSpec().(tokFile, info, curId) |
| 38 | |
| 39 | case edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.AssignStmt_Lhsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/AssignStmt_Lhs.: |
| 40 | return deleteVarFromAssignStmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/deleteVarFromAssignStmt().(tokFile, info, curId) |
| 41 | } |
| 42 | |
| 43 | // e.g. function receiver, parameter, or result, |
| 44 | // or "switch v := expr.(T) {}" (which has no object). |
| 45 | return nil |
| 46 | } |
| 47 | |
| 48 | // deleteVarFromValueSpec returns edits to delete the declaration of a |
| 49 | // variable or constant within a ValueSpec. |
| 50 | // |
| 51 | // Precondition: curId is Ident beneath ValueSpec.Names beneath GenDecl. |
| 52 | // |
| 53 | // See also [deleteVarFromAssignStmt], which has parallel structure. |
| 54 | func deleteVarFromValueSpecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/deleteVarFromValueSpec().(tokFile *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#, info *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Infoscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#, curIdent inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#) []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit# { |
| 55 | var ( |
| 56 | id = curIdent.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) |
| 57 | curSpec = curIdent.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().() |
| 58 | spec = curSpec.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ValueSpecscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#) |
| 59 | ) |
| 60 | |
| 61 | declaresOtherNames := slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.ContainsFuncscip-go gomod github.com/golang/go/src go1.25.0 slices/ContainsFunc().(spec.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Names., func(name *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) bool { |
| 62 | return name != id && name.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. != "_" |
| 63 | }) |
| 64 | noRHSEffects := !slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.ContainsFuncscip-go gomod github.com/golang/go/src go1.25.0 slices/ContainsFunc().(spec.Valuesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Values., func(rhs astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#) bool { |
| 65 | return !typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.NoEffectsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/NoEffects().(info, rhs) |
| 66 | }) |
| 67 | if !declaresOtherNames && noRHSEffects { |
| 68 | // The spec is no longer needed, either to declare |
| 69 | // other variables, or for its side effects. |
| 70 | return DeleteSpecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/DeleteSpec().(tokFile, curSpec) |
| 71 | } |
| 72 | |
| 73 | // The spec is still needed, either for |
| 74 | // at least one LHS, or for effects on RHS. |
| 75 | // Blank out or delete just one LHS. |
| 76 | |
| 77 | index := curIdent.ParentEdgeIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdgeIndex().() // index of LHS within ValueSpec.Names |
| 78 | |
| 79 | // If there is no RHS, we can delete the LHS. |
| 80 | if len(spec.Valuesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Values.) == 0 { |
| 81 | var pos, end 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# |
| 82 | if index == len(spec.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Names.)-1 { |
| 83 | // Delete final name. |
| 84 | // |
| 85 | // var _, lhs1 T |
| 86 | // ------ |
| 87 | pos = spec.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Names.[index-1].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#End().() |
| 88 | end = spec.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Names.[index].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#End().() |
| 89 | } else { |
| 90 | // Delete non-final name. |
| 91 | // |
| 92 | // var lhs0, _ T |
| 93 | // ------ |
| 94 | pos = spec.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Names.[index].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Pos().() |
| 95 | end = spec.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Names.[index+1].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Pos().() |
| 96 | } |
| 97 | return []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit#{{ |
| 98 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: pos, |
| 99 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: end, |
| 100 | }} |
| 101 | } |
| 102 | |
| 103 | // If the assignment is n:n and the RHS has no effects, |
| 104 | // we can delete the LHS and its corresponding RHS. |
| 105 | if len(spec.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Names.) == len(spec.Valuesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Values.) && |
| 106 | typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.NoEffectsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/NoEffects().(info, spec.Valuesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Values.[index]) { |
| 107 | |
| 108 | if index == len(spec.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Names.)-1 { |
| 109 | // Delete final items. |
| 110 | // |
| 111 | // var _, lhs1 = rhs0, rhs1 |
| 112 | // ------ ------ |
| 113 | return []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit#{ |
| 114 | { |
| 115 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: spec.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Names.[index-1].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#End().(), |
| 116 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: spec.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Names.[index].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#End().(), |
| 117 | }, |
| 118 | { |
| 119 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: spec.Valuesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Values.[index-1].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(), |
| 120 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: spec.Valuesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Values.[index].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(), |
| 121 | }, |
| 122 | } |
| 123 | } else { |
| 124 | // Delete non-final items. |
| 125 | // |
| 126 | // var lhs0, _ = rhs0, rhs1 |
| 127 | // ------ ------ |
| 128 | return []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit#{ |
| 129 | { |
| 130 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: spec.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Names.[index].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Pos().(), |
| 131 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: spec.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Names.[index+1].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Pos().(), |
| 132 | }, |
| 133 | { |
| 134 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: spec.Valuesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Values.[index].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), |
| 135 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: spec.Valuesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Values.[index+1].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), |
| 136 | }, |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // We cannot delete the RHS. |
| 142 | // Blank out the LHS. |
| 143 | return []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit#{{ |
| 144 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: id.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Pos().(), |
| 145 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: id.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#End().(), |
| 146 | NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: []byte("_"), |
| 147 | }} |
| 148 | } |
| 149 | |
| 150 | // Precondition: curId is Ident beneath AssignStmt.Lhs. |
| 151 | // |
| 152 | // See also [deleteVarFromValueSpec], which has parallel structure. |
| 153 | func deleteVarFromAssignStmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/deleteVarFromAssignStmt().(tokFile *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#, info *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Infoscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#, curIdent inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#) []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit# { |
| 154 | var ( |
| 155 | id = curIdent.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) |
| 156 | curStmt = curIdent.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().() |
| 157 | assign = curStmt.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.AssignStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#) |
| 158 | ) |
| 159 | |
| 160 | declaresOtherNames := slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.ContainsFuncscip-go gomod github.com/golang/go/src go1.25.0 slices/ContainsFunc().(assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs., func(lhs astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#) bool { |
| 161 | lhsId, ok := lhs.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) |
| 162 | return ok && lhsId != id && lhsId.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. != "_" |
| 163 | }) |
| 164 | noRHSEffects := !slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.ContainsFuncscip-go gomod github.com/golang/go/src go1.25.0 slices/ContainsFunc().(assign.Rhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Rhs., func(rhs astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#) bool { |
| 165 | return !typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.NoEffectsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/NoEffects().(info, rhs) |
| 166 | }) |
| 167 | if !declaresOtherNames && noRHSEffects { |
| 168 | // The assignment is no longer needed, either to |
| 169 | // declare other variables, or for its side effects. |
| 170 | if edits := DeleteStmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/DeleteStmt().(tokFile, curStmt); edits != nil { |
| 171 | return edits |
| 172 | } |
| 173 | // Statement could not not be deleted in this context. |
| 174 | // Fall back to conservative deletion. |
| 175 | } |
| 176 | |
| 177 | // The assign is still needed, either for |
| 178 | // at least one LHS, or for effects on RHS, |
| 179 | // or because it cannot deleted because of its context. |
| 180 | // Blank out or delete just one LHS. |
| 181 | |
| 182 | // If the assignment is 1:1 and the RHS has no effects, |
| 183 | // we can delete the LHS and its corresponding RHS. |
| 184 | index := curIdent.ParentEdgeIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdgeIndex().() |
| 185 | if len(assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.) > 1 && |
| 186 | len(assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.) == len(assign.Rhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Rhs.) && |
| 187 | typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.NoEffectsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/NoEffects().(info, assign.Rhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Rhs.[index]) { |
| 188 | |
| 189 | if index == len(assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.)-1 { |
| 190 | // Delete final items. |
| 191 | // |
| 192 | // _, lhs1 := rhs0, rhs1 |
| 193 | // ------ ------ |
| 194 | return []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit#{ |
| 195 | { |
| 196 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.[index-1].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(), |
| 197 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.[index].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(), |
| 198 | }, |
| 199 | { |
| 200 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: assign.Rhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Rhs.[index-1].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(), |
| 201 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: assign.Rhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Rhs.[index].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(), |
| 202 | }, |
| 203 | } |
| 204 | } else { |
| 205 | // Delete non-final items. |
| 206 | // |
| 207 | // lhs0, _ := rhs0, rhs1 |
| 208 | // ------ ------ |
| 209 | return []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit#{ |
| 210 | { |
| 211 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.[index].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), |
| 212 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.[index+1].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), |
| 213 | }, |
| 214 | { |
| 215 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: assign.Rhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Rhs.[index].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), |
| 216 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: assign.Rhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Rhs.[index+1].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), |
| 217 | }, |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // We cannot delete the RHS. |
| 223 | // Blank out the LHS. |
| 224 | edits := []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit#{{ |
| 225 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: id.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Pos().(), |
| 226 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: id.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#End().(), |
| 227 | NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: []byte("_"), |
| 228 | }} |
| 229 | |
| 230 | // If this eliminates the final variable declared by |
| 231 | // an := statement, we need to turn it into an = |
| 232 | // assignment to avoid a "no new variables on left |
| 233 | // side of :=" error. |
| 234 | if !declaresOtherNames { |
| 235 | edits = append(edits, Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit#{ |
| 236 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: assign.TokPosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#TokPos., |
| 237 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: assign.TokPosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#TokPos. + 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#(len(":=")), |
| 238 | NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: []byte("="), |
| 239 | }) |
| 240 | } |
| 241 | |
| 242 | return edits |
| 243 | } |
| 244 | |
| 245 | // DeleteSpec returns edits to delete the {Type,Value}Spec identified by curSpec. |
| 246 | // |
| 247 | // TODO(adonovan): add test suite. Test for consts as well. |
| 248 | func DeleteSpecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/DeleteSpec().(tokFile *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#, curSpec inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#) []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit# { |
| 249 | var ( |
| 250 | spec = curSpec.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Specscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Spec#) |
| 251 | curDecl = curSpec.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().() |
| 252 | decl = curDecl.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.GenDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#) |
| 253 | ) |
| 254 | |
| 255 | // If it is the sole spec in the decl, |
| 256 | // delete the entire decl. |
| 257 | if len(decl.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.) == 1 { |
| 258 | return DeleteDeclscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/DeleteDecl().(tokFile, curDecl) |
| 259 | } |
| 260 | |
| 261 | // Delete the spec and its comments. |
| 262 | index := curSpec.ParentEdgeIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdgeIndex().() // index of ValueSpec within GenDecl.Specs |
| 263 | pos, end := spec.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), spec.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().() |
| 264 | if doc := astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.DocCommentscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/DocComment().(spec); doc != nil { |
| 265 | pos = doc.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Pos().() // leading comment |
| 266 | } |
| 267 | if index == len(decl.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.)-1 { |
| 268 | // Delete final spec. |
| 269 | if c := eolCommentscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/eolComment().(spec); c != nil { |
| 270 | // var (v int // comment \n) |
| 271 | end = c.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#End().() |
| 272 | } |
| 273 | } else { |
| 274 | // Delete non-final spec. |
| 275 | // var ( a T; b T ) |
| 276 | // ----- |
| 277 | end = decl.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.[index+1].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().() |
| 278 | } |
| 279 | return []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit#{{ |
| 280 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: pos, |
| 281 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: end, |
| 282 | }} |
| 283 | } |
| 284 | |
| 285 | // DeleteDecl returns edits to delete the ast.Decl identified by curDecl. |
| 286 | // |
| 287 | // TODO(adonovan): add test suite. |
| 288 | func DeleteDeclscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/DeleteDecl().(tokFile *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#, curDecl inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#) []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit# { |
| 289 | decl := curDecl.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Declscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Decl#) |
| 290 | |
| 291 | ek := curDecl.ParentEdgeKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdgeKind().() |
| 292 | switch ek { |
| 293 | case edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.DeclStmt_Declscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/DeclStmt_Decl.: |
| 294 | return DeleteStmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/DeleteStmt().(tokFile, curDecl.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().()) |
| 295 | |
| 296 | case edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.File_Declsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/File_Decls.: |
| 297 | pos, end := decl.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), decl.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().() |
| 298 | if doc := astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.DocCommentscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/DocComment().(decl); doc != nil { |
| 299 | pos = doc.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Pos().() |
| 300 | } |
| 301 | |
| 302 | // Delete free-floating comments on same line as rparen. |
| 303 | // var (...) // comment |
| 304 | var ( |
| 305 | file = curDecl.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().().Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#) |
| 306 | lineOf = tokFile.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Line(). |
| 307 | declEndLine = lineOf(decl.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().()) |
| 308 | ) |
| 309 | for _, cg := range file.Commentsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Comments. { |
| 310 | for _, c := range cg.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#List. { |
| 311 | if c.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#Pos().() < end { |
| 312 | continue // too early |
| 313 | } |
| 314 | commentEndLine := lineOf(c.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#End().()) |
| 315 | if commentEndLine > declEndLine { |
| 316 | break // too late |
| 317 | } else if lineOf(c.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#Pos().()) == declEndLine && commentEndLine == declEndLine { |
| 318 | end = c.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#End().() |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit#{{ |
| 324 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: pos, |
| 325 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: end, |
| 326 | }} |
| 327 | |
| 328 | default: |
| 329 | panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("Decl parent is %v, want DeclStmt or File", ek)) |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | // find leftmost Pos bigger than start and rightmost less than end |
| 334 | func filterPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/filterPos().(nds []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Commentscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#, start, end 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#) (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#, 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#, bool) { |
| 335 | l, r := end, tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NoPosscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NoPos. |
| 336 | ok := false |
| 337 | for _, n := range nds { |
| 338 | if n.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#Pos().() > start && n.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#Pos().() < l { |
| 339 | l = n.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#Pos().() |
| 340 | ok = true |
| 341 | } |
| 342 | if n.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#End().() <= end && n.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#End().() > r { |
| 343 | r = n.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#End().() |
| 344 | ok = true |
| 345 | } |
| 346 | } |
| 347 | return l, r, ok |
| 348 | } |
| 349 | |
| 350 | // DeleteStmt returns the edits to remove the [ast.Stmt] identified by |
| 351 | // curStmt if it recognizes the context. It returns nil otherwise. |
| 352 | // TODO(pjw, adonovan): it should not return nil, it should return an error |
| 353 | // |
| 354 | // DeleteStmt is called with just the AST so it has trouble deciding if |
| 355 | // a comment is associated with the statement to be deleted. For instance, |
| 356 | // |
| 357 | // for /*A*/ init()/*B*/;/*C/cond()/*D/;/*E*/post() /*F*/ { /*G*/} |
| 358 | // |
| 359 | // comment B and C are indistinguishable, as are D and E. That is, as the |
| 360 | // AST does not say where the semicolons are, B and C could go either |
| 361 | // with the init() or the cond(), so cannot be removed safely. The same |
| 362 | // is true for D, E, and the post(). (And there are other similar cases.) |
| 363 | // But the other comments can be removed as they are unambiguously |
| 364 | // associated with the statement being deleted. In particular, |
| 365 | // it removes whole lines like |
| 366 | // |
| 367 | // stmt // comment |
| 368 | func DeleteStmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/DeleteStmt().(file *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#, curStmt inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#) []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit# { |
| 369 | // if the stmt is on a line by itself, or a range of lines, delete the whole thing |
| 370 | // including comments. Except for the heads of switches, type |
| 371 | // switches, and for-statements that's the usual case. Complexity occurs where |
| 372 | // there are multiple statements on the same line, and adjacent comments. |
| 373 | |
| 374 | // In that case we remove some adjacent comments: |
| 375 | // In me()/*A*/;b(), comment A cannot be removed, because the ast |
| 376 | // is indistinguishable from me();/*A*/b() |
| 377 | // and the same for cases like switch me()/*A*/; x.(type) { |
| 378 | |
| 379 | // this would be more precise with the file contents, or if the ast |
| 380 | // contained the location of semicolons |
| 381 | var ( |
| 382 | stmt = curStmt.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Stmt#) |
| 383 | tokFile = file |
| 384 | lineOf = tokFile.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Line(). |
| 385 | stmtStartLine = lineOf(stmt.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().()) |
| 386 | stmtEndLine = lineOf(stmt.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().()) |
| 387 | |
| 388 | leftSyntax, rightSyntax 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# // pieces of parent node on stmt{Start,End}Line |
| 389 | leftComments, rightComments []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Commentscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment# // comments before/after stmt on the same line |
| 390 | ) |
| 391 | |
| 392 | // remember the Pos that are on the same line as stmt |
| 393 | use := func(left, right 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#) { |
| 394 | if lineOf(left) == stmtStartLine { |
| 395 | leftSyntax = left |
| 396 | } |
| 397 | if lineOf(right) == stmtEndLine { |
| 398 | rightSyntax = right |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | // find the comments, if any, on the same line |
| 403 | Big: |
| 404 | for _, cg := range astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.EnclosingFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/EnclosingFile().(curStmt).Commentsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Comments. { |
| 405 | for _, co := range cg.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#List. { |
| 406 | if lineOf(co.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#End().()) < stmtStartLine { |
| 407 | continue |
| 408 | } else if lineOf(co.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#Pos().()) > stmtEndLine { |
| 409 | break Big // no more are possible |
| 410 | } |
| 411 | if lineOf(co.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#End().()) == stmtStartLine && co.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#End().() <= stmt.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().() { |
| 412 | // comment is before the statement |
| 413 | leftComments = append(leftComments, co) |
| 414 | } else if lineOf(co.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#Pos().()) == stmtEndLine && co.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#Pos().() >= stmt.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().() { |
| 415 | // comment is after the statement |
| 416 | rightComments = append(rightComments, co) |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | // find any other syntax on the same line |
| 422 | var ( |
| 423 | leftStmt, rightStmt 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# // end/start positions of sibling statements in a []Stmt list |
| 424 | inStmtList = false |
| 425 | curParent = curStmt.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().() |
| 426 | ) |
| 427 | switch parent := curParent.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(type) { |
| 428 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BlockStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#: |
| 429 | use(parent.Lbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#Lbrace., parent.Rbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#Rbrace.) |
| 430 | inStmtList = true |
| 431 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CaseClausescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CaseClause#: |
| 432 | use(parent.Colonscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CaseClause#Colon., curStmt.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().().Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().().Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BlockStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#).Rbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#Rbrace.) |
| 433 | inStmtList = true |
| 434 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CommClausescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommClause#: |
| 435 | if parent.Commscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommClause#Comm. == stmt { |
| 436 | return nil // maybe the user meant to remove the entire CommClause? |
| 437 | } |
| 438 | use(parent.Colonscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommClause#Colon., curStmt.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().().Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().().Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BlockStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#).Rbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#Rbrace.) |
| 439 | inStmtList = true |
| 440 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ForStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#: |
| 441 | use(parent.Forscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#For., parent.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Body..Lbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#Lbrace.) |
| 442 | // special handling, as init;cond;post BlockStmt is not a statment list |
| 443 | if parent.Initscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Init. != nil && parent.Condscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Cond. != nil && stmt == parent.Initscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Init. && lineOf(parent.Condscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Cond..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().()) == lineOf(stmt.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().()) { |
| 444 | rightStmt = parent.Condscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Cond..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().() |
| 445 | } else if parent.Postscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Post. != nil && parent.Condscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Cond. != nil && stmt == parent.Postscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Post. && lineOf(parent.Condscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Cond..Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().()) == lineOf(stmt.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().()) { |
| 446 | leftStmt = parent.Condscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Cond..Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().() |
| 447 | } |
| 448 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.IfStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#: |
| 449 | switch stmt { |
| 450 | case parent.Initscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Init.: |
| 451 | use(parent.Ifscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#If., parent.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Body..Lbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#Lbrace.) |
| 452 | case parent.Elsescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Else.: |
| 453 | // stmt is the {...} in "if cond {} else {...}" and removing |
| 454 | // it would require removing the 'else' keyword, but the ast |
| 455 | // does not contain its position. |
| 456 | return nil |
| 457 | } |
| 458 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SwitchStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SwitchStmt#: |
| 459 | use(parent.Switchscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SwitchStmt#Switch., parent.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SwitchStmt#Body..Lbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#Lbrace.) |
| 460 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.TypeSwitchStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSwitchStmt#: |
| 461 | if stmt == parent.Assignscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSwitchStmt#Assign. { |
| 462 | return nil // don't remove .(type) |
| 463 | } |
| 464 | use(parent.Switchscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSwitchStmt#Switch., parent.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSwitchStmt#Body..Lbracescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#Lbrace.) |
| 465 | default: |
| 466 | return nil // not one of ours |
| 467 | } |
| 468 | |
| 469 | if inStmtList { |
| 470 | // find the siblings, if any, on the same line |
| 471 | if prev, found := curStmt.PrevSiblingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#PrevSibling().(); found && lineOf(prev.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().()) == stmtStartLine { |
| 472 | if _, ok := prev.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Stmt#); ok { |
| 473 | leftStmt = prev.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().() // preceding statement ends on same line |
| 474 | } |
| 475 | } |
| 476 | if next, found := curStmt.NextSiblingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#NextSibling().(); found && lineOf(next.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().()) == stmtEndLine { |
| 477 | rightStmt = next.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().() // following statement begins on same line |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | // compute the left and right limits of the edit |
| 482 | var leftEdit, rightEdit 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# |
| 483 | if leftStmt.IsValidscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#IsValid().() { |
| 484 | leftEdit = stmt.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().() // can't remove preceding comments: a()/*A*/; me() |
| 485 | } else if leftSyntax.IsValidscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#IsValid().() { |
| 486 | // remove intervening leftComments |
| 487 | if a, _, ok := filterPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/filterPos().(leftComments, leftSyntax, stmt.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().()); ok { |
| 488 | leftEdit = a |
| 489 | } else { |
| 490 | leftEdit = stmt.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().() |
| 491 | } |
| 492 | } else { // remove whole line |
| 493 | for leftEdit = stmt.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(); lineOf(leftEdit) == stmtStartLine; leftEdit-- { |
| 494 | } |
| 495 | if leftEdit < stmt.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().() { |
| 496 | leftEdit++ // beginning of line |
| 497 | } |
| 498 | } |
| 499 | if rightStmt.IsValidscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#IsValid().() { |
| 500 | rightEdit = stmt.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().() // can't remove following comments |
| 501 | } else if rightSyntax.IsValidscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#IsValid().() { |
| 502 | // remove intervening rightComments |
| 503 | if _, b, ok := filterPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/filterPos().(rightComments, stmt.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(), rightSyntax); ok { |
| 504 | rightEdit = b |
| 505 | } else { |
| 506 | rightEdit = stmt.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().() |
| 507 | } |
| 508 | } else { // remove whole line |
| 509 | fend := 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().()) + 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.Sizescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Size().()) |
| 510 | for rightEdit = stmt.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(); fend >= rightEdit && lineOf(rightEdit) == stmtEndLine; rightEdit++ { |
| 511 | } |
| 512 | // don't remove \n if there was other stuff earlier |
| 513 | if leftSyntax.IsValidscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#IsValid().() || leftStmt.IsValidscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#IsValid().() { |
| 514 | rightEdit-- |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | return []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit#{{Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: leftEdit, Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: rightEdit}} |
| 519 | } |
| 520 | |
| 521 | // DeleteUnusedVars computes the edits required to delete the |
| 522 | // declarations of any local variables whose last uses are in the |
| 523 | // curDelend subtree, which is about to be deleted. |
| 524 | func DeleteUnusedVarsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/DeleteUnusedVars().(index *typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#, info *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Infoscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#, tokFile *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#, curDelend inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#) []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit# { |
| 525 | // TODO(adonovan): we might want to generalize this by |
| 526 | // splitting the two phases below, so that we can gather |
| 527 | // across a whole sequence of deletions then finally compute the |
| 528 | // set of variables that are no longer wanted. |
| 529 | |
| 530 | // Count number of deletions of each var. |
| 531 | delcount := make(map[*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Varscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var#]int) |
| 532 | for curId := range curDelend.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().((*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#)(nil)) { |
| 533 | id := curId.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) |
| 534 | if v, ok := info.Usesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Uses.[id].(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Varscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var#); ok && |
| 535 | typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.GetVarKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/GetVarKind().(v) == typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.LocalVarscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/LocalVar. { // always false before go1.25 |
| 536 | delcount[v]++ |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | // Delete declaration of each var that became unused. |
| 541 | var edits []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit# |
| 542 | for v, count := range delcount { |
| 543 | if len(slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Collectscip-go gomod github.com/golang/go/src go1.25.0 slices/Collect().(index.Usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Uses().(v))) == count { |
| 544 | if curDefId, ok := index.Defscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Def().(v); ok { |
| 545 | edits = append(edits, DeleteVarscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/DeleteVar().(tokFile, info, curDefId)...) |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | return edits |
| 550 | } |
| 551 | |
| 552 | func eolCommentscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/eolComment().(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CommentGroupscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup# { |
| 553 | // TODO(adonovan): support: |
| 554 | // func f() {...} // comment |
| 555 | switch n := n.(type) { |
| 556 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.GenDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#: |
| 557 | if !n.TokPosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#TokPos..IsValidscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#IsValid().() && len(n.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.) == 1 { |
| 558 | return eolCommentscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/eolComment().(n.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.[0]) |
| 559 | } |
| 560 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ValueSpecscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#: |
| 561 | return n.Commentscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Comment. |
| 562 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.TypeSpecscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#: |
| 563 | return n.Commentscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#Comment. |
| 564 | } |
| 565 | return nil |
| 566 | } |