Skip to content

x/tools/go/ast/inspector: add (*Cursor).Valid() bool method #76361

@adonovan

Description

@adonovan

Background: When using inspector.Cursor, It is often necessary to check whether a cursor is valid. This can be done by comparing it to the zero value, or by testing whether its Inspector field is nil:

if cur != (Cursor{}) { use(cur) }
if cur.Inspector() != nil { use(cur) }

However, both are rather roundabout, and the second is unclear.

Proposal: we propose to add a Valid method that reports this property directly:

package inspector // golang.org/x/tools/go/ast/inspector

type Cursor ...

// Valid reports whether the cursor is valid.
func (Cursor) Valid() bool 

Metadata

Metadata

Assignees

Labels

LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposalProposal-AcceptedToolsThis label describes issues relating to any tools in the x/tools repository.

Type

No type

Projects

Status

Accepted

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions