internal/lsp: avoid possible nil pointer in references/rename
Noticed this in https://github.com/fatih/vim-go/issues/2786. I don't
think that this will fix the problem in this issue, but we should avoid
nil pointers as much as possible. Also, remove a bit of extra whitespace
so that the style closer matches that of the rest of the project.
Change-Id: I94b924ea14e4a296382e3e68c52eeb43f6cd86a6
Reviewed-on: https://go-review.googlesource.com/c/tools/+/225523
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
diff --git a/internal/lsp/source/implementation.go b/internal/lsp/source/implementation.go
index 1257048..3c8c249 100644
--- a/internal/lsp/source/implementation.go
+++ b/internal/lsp/source/implementation.go
@@ -204,31 +204,26 @@
// qualifiedObjsAtProtocolPos returns info for all the type.Objects
// referenced at the given position. An object will be returned for
// every package that the file belongs to.
-func qualifiedObjsAtProtocolPos(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Position) ([]qualifiedObject, error) {
- phs, err := s.PackageHandles(ctx, f)
+func qualifiedObjsAtProtocolPos(ctx context.Context, s Snapshot, fh FileHandle, pp protocol.Position) ([]qualifiedObject, error) {
+ phs, err := s.PackageHandles(ctx, fh)
if err != nil {
return nil, err
}
-
- var qualifiedObjs []qualifiedObject
-
// Check all the packages that the file belongs to.
+ var qualifiedObjs []qualifiedObject
for _, ph := range phs {
pkg, err := ph.Check(ctx)
if err != nil {
return nil, err
}
-
- astFile, pos, err := getASTFile(pkg, f, pp)
+ astFile, pos, err := getASTFile(pkg, fh, pp)
if err != nil {
return nil, err
}
-
path := pathEnclosingObjNode(astFile, pos)
if path == nil {
return nil, ErrNoIdentFound
}
-
var objs []types.Object
switch leaf := path[0].(type) {
case *ast.Ident:
@@ -252,13 +247,11 @@
}
objs = append(objs, obj)
}
-
pkgs := make(map[*types.Package]Package)
pkgs[pkg.GetTypes()] = pkg
for _, imp := range pkg.Imports() {
pkgs[imp.GetTypes()] = imp
}
-
for _, obj := range objs {
qualifiedObjs = append(qualifiedObjs, qualifiedObject{
obj: obj,
@@ -268,7 +261,11 @@
})
}
}
-
+ // Return an error if no objects were found since callers will assume that
+ // the slice has at least 1 element.
+ if len(qualifiedObjs) == 0 {
+ return nil, errors.Errorf("no object found")
+ }
return qualifiedObjs, nil
}
@@ -277,22 +274,18 @@
if err != nil {
return nil, 0, err
}
-
file, _, m, _, err := pgh.Cached()
if err != nil {
return nil, 0, err
}
-
spn, err := m.PointSpan(pos)
if err != nil {
return nil, 0, err
}
-
rng, err := spn.Range(m.Converter)
if err != nil {
return nil, 0, err
}
-
return file, rng.Start, nil
}