x/tools/analysis/passes/printf: fix error message for unsuppported %w
The current error message for calls to functions that not support %w
formatting directive does not explicitly say what is wrong nor why using
%w is wrong. This CL makes the message more direct.
Fixes golang/go#47690
Change-Id: I6e42ab725e5e3a989a8505ec230b4bb6218079ff
Reviewed-on: https://go-review.googlesource.com/c/tools/+/342111
Run-TryBot: Zvonimir Pavlinovic <zpavlinovic@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
Trust: Damien Neil <dneil@google.com>
Trust: Zvonimir Pavlinovic <zpavlinovic@google.com>
diff --git a/go/analysis/passes/printf/printf.go b/go/analysis/passes/printf/printf.go
index 6589478..53b3f2b 100644
--- a/go/analysis/passes/printf/printf.go
+++ b/go/analysis/passes/printf/printf.go
@@ -590,12 +590,9 @@
}
if state.verb == 'w' {
switch kind {
- case KindNone, KindPrint:
+ case KindNone, KindPrint, KindPrintf:
pass.Reportf(call.Pos(), "%s does not support error-wrapping directive %%w", state.name)
return
- case KindPrintf:
- pass.Reportf(call.Pos(), "%s call has error-wrapping directive %%w, which is only supported for functions backed by fmt.Errorf", state.name)
- return
}
if anyW {
pass.Reportf(call.Pos(), "%s call has more than one error-wrapping directive %%w", state.name)
diff --git a/go/analysis/passes/printf/testdata/src/a/a.go b/go/analysis/passes/printf/testdata/src/a/a.go
index e27dd05..378bdff 100644
--- a/go/analysis/passes/printf/testdata/src/a/a.go
+++ b/go/analysis/passes/printf/testdata/src/a/a.go
@@ -333,7 +333,7 @@
_ = fmt.Errorf("%[2]w %[1]s", e, "x") // want `fmt.Errorf format %\[2\]w has arg "x" of wrong type string`
_ = fmt.Errorf("%w", "x") // want `fmt.Errorf format %w has arg "x" of wrong type string`
_ = fmt.Errorf("%w %w", err, err) // want `fmt.Errorf call has more than one error-wrapping directive %w`
- fmt.Printf("%w", err) // want `fmt.Printf call has error-wrapping directive %w`
+ fmt.Printf("%w", err) // want `fmt.Printf does not support error-wrapping directive %w`
Errorf(0, "%w", err)
}