1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package undeclaredname
import (
"bytes"
_ "embed"
"fmt"
"go/ast"
"go/format"
"go/token"
"go/types"
"strings"
"unicode"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/gopls/internal/util/safetoken"
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/analysisinternal"
)
//go:embed doc.go
var doc string
var Analyzer = &analysis.Analyzer{
Name: "undeclaredname",
Doc: analysisinternal.MustExtractDoc(doc, "undeclaredname"),
Requires: []*analysis.Analyzer{},
Run: run,
RunDespiteErrors: true,
URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/undeclaredname",
}
// The prefix for this error message changed in Go 1.20.
var undeclaredNamePrefixes = []string{"undeclared name: ", "undefined: "}
func run(pass *analysis.Pass) (interface{}, error) {
for _, err := range pass.TypeErrors {
runForError(pass, err)
}
return nil, nil
}
func runForError(pass *analysis.Pass, err types.Error) {
// Extract symbol name from error.
var name string
for _, prefix := range undeclaredNamePrefixes {
if !strings.HasPrefix(err.Msg, prefix) {
continue
}
name = strings.TrimPrefix(err.Msg, prefix)
}
if name == "" {
return
}
// Find file enclosing error.
var file *ast.File
for _, f := range pass.Files {
if f.Pos() <= err.Pos && err.Pos < f.End() {
file = f
break
}
}
if file == nil {
return
}
// Find path to identifier in the error.
path, _ := astutil.PathEnclosingInterval(file, err.Pos, err.Pos)
if len(path) < 2 {
return
}
ident, ok := path[0].(*ast.Ident)
if !ok || ident.Name != name {
return
}
// Skip selector expressions because it might be too complex
// to try and provide a suggested fix for fields and methods.
if _, ok := path[1].(*ast.SelectorExpr); ok {
return
}
// Undeclared quick fixes only work in function bodies.
inFunc := false
for i := range path {
if _, inFunc = path[i].(*ast.FuncDecl); inFunc {
if i == 0 {
return
}
if _, isBody := path[i-1].(*ast.BlockStmt); !isBody {
return
}
break
}
}
if !inFunc {
return
}
// Offer a fix.
noun := "variable"
if isCallPosition(path) {
noun = "function"
}
pass.Report(analysis.Diagnostic{
Pos: err.Pos,
End: err.Pos + token.Pos(len(name)),
Message: err.Msg,
Category: FixCategory,
SuggestedFixes: []analysis.SuggestedFix{{
Message: fmt.Sprintf("Create %s %q", noun, name),
// No TextEdits => computed by a gopls command
}},
})
}
const FixCategory = "undeclaredname" // recognized by gopls ApplyFix
// SuggestedFix computes the edits for the lazy (no-edits) fix suggested by the analyzer.
func SuggestedFix(fset *token.FileSet, start, end token.Pos, content []byte, file *ast.File, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) {
pos := start // don't use the end
path, _ := astutil.PathEnclosingInterval(file, pos, pos)
if len(path) < 2 {
return nil, nil, fmt.Errorf("no expression found")
}
ident, ok := path[0].(*ast.Ident)
if !ok {
return nil, nil, fmt.Errorf("no identifier found")
}
// Check for a possible call expression, in which case we should add a
// new function declaration.
if isCallPosition(path) {
return newFunctionDeclaration(path, file, pkg, info, fset)
}
// Get the place to insert the new statement.
insertBeforeStmt := analysisinternal.StmtToInsertVarBefore(path)
if insertBeforeStmt == nil {
return nil, nil, fmt.Errorf("could not locate insertion point")
}
insertBefore := safetoken.StartPosition(fset, insertBeforeStmt.Pos()).Offset
// Get the indent to add on the line after the new statement.
// Since this will have a parse error, we can not use format.Source().
contentBeforeStmt, indent := content[:insertBefore], "\n"
if nl := bytes.LastIndex(contentBeforeStmt, []byte("\n")); nl != -1 {
indent = string(contentBeforeStmt[nl:])
}
// Create the new local variable statement.
newStmt := fmt.Sprintf("%s := %s", ident.Name, indent)
return fset, &analysis.SuggestedFix{
Message: fmt.Sprintf("Create variable %q", ident.Name),
TextEdits: []analysis.TextEdit{{
Pos: insertBeforeStmt.Pos(),
End: insertBeforeStmt.Pos(),
NewText: []byte(newStmt),
}},
}, nil
}
func newFunctionDeclaration(path []ast.Node, file *ast.File, pkg *types.Package, info *types.Info, fset *token.FileSet) (*token.FileSet, *analysis.SuggestedFix, error) {
if len(path) < 3 {
return nil, nil, fmt.Errorf("unexpected set of enclosing nodes: %v", path)
}
ident, ok := path[0].(*ast.Ident)
if !ok {
return nil, nil, fmt.Errorf("no name for function declaration %v (%T)", path[0], path[0])
}
call, ok := path[1].(*ast.CallExpr)
if !ok {
return nil, nil, fmt.Errorf("no call expression found %v (%T)", path[1], path[1])
}
// Find the enclosing function, so that we can add the new declaration
// below.
var enclosing *ast.FuncDecl
for _, n := range path {
if n, ok := n.(*ast.FuncDecl); ok {
enclosing = n
break
}
}
// TODO(rstambler): Support the situation when there is no enclosing
// function.
if enclosing == nil {
return nil, nil, fmt.Errorf("no enclosing function found: %v", path)
}
pos := enclosing.End()
var paramNames []string
var paramTypes []types.Type
// keep track of all param names to later ensure uniqueness
nameCounts := map[string]int{}
for _, arg := range call.Args {
typ := info.TypeOf(arg)
if typ == nil {
return nil, nil, fmt.Errorf("unable to determine type for %s", arg)
}
switch t := typ.(type) {
// this is the case where another function call returning multiple
// results is used as an argument
case *types.Tuple:
n := t.Len()
for i := 0; i < n; i++ {
name := typeToArgName(t.At(i).Type())
nameCounts[name]++
paramNames = append(paramNames, name)
paramTypes = append(paramTypes, types.Default(t.At(i).Type()))
}
default:
// does the argument have a name we can reuse?
// only happens in case of a *ast.Ident
var name string
if ident, ok := arg.(*ast.Ident); ok {
name = ident.Name
}
if name == "" {
name = typeToArgName(typ)
}
nameCounts[name]++
paramNames = append(paramNames, name)
paramTypes = append(paramTypes, types.Default(typ))
}
}
for n, c := range nameCounts {
// Any names we saw more than once will need a unique suffix added
// on. Reset the count to 1 to act as the suffix for the first
// occurrence of that name.
if c >= 2 {
nameCounts[n] = 1
} else {
delete(nameCounts, n)
}
}
params := &ast.FieldList{}
for i, name := range paramNames {
if suffix, repeats := nameCounts[name]; repeats {
nameCounts[name]++
name = fmt.Sprintf("%s%d", name, suffix)
}
// only worth checking after previous param in the list
if i > 0 {
// if type of parameter at hand is the same as the previous one,
// add it to the previous param list of identifiers so to have:
// (s1, s2 string)
// and not
// (s1 string, s2 string)
if paramTypes[i] == paramTypes[i-1] {
params.List[len(params.List)-1].Names = append(params.List[len(params.List)-1].Names, ast.NewIdent(name))
continue
}
}
params.List = append(params.List, &ast.Field{
Names: []*ast.Ident{
ast.NewIdent(name),
},
Type: analysisinternal.TypeExpr(file, pkg, paramTypes[i]),
})
}
decl := &ast.FuncDecl{
Name: ast.NewIdent(ident.Name),
Type: &ast.FuncType{
Params: params,
// TODO(golang/go#47558): Also handle result
// parameters here based on context of CallExpr.
},
Body: &ast.BlockStmt{
List: []ast.Stmt{
&ast.ExprStmt{
X: &ast.CallExpr{
Fun: ast.NewIdent("panic"),
Args: []ast.Expr{
&ast.BasicLit{
Value: `"unimplemented"`,
},
},
},
},
},
},
}
b := bytes.NewBufferString("\n\n")
if err := format.Node(b, fset, decl); err != nil {
return nil, nil, err
}
return fset, &analysis.SuggestedFix{
Message: fmt.Sprintf("Create function %q", ident.Name),
TextEdits: []analysis.TextEdit{{
Pos: pos,
End: pos,
NewText: b.Bytes(),
}},
}, nil
}
func typeToArgName(ty types.Type) string {
s := types.Default(ty).String()
switch t := aliases.Unalias(ty).(type) {
case *types.Basic:
// use first letter in type name for basic types
return s[0:1]
case *types.Slice:
// use element type to decide var name for slices
return typeToArgName(t.Elem())
case *types.Array:
// use element type to decide var name for arrays
return typeToArgName(t.Elem())
case *types.Chan:
return "ch"
}
s = strings.TrimFunc(s, func(r rune) bool {
return !unicode.IsLetter(r)
})
if s == "error" {
return "err"
}
// remove package (if present)
// and make first letter lowercase
a := []rune(s[strings.LastIndexByte(s, '.')+1:])
a[0] = unicode.ToLower(a[0])
return string(a)
}
// isCallPosition reports whether the path denotes the subtree in call position, f().
func isCallPosition(path []ast.Node) bool {
return len(path) > 1 &&
is[*ast.CallExpr](path[1]) &&
path[1].(*ast.CallExpr).Fun == path[0]
}
func is[T any](x any) bool {
_, ok := x.(T)
return ok
}
|