Skip to content

Commit 9d634e5

Browse files
committed
gofmt: rewriter matches apply to expressions only
Fixes #1384. R=rsc CC=golang-dev https://golang.org/cl/3912041
1 parent ee58cc7 commit 9d634e5

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/cmd/gofmt/rewrite.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,18 @@ func match(m map[string]reflect.Value, pattern, val reflect.Value) bool {
111111
if m != nil && pattern.Type() == identType {
112112
name := pattern.Interface().(*ast.Ident).Name
113113
if isWildcard(name) {
114-
if old, ok := m[name]; ok {
115-
return match(nil, old, val)
114+
// wildcards only match expressions
115+
if _, ok := val.Interface().(ast.Expr); ok {
116+
if old, ok := m[name]; ok {
117+
return match(nil, old, val)
118+
}
119+
m[name] = val
120+
return true
116121
}
117-
m[name] = val
118-
return true
119122
}
120123
}
121124

122-
// Otherwise, the expressions must match recursively.
125+
// Otherwise, pattern and val must match recursively.
123126
if pattern == nil || val == nil {
124127
return pattern == nil && val == nil
125128
}
@@ -204,7 +207,7 @@ func subst(m map[string]reflect.Value, pattern reflect.Value, pos reflect.Value)
204207

205208
if pos != nil && pattern.Type() == positionType {
206209
// use new position only if old position was valid in the first place
207-
if old := pattern.Interface().(token.Position); !old.IsValid() {
210+
if old := pattern.Interface().(token.Pos); !old.IsValid() {
208211
return pattern
209212
}
210213
return pos

0 commit comments

Comments
 (0)