go.tools/cmd/vet: exclude "%#v" from recursive stringer check

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/13163043
This commit is contained in:
Andrew Gerrand 2013-08-22 11:44:39 +10:00
parent f6a22edf66
commit 890e4c0731
2 changed files with 8 additions and 6 deletions

View File

@ -7,6 +7,7 @@
package main
import (
"bytes"
"flag"
"fmt"
"go/ast"
@ -385,21 +386,21 @@ func (f *File) okPrintfArg(call *ast.CallExpr, state *formatState) (ok bool) {
f.Badf(call.Pos(), "arg %s for printf verb %%%c of wrong type: %s", f.gofmt(arg), state.verb, typeString)
return false
}
if v.typ&argString != 0 && f.recursiveStringer(arg) {
if v.typ&argString != 0 && !bytes.Contains(state.flags, []byte{'#'}) && f.recursiveStringer(arg) {
f.Badf(call.Pos(), "arg %s for printf causes recursive call to String method", f.gofmt(arg))
return false
}
return true
}
// recursiveStringer reports whether the provided argument is r, *r, or &r
// for the fmt.Stringer receiver identifier r.
func (f *File) recursiveStringer(arg ast.Expr) bool {
// recursiveStringer reports whether the provided argument is r or &r for the
// fmt.Stringer receiver identifier r.
func (f *File) recursiveStringer(e ast.Expr) bool {
if f.lastStringerReceiver == nil {
return false
}
var obj *ast.Object
switch e := arg.(type) {
switch e := e.(type) {
case *ast.Ident:
obj = e.Obj
case *ast.UnaryExpr:

View File

@ -273,9 +273,10 @@ var percentSV percentSStruct
type recursiveStringer int
func (s recursiveStringer) String() string {
fmt.Sprintf("%v", &s) // ERROR "arg &s for printf causes recursive call to String method"
fmt.Sprintf("%d", s)
fmt.Sprintf("%#v", s)
fmt.Sprintf("%v", s) // ERROR "arg s for printf causes recursive call to String method"
fmt.Sprintf("%v", &s) // ERROR "arg &s for printf causes recursive call to String method"
return fmt.Sprintln(s) // ERROR "arg s for print causes recursive call to String method"
}