mirror of https://github.com/golang/go.git
cmd/vet: ignore unrecognized flags for fmt.Formatter
Fixes #22608. Change-Id: Id85eb86b0b262156646e55f102fe888b345b20cf Reviewed-on: https://go-review.googlesource.com/77230 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Russ Cox <rsc@golang.org> Reviewed-by: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
fafce97ec6
commit
510327012b
|
|
@ -483,15 +483,17 @@ func (f *File) okPrintfArg(call *ast.CallExpr, state *formatState) (ok bool) {
|
|||
}
|
||||
}
|
||||
|
||||
if !found && !formatter {
|
||||
f.Badf(call.Pos(), "%s format %s has unknown verb %c", state.name, state.format, state.verb)
|
||||
return false
|
||||
}
|
||||
for _, flag := range state.flags {
|
||||
if !strings.ContainsRune(v.flags, rune(flag)) {
|
||||
f.Badf(call.Pos(), "%s format %s has unrecognized flag %c", state.name, state.format, flag)
|
||||
if !formatter {
|
||||
if !found {
|
||||
f.Badf(call.Pos(), "%s format %s has unknown verb %c", state.name, state.format, state.verb)
|
||||
return false
|
||||
}
|
||||
for _, flag := range state.flags {
|
||||
if !strings.ContainsRune(v.flags, rune(flag)) {
|
||||
f.Badf(call.Pos(), "%s format %s has unrecognized flag %c", state.name, state.format, flag)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
// Verb is good. If len(state.argNums)>trueArgs, we have something like %.*s and all
|
||||
// but the final arg must be an integer.
|
||||
|
|
|
|||
|
|
@ -163,11 +163,12 @@ func PrintfTests() {
|
|||
Printf(format, "hi") // ERROR "Printf format %s reads arg #2, but call has only 1 arg$"
|
||||
Printf("%s %d %.3v %q", "str", 4) // ERROR "Printf format %.3v reads arg #3, but call has only 2 args"
|
||||
f := new(stringer)
|
||||
f.Warn(0, "%s", "hello", 3) // ERROR "Warn call has possible formatting directive %s"
|
||||
f.Warnf(0, "%s", "hello", 3) // ERROR "Warnf call needs 1 arg but has 2 args"
|
||||
f.Warnf(0, "%r", "hello") // ERROR "Warnf format %r has unknown verb r"
|
||||
f.Warnf(0, "%#s", "hello") // ERROR "Warnf format %#s has unrecognized flag #"
|
||||
Printf("d%", 2) // ERROR "Printf format % is missing verb at end of string"
|
||||
f.Warn(0, "%s", "hello", 3) // ERROR "Warn call has possible formatting directive %s"
|
||||
f.Warnf(0, "%s", "hello", 3) // ERROR "Warnf call needs 1 arg but has 2 args"
|
||||
f.Warnf(0, "%r", "hello") // ERROR "Warnf format %r has unknown verb r"
|
||||
f.Warnf(0, "%#s", "hello") // ERROR "Warnf format %#s has unrecognized flag #"
|
||||
fmt.Printf("%#s", FormatterVal(true)) // correct (the type is responsible for formatting)
|
||||
Printf("d%", 2) // ERROR "Printf format % is missing verb at end of string"
|
||||
Printf("%d", percentDV)
|
||||
Printf("%d", &percentDV)
|
||||
Printf("%d", notPercentDV) // ERROR "Printf format %d has arg notPercentDV of wrong type testdata.notPercentDStruct"
|
||||
|
|
|
|||
Loading…
Reference in New Issue