]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gofmt: clear pattern match map at the correct time
authorMatthew Dempsky <mdempsky@google.com>
Tue, 7 Feb 2017 23:29:18 +0000 (15:29 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Wed, 8 Feb 2017 04:48:20 +0000 (04:48 +0000)
We need to clear the pattern match map after the recursive rewrite
applications, otherwise there might be lingering entries that cause
match to fail.

Fixes #18987.

Change-Id: I7913951c455c98932bda790861db6a860ebad032
Reviewed-on: https://go-review.googlesource.com/36546
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/gofmt/rewrite.go
src/cmd/gofmt/testdata/rewrite9.golden [new file with mode: 0644]
src/cmd/gofmt/testdata/rewrite9.input [new file with mode: 0644]

index 550492bf29a4b61160d86863e28b9db952b3355b..79b7858a5afd06be14152efdc3cf9d12a4fa1887 100644 (file)
@@ -66,10 +66,10 @@ func rewriteFile(pattern, replace ast.Expr, p *ast.File) *ast.File {
                if !val.IsValid() {
                        return reflect.Value{}
                }
+               val = apply(rewriteVal, val)
                for k := range m {
                        delete(m, k)
                }
-               val = apply(rewriteVal, val)
                if match(m, pat, val) {
                        val = subst(m, repl, reflect.ValueOf(val.Interface().(ast.Node).Pos()))
                }
diff --git a/src/cmd/gofmt/testdata/rewrite9.golden b/src/cmd/gofmt/testdata/rewrite9.golden
new file mode 100644 (file)
index 0000000..fffbd3d
--- /dev/null
@@ -0,0 +1,11 @@
+//gofmt -r=a&&b!=2->a
+
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Issue 18987.
+
+package p
+
+const _ = x != 1
diff --git a/src/cmd/gofmt/testdata/rewrite9.input b/src/cmd/gofmt/testdata/rewrite9.input
new file mode 100644 (file)
index 0000000..106ad94
--- /dev/null
@@ -0,0 +1,11 @@
+//gofmt -r=a&&b!=2->a
+
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Issue 18987.
+
+package p
+
+const _ = x != 1 && x != 2