]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: don't warn on escaped newlines in Println
authorDaniel Martí <mvdan@mvdan.cc>
Tue, 7 Nov 2017 11:34:35 +0000 (11:34 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Fri, 10 Nov 2017 09:08:06 +0000 (09:08 +0000)
The old code only worked for double-quoted strings, and only checked
that the end of the literal value was \n". This worked most of the time,
except for some strings like "foo\\n", which doesn't actually translate
into a trailing newline when unquoted.

To fix this, unquote the string first and look for a real newline at the
end of it. Ignore errors, as we don't have anything to do with string
literals using back quotes.

Fixes #22613.

Change-Id: I7cf96916dd578b7068216c2051ec2622cce0b740
Reviewed-on: https://go-review.googlesource.com/76194
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/vet/print.go
src/cmd/vet/testdata/print.go

index 620075d1184f6f4d0d46f321417383b98f11b9bf..5dc6bdf628ab18348fc33145a9a797cbcc0cf80d 100644 (file)
@@ -672,8 +672,9 @@ func (f *File) checkPrint(call *ast.CallExpr, name string) {
                // The last item, if a string, should not have a newline.
                arg = args[len(args)-1]
                if lit, ok := arg.(*ast.BasicLit); ok && lit.Kind == token.STRING {
-                       if strings.HasSuffix(lit.Value, `\n"`) {
-                               f.Badf(call.Pos(), "%s args end with redundant newline", name)
+                       str, _ := strconv.Unquote(lit.Value)
+                       if strings.HasSuffix(str, "\n") {
+                               f.Badf(call.Pos(), "%s arg list ends with redundant newline", name)
                        }
                }
        }
index c3f5abe4f11276db34ce1bb9f8a9b746b2ae0784..b7bc98ab560270741b6fbff610a55037700b767e 100644 (file)
@@ -532,4 +532,9 @@ func UnexportedStringerOrError() {
        }
        fmt.Printf("%s", uef)  // ERROR "Printf format %s has arg uef of wrong type testdata.unexportedErrorOtherFields"
        fmt.Printf("%s", &uef) // ERROR "Printf format %s has arg &uef of wrong type [*]testdata.unexportedErrorOtherFields"
+
+       fmt.Println("foo\n", "bar") // not an error
+       fmt.Println("foo\n")        // ERROR "Println arg list ends with redundant newline"
+       fmt.Println("foo\\n")       // not an error
+       fmt.Println(`foo\n`)        // not an error
 }