From 932b62a02f87862730a919fcb6f627010bfdecd9 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 12 Nov 2014 15:39:43 -0800 Subject: [PATCH] go/types: add missing nil check in Info.TypeOf Also use type assertions in a more defensive way (check for != nil rather than ok). LGTM=dsymonds, adonovan R=adonovan, dsymonds CC=golang-codereviews https://golang.org/cl/169480043 --- go/types/api.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/go/types/api.go b/go/types/api.go index 908b0f0eda..bfdcccb0d9 100644 --- a/go/types/api.go +++ b/go/types/api.go @@ -215,8 +215,10 @@ func (info *Info) TypeOf(e ast.Expr) Type { if t, ok := info.Types[e]; ok { return t.Type } - if id, ok := e.(*ast.Ident); ok { - return info.ObjectOf(id).Type() + if id, _ := e.(*ast.Ident); id != nil { + if obj := info.ObjectOf(id); obj != nil { + return obj.Type() + } } return nil } @@ -230,7 +232,7 @@ func (info *Info) TypeOf(e ast.Expr) Type { // Precondition: the Uses and Defs maps are populated. // func (info *Info) ObjectOf(id *ast.Ident) Object { - if obj, ok := info.Defs[id]; ok { + if obj, _ := info.Defs[id]; obj != nil { return obj } return info.Uses[id]