| 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 gofixdirective searches for and validates go:fix directives. The |
| 6 | // go/analysis/passes/inline package uses findgofix to perform inlining. |
| 7 | // The go/analysis/passes/gofix package uses findgofix to check for problems |
| 8 | // with go:fix directives. |
| 9 | // |
| 10 | // gofixdirective is separate from gofix to avoid depending on refactor/inline, |
| 11 | // which is large. |
| 12 | package gofixdirectivescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/ |
| 13 | |
| 14 | // This package is tested by go/analysis/passes/inline. |
| 15 | |
| 16 | import ( |
| 17 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 18 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 19 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 20 | |
| 21 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 22 | "golang.org/x/tools/go/ast/inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/" |
| 23 | internalastutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/ "golang.org/x/tools/internal/astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/" |
| 24 | ) |
| 25 | |
| 26 | // A Handler handles language entities with go:fix directives. |
| 27 | type Handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/Handler# interface { |
| 28 | HandleFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/Handler#HandleFunc.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#) |
| 29 | HandleAliasscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/Handler#HandleAlias.(*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#) |
| 30 | HandleConstscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/Handler#HandleConst.(name, rhs *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#) |
| 31 | } |
| 32 | |
| 33 | // Find finds functions and constants annotated with an appropriate "//go:fix" |
| 34 | // comment (the syntax proposed by #32816), and calls handler methods for each one. |
| 35 | // h may be nil. |
| 36 | func Findscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/Find().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#, root 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#, h Handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/Handler#) { |
| 37 | for cur := range root.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`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#)(nil), (*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#)(nil)) { |
| 38 | switch decl := cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(type) { |
| 39 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#: |
| 40 | findFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/findFunc().(decl, h) |
| 41 | |
| 42 | 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#: |
| 43 | if decl.Tokscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Tok. != tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.CONSTscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/CONST. && decl.Tokscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Tok. != tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.TYPEscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/TYPE. { |
| 44 | continue |
| 45 | } |
| 46 | declInline := hasFixInlinescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/hasFixInline().(decl.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Doc.) |
| 47 | // Accept inline directives on the entire decl as well as individual specs. |
| 48 | for _, spec := range decl.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs. { |
| 49 | switch spec := spec.(type) { |
| 50 | 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#: // Tok == TYPE |
| 51 | findAliasscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/findAlias().(pass, spec, declInline, h) |
| 52 | |
| 53 | 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#: // Tok == CONST |
| 54 | findConstscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/findConst().(pass, spec, declInline, h) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func findFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/findFunc().(decl *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#, h Handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/Handler#) { |
| 62 | if !hasFixInlinescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/hasFixInline().(decl.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Doc.) { |
| 63 | return |
| 64 | } |
| 65 | if h != nil { |
| 66 | h.HandleFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/Handler#HandleFunc.(decl) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func findAliasscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/findAlias().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#, spec *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#, declInline bool, h Handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/Handler#) { |
| 71 | if !declInline && !hasFixInlinescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/hasFixInline().(spec.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#Doc.) { |
| 72 | return |
| 73 | } |
| 74 | if !spec.Assignscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#Assign..IsValidscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#IsValid().() { |
| 75 | pass.Reportfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Reportf().(spec.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#Pos().(), "invalid //go:fix inline directive: not a type alias") |
| 76 | return |
| 77 | } |
| 78 | |
| 79 | // Disallow inlines of type expressions containing array types. |
| 80 | // Given an array type like [N]int where N is a named constant, go/types provides |
| 81 | // only the value of the constant as an int64. So inlining A in this code: |
| 82 | // |
| 83 | // const N = 5 |
| 84 | // type A = [N]int |
| 85 | // |
| 86 | // would result in [5]int, breaking the connection with N. |
| 87 | for n := range astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Preorderscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Preorder().(spec.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#Type.) { |
| 88 | if ar, ok := n.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ArrayTypescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ArrayType#); ok && ar.Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ArrayType#Len. != nil { |
| 89 | // Make an exception when the array length is a literal int. |
| 90 | if lit, ok := astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Unparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Unparen().(ar.Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ArrayType#Len.).(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BasicLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#); ok && lit.Kindscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Kind. == tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.INTscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/INT. { |
| 91 | continue |
| 92 | } |
| 93 | pass.Reportfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Reportf().(spec.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#Pos().(), "invalid //go:fix inline directive: array types not supported") |
| 94 | return |
| 95 | } |
| 96 | } |
| 97 | if h != nil { |
| 98 | h.HandleAliasscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/Handler#HandleAlias.(spec) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func findConstscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/findConst().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#, spec *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#, declInline bool, h Handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/Handler#) { |
| 103 | specInline := hasFixInlinescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/hasFixInline().(spec.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Doc.) |
| 104 | if declInline || specInline { |
| 105 | for i, nameIdent := range spec.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Names. { |
| 106 | if i >= len(spec.Valuesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Values.) { |
| 107 | // Possible following an iota. |
| 108 | break |
| 109 | } |
| 110 | var rhsIdent *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# |
| 111 | switch val := spec.Valuesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Values.[i].(type) { |
| 112 | case *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#: |
| 113 | // Constants defined with the predeclared iota cannot be inlined. |
| 114 | if pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo..Usesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Uses.[val] == builtinIotascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/builtinIota. { |
| 115 | pass.Reportfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Reportf().(val.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Pos().(), "invalid //go:fix inline directive: const value is iota") |
| 116 | return |
| 117 | } |
| 118 | rhsIdent = val |
| 119 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#: |
| 120 | rhsIdent = val.Selscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#Sel. |
| 121 | default: |
| 122 | pass.Reportfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Reportf().(val.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), "invalid //go:fix inline directive: const value is not the name of another constant") |
| 123 | return |
| 124 | } |
| 125 | if h != nil { |
| 126 | h.HandleConstscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/Handler#HandleConst.(nameIdent, rhsIdent) |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // hasFixInline reports the presence of a "//go:fix inline" directive |
| 133 | // in the comments. |
| 134 | func hasFixInlinescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/hasFixInline().(cg *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#) bool { |
| 135 | for _, d := range internalastutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.Directivesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Directives().(cg) { |
| 136 | if d.Toolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Directive#Tool. == "go" && d.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Directive#Name. == "fix" && d.Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Directive#Args. == "inline" { |
| 137 | return true |
| 138 | } |
| 139 | } |
| 140 | return false |
| 141 | } |
| 142 | |
| 143 | var builtinIotascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/internal/gofixdirective`/builtinIota. = typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Universescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Universe..Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Lookup().("iota") |