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:
parent
f6a22edf66
commit
890e4c0731
|
|
@ -7,6 +7,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"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)
|
f.Badf(call.Pos(), "arg %s for printf verb %%%c of wrong type: %s", f.gofmt(arg), state.verb, typeString)
|
||||||
return false
|
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))
|
f.Badf(call.Pos(), "arg %s for printf causes recursive call to String method", f.gofmt(arg))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// recursiveStringer reports whether the provided argument is r, *r, or &r
|
// recursiveStringer reports whether the provided argument is r or &r for the
|
||||||
// for the fmt.Stringer receiver identifier r.
|
// fmt.Stringer receiver identifier r.
|
||||||
func (f *File) recursiveStringer(arg ast.Expr) bool {
|
func (f *File) recursiveStringer(e ast.Expr) bool {
|
||||||
if f.lastStringerReceiver == nil {
|
if f.lastStringerReceiver == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
var obj *ast.Object
|
var obj *ast.Object
|
||||||
switch e := arg.(type) {
|
switch e := e.(type) {
|
||||||
case *ast.Ident:
|
case *ast.Ident:
|
||||||
obj = e.Obj
|
obj = e.Obj
|
||||||
case *ast.UnaryExpr:
|
case *ast.UnaryExpr:
|
||||||
|
|
|
||||||
|
|
@ -273,9 +273,10 @@ var percentSV percentSStruct
|
||||||
type recursiveStringer int
|
type recursiveStringer int
|
||||||
|
|
||||||
func (s recursiveStringer) String() string {
|
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("%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"
|
||||||
|
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"
|
return fmt.Sprintln(s) // ERROR "arg s for print causes recursive call to String method"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue