From 10bcabde7ceabfa7ace41aa1e019efd1f7f93371 Mon Sep 17 00:00:00 2001 From: Zvonimir Pavlinovic Date: Fri, 13 Aug 2021 13:58:20 -0700 Subject: [PATCH] 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 gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Damien Neil Trust: Damien Neil Trust: Zvonimir Pavlinovic --- go/analysis/passes/printf/printf.go | 5 +---- go/analysis/passes/printf/testdata/src/a/a.go | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/go/analysis/passes/printf/printf.go b/go/analysis/passes/printf/printf.go index 6589478af0..53b3f2b5e3 100644 --- a/go/analysis/passes/printf/printf.go +++ b/go/analysis/passes/printf/printf.go @@ -590,12 +590,9 @@ func checkPrintf(pass *analysis.Pass, kind Kind, call *ast.CallExpr, fn *types.F } 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 e27dd054c3..378bdff6c3 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 @@ func PrintfTests() { _ = 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) }