Skip to content

Commit 2182926

Browse files
committed
go/ast/inspector: add Cursor.ParentEdge{Kind,Index} methods
These methods return just one or the other component of ParentEdge, making them more convenient to use than ParentEdge in most cases, obviating astutil.IsChildOf. Also, fix some doc links, and use plain "valid" instead of [Cursor.Valid] in adjectival contexts; the meaning is clear. Fixes golang/go#77195 Change-Id: Id9521a67cda78958ed799874a38ff3f039e4a564 Reviewed-on: https://go-review.googlesource.com/c/tools/+/740280 Reviewed-by: Madeline Kalil <mkalil@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 27020ac commit 2182926

File tree

26 files changed

+70
-77
lines changed

26 files changed

+70
-77
lines changed

go/analysis/passes/inline/inline.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,13 +364,13 @@ func (a *analyzer) inlineAlias(tn *types.TypeName, curId inspector.Cursor) {
364364
// pkg.Id[T]
365365
// pkg.Id[K, V]
366366
var expr ast.Expr = id
367-
if astutil.IsChildOf(curId, edge.SelectorExpr_Sel) {
367+
if curId.ParentEdgeKind() == edge.SelectorExpr_Sel {
368368
curId = curId.Parent()
369369
expr = curId.Node().(ast.Expr)
370370
}
371371
// If expr is part of an IndexExpr or IndexListExpr, we'll need that node.
372372
// Given C[int], TypeOf(C) is generic but TypeOf(C[int]) is instantiated.
373-
switch ek, _ := curId.ParentEdge(); ek {
373+
switch curId.ParentEdgeKind() {
374374
case edge.IndexExpr_X:
375375
expr = curId.Parent().Node().(*ast.IndexExpr)
376376
case edge.IndexListExpr_X:
@@ -526,7 +526,7 @@ func (a *analyzer) inlineConst(con *types.Const, cur inspector.Cursor) {
526526
}
527527
// If n is qualified by a package identifier, we'll need the full selector expression.
528528
var expr ast.Expr = n
529-
if astutil.IsChildOf(cur, edge.SelectorExpr_Sel) {
529+
if cur.ParentEdgeKind() == edge.SelectorExpr_Sel {
530530
expr = cur.Parent().Node().(ast.Expr)
531531
}
532532
a.reportInline("constant", "Constant", expr, edits, importPrefix+incon.RHSName)

go/analysis/passes/modernize/errorsastype.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func errorsastype(pass *analysis.Pass) (any, error) {
189189
// declaration of the typed error var. The var must not be
190190
// used outside the if statement.
191191
func canUseErrorsAsType(info *types.Info, index *typeindex.Index, curCall inspector.Cursor) (_ *types.Var, _ inspector.Cursor) {
192-
if !astutil.IsChildOf(curCall, edge.IfStmt_Cond) {
192+
if curCall.ParentEdgeKind() != edge.IfStmt_Cond {
193193
return // not beneath if statement
194194
}
195195
var (
@@ -221,7 +221,7 @@ func canUseErrorsAsType(info *types.Info, index *typeindex.Index, curCall inspec
221221
return // v used before/after if statement
222222
}
223223
}
224-
if !astutil.IsChildOf(curDef, edge.ValueSpec_Names) {
224+
if curDef.ParentEdgeKind() != edge.ValueSpec_Names {
225225
return // v not declared by "var v T"
226226
}
227227
var (

go/analysis/passes/modernize/minmax.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func minmax(pass *analysis.Pass) (any, error) {
209209
// (This case would require introducing another block
210210
// if cond { ... } else { if a < b { lhs = rhs } }
211211
// and checking that there is no following "else".)
212-
if astutil.IsChildOf(curIfStmt, edge.IfStmt_Else) {
212+
if curIfStmt.ParentEdgeKind() == edge.IfStmt_Else {
213213
continue
214214
}
215215

go/analysis/passes/modernize/modernize.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"golang.org/x/tools/go/ast/edge"
2121
"golang.org/x/tools/go/ast/inspector"
2222
"golang.org/x/tools/internal/analysis/analyzerutil"
23-
"golang.org/x/tools/internal/astutil"
23+
2424
"golang.org/x/tools/internal/moreiters"
2525
"golang.org/x/tools/internal/packagepath"
2626
"golang.org/x/tools/internal/stdlib"
@@ -114,7 +114,7 @@ func within(pass *analysis.Pass, pkgs ...string) bool {
114114
// unparenEnclosing removes enclosing parens from cur in
115115
// preparation for a call to [Cursor.ParentEdge].
116116
func unparenEnclosing(cur inspector.Cursor) inspector.Cursor {
117-
for astutil.IsChildOf(cur, edge.ParenExpr_X) {
117+
for cur.ParentEdgeKind() == edge.ParenExpr_X {
118118
cur = cur.Parent()
119119
}
120120
return cur

go/analysis/passes/modernize/rangeint.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,10 @@ func isScalarLvalue(info *types.Info, curId inspector.Cursor) bool {
316316
cur := curId
317317

318318
// Strip enclosing parens.
319-
ek, _ := cur.ParentEdge()
319+
ek := cur.ParentEdgeKind()
320320
for ek == edge.ParenExpr_X {
321321
cur = cur.Parent()
322-
ek, _ = cur.ParentEdge()
322+
ek = cur.ParentEdgeKind()
323323
}
324324

325325
switch ek {

go/analysis/passes/modernize/reflect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ func reflecttypefor(pass *analysis.Pass) (any, error) {
5656

5757
// Special case for TypeOf((*T)(nil)).Elem(),
5858
// needed when T is an interface type.
59-
if astutil.IsChildOf(curCall, edge.SelectorExpr_X) {
59+
if curCall.ParentEdgeKind() == edge.SelectorExpr_X {
6060
curSel := unparenEnclosing(curCall).Parent()
61-
if astutil.IsChildOf(curSel, edge.CallExpr_Fun) {
61+
if curSel.ParentEdgeKind() == edge.CallExpr_Fun {
6262
call2 := unparenEnclosing(curSel).Parent().Node().(*ast.CallExpr)
6363
obj := typeutil.Callee(info, call2)
6464
if typesinternal.IsMethodNamed(obj, "reflect", "Type", "Elem") {

go/analysis/passes/modernize/stditerators.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,15 @@ func stditerators(pass *analysis.Pass) (any, error) {
220220
)
221221

222222
// Analyze enclosing loop.
223-
switch first(curLenCall.ParentEdge()) {
223+
switch curLenCall.ParentEdgeKind() {
224224
case edge.BinaryExpr_Y:
225225
// pattern 1: for i := 0; i < x.Len(); i++ { ... }
226226
var (
227227
curCmp = curLenCall.Parent()
228228
cmp = curCmp.Node().(*ast.BinaryExpr)
229229
)
230230
if cmp.Op != token.LSS ||
231-
!astutil.IsChildOf(curCmp, edge.ForStmt_Cond) {
231+
curCmp.ParentEdgeKind() != edge.ForStmt_Cond {
232232
continue
233233
}
234234
if id, ok := cmp.X.(*ast.Ident); ok {

go/analysis/passes/modernize/stringsbuilder.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ nextcand:
102102
continue
103103
}
104104

105-
ek, _ := def.ParentEdge()
105+
ek := def.ParentEdgeKind()
106106
if ek == edge.AssignStmt_Lhs &&
107107
len(def.Parent().Node().(*ast.AssignStmt).Lhs) == 1 {
108108
// Have: s := expr
@@ -274,10 +274,10 @@ nextcand:
274274
)
275275
for curUse := range index.Uses(v) {
276276
// Strip enclosing parens around Ident.
277-
ek, _ := curUse.ParentEdge()
277+
ek := curUse.ParentEdgeKind()
278278
for ek == edge.ParenExpr_X {
279279
curUse = curUse.Parent()
280-
ek, _ = curUse.ParentEdge()
280+
ek = curUse.ParentEdgeKind()
281281
}
282282

283283
// The rvalueUse must be the lexically last use.

go/analysis/passes/modernize/stringscut.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ func indexArgValid(info *types.Info, index *typeindex.Index, expr ast.Expr, afte
376376
// 4. afterSlice - a slice of `s` that matches one of: s[i+len(substr):], s[len(substr) + i:], s[i + const], s[k + i] (where k = len(substr))
377377
func checkIdxUses(info *types.Info, uses iter.Seq[inspector.Cursor], s, substr ast.Expr) (negative, nonnegative, beforeSlice, afterSlice []ast.Expr) {
378378
use := func(cur inspector.Cursor) bool {
379-
ek, _ := cur.ParentEdge()
379+
ek := cur.ParentEdgeKind()
380380
n := cur.Parent().Node()
381381
switch ek {
382382
case edge.BinaryExpr_X, edge.BinaryExpr_Y:
@@ -435,7 +435,7 @@ func checkIdxUses(info *types.Info, uses iter.Seq[inspector.Cursor], s, substr a
435435
// considered.
436436
func hasModifyingUses(info *types.Info, uses iter.Seq[inspector.Cursor], afterPos token.Pos) bool {
437437
for curUse := range uses {
438-
ek, _ := curUse.ParentEdge()
438+
ek := curUse.ParentEdgeKind()
439439
if ek == edge.AssignStmt_Lhs {
440440
if curUse.Node().Pos() <= afterPos {
441441
continue

go/analysis/passes/modernize/unsafefuncs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func unsafefuncs(pass *analysis.Pass) (any, error) {
8888
if sum, ok := curSum.Node().(*ast.BinaryExpr); ok &&
8989
sum.Op == token.ADD &&
9090
types.Identical(info.TypeOf(sum.X), types.Typ[types.Uintptr]) &&
91-
astutil.IsChildOf(curSum, edge.CallExpr_Args) {
91+
curSum.ParentEdgeKind() == edge.CallExpr_Args {
9292
// Have: sum ≡ T(x:...uintptr... + y:...uintptr...)
9393
curX := curSum.ChildAt(edge.BinaryExpr_X, -1)
9494
curY := curSum.ChildAt(edge.BinaryExpr_Y, -1)

0 commit comments

Comments
 (0)