Skip to content

Commit 52ff224

Browse files
committed
internal/lsp: avoid possible nil pointer in references/rename
Noticed this in fatih/vim-go#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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent c994279 commit 52ff224

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

internal/lsp/source/implementation.go

+9-16
Original file line numberDiff line numberDiff line change
@@ -204,31 +204,26 @@ type qualifiedObject struct {
204204
// qualifiedObjsAtProtocolPos returns info for all the type.Objects
205205
// referenced at the given position. An object will be returned for
206206
// every package that the file belongs to.
207-
func qualifiedObjsAtProtocolPos(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Position) ([]qualifiedObject, error) {
208-
phs, err := s.PackageHandles(ctx, f)
207+
func qualifiedObjsAtProtocolPos(ctx context.Context, s Snapshot, fh FileHandle, pp protocol.Position) ([]qualifiedObject, error) {
208+
phs, err := s.PackageHandles(ctx, fh)
209209
if err != nil {
210210
return nil, err
211211
}
212-
213-
var qualifiedObjs []qualifiedObject
214-
215212
// Check all the packages that the file belongs to.
213+
var qualifiedObjs []qualifiedObject
216214
for _, ph := range phs {
217215
pkg, err := ph.Check(ctx)
218216
if err != nil {
219217
return nil, err
220218
}
221-
222-
astFile, pos, err := getASTFile(pkg, f, pp)
219+
astFile, pos, err := getASTFile(pkg, fh, pp)
223220
if err != nil {
224221
return nil, err
225222
}
226-
227223
path := pathEnclosingObjNode(astFile, pos)
228224
if path == nil {
229225
return nil, ErrNoIdentFound
230226
}
231-
232227
var objs []types.Object
233228
switch leaf := path[0].(type) {
234229
case *ast.Ident:
@@ -252,13 +247,11 @@ func qualifiedObjsAtProtocolPos(ctx context.Context, s Snapshot, f FileHandle, p
252247
}
253248
objs = append(objs, obj)
254249
}
255-
256250
pkgs := make(map[*types.Package]Package)
257251
pkgs[pkg.GetTypes()] = pkg
258252
for _, imp := range pkg.Imports() {
259253
pkgs[imp.GetTypes()] = imp
260254
}
261-
262255
for _, obj := range objs {
263256
qualifiedObjs = append(qualifiedObjs, qualifiedObject{
264257
obj: obj,
@@ -268,7 +261,11 @@ func qualifiedObjsAtProtocolPos(ctx context.Context, s Snapshot, f FileHandle, p
268261
})
269262
}
270263
}
271-
264+
// Return an error if no objects were found since callers will assume that
265+
// the slice has at least 1 element.
266+
if len(qualifiedObjs) == 0 {
267+
return nil, errors.Errorf("no object found")
268+
}
272269
return qualifiedObjs, nil
273270
}
274271

@@ -277,22 +274,18 @@ func getASTFile(pkg Package, f FileHandle, pos protocol.Position) (*ast.File, to
277274
if err != nil {
278275
return nil, 0, err
279276
}
280-
281277
file, _, m, _, err := pgh.Cached()
282278
if err != nil {
283279
return nil, 0, err
284280
}
285-
286281
spn, err := m.PointSpan(pos)
287282
if err != nil {
288283
return nil, 0, err
289284
}
290-
291285
rng, err := spn.Range(m.Converter)
292286
if err != nil {
293287
return nil, 0, err
294288
}
295-
296289
return file, rng.Start, nil
297290
}
298291

0 commit comments

Comments
 (0)