-
Notifications
You must be signed in to change notification settings - Fork 18k
x/tools/go/analysis/passes/printf: Errorf functions are treated as fmt.Errorf-wrappers #47641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Change https://golang.org/cl/340409 mentions this issue: |
This indeed seems like a problem. The issue is that
Agreed. The fix should make this distinction. The good thing is that the only supported "root" function producing errors is indeed
I am assuming you mean a flag for specifying other |
It is maybe plausible to assume an This heuristic might suppress warnings on "%w" verbs in
Do we have examples of functions we would want to specify with this flag? |
Right. For an already detected formatting function or wrapper whose name is Errorf: If return type is
If my understanding is correct, then such functions will also return something else in case of non-failures. No formatting functions with such signatures are supported right now by default: they all either return a single value or no value at all.
+1 But this also seems like a separate issue. |
Change https://golang.org/cl/342109 mentions this issue: |
The printf analyzer is already capable of identifying functions which wrap formatting functions like |
I'm not aware of any yet.
Yes, but, if I understand correctly, identifying wrapper functinos only works for functions that are implemented in the same package. So, if you have a generic fmt.Errorf wrapper, this check will not detect it.
I had a similar logic in mind, but forgot to mention it in the issue description :) Another edge case is when a method doesn't return an error, but instead updates an error field in a struct. It might be reasonable to accept a I guess we have two CLs doing the ~same thing (tests are different): https://golang.org/cl/342109 and https://golang.org/cl/340409. I'm fine with submitting either of these :) |
This should work cross-package. Empirically,
|
Previously all functions that were named Errorf have been treated like a fmt.Errorf-based function. But only fmt.Errorf (and functions based on fmt.Errorf) accept the %w verb. Fix that. Updates golang/go#47641. Change-Id: Iec5d0ae674c7dc817e85291dcfa064303eafba7e Reviewed-on: https://go-review.googlesource.com/c/tools/+/340409 Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com> Reviewed-by: Damien Neil <dneil@google.com> Trust: Damien Neil <dneil@google.com>
Fixed by https://golang.org/cl/340409. |
Go 1.18 introduced a change where only fmt.Errorf function accepts the %w verb. Other Errorf function like t.Errorf do not accept it anymore. See golang/go#47641 Fix: prometheus#430
Go 1.18 introduced a change where only fmt.Errorf function accepts the %w verb. Other Errorf function like t.Errorf do not accept it anymore. See golang/go#47641 Fix: prometheus#430 Signed-off-by: Robert-André Mauchin <zebob.m@gmail.com>
Go 1.18 introduced a change where only fmt.Errorf function accepts the %w verb. Other Errorf function like t.Errorf do not accept it anymore. See golang/go#47641 Fix: #430 Signed-off-by: Robert-André Mauchin <zebob.m@gmail.com>
Doesn't this break specifying xerrors.Errorf as a printf function? |
@benbuzbee Yes, that seems likely. Do we care, though? Is there any reason to use |
My team uses it because it captures the stack frame where created which I find super valuable. I would love for standard libraries to support that but :) In the meantime, it's not the end of the world that we can't use go vet to...vet...our errors but it is a bit too bad If I can tell you more about why I like stack frames though... |
@benbuzbee Fair enough, I suggest opening a new issue to add |
Go 1.18 introduced a change where only fmt.Errorf function accepts the %w verb. Other Errorf function like t.Errorf do not accept it anymore. See golang/go#47641 Fix: prometheus#430 Signed-off-by: Robert-André Mauchin <zebob.m@gmail.com>
Background
The
printf
analyzer has a predefined list of functions and methods that are treated as functions that print something (as defined in https://pkg.go.dev/fmt, likefmt.Sprint
orfmt.Sprintf
). It is possible to make the analyzer aware of more such functions via a flag.Problem statement
The analyzer treats all functions and methods named
Errorf
as wrappers offmt.Errorf
(code). That effectively means it accepts all usages of the%w
verb in formatting strings toErrorf
calls.For example, a call like
t.Errorf("%w", err)
is not flagged by the analyzer. Neither are calls to logging functions that have an error level logging, likelogrus.Errorf
orglog.Errorf
.Proposal
First, only the
fmt.Errorf
function should be recognized by default as a function that accepts the%w
verb. OtherErrorf
functions should be treated like regular formatting functions.Second, it would probably be nice to have a way to make the
printf
analyzer aware of additionalfmt.Errorf
-wrapper functions. This could be done through an additional flag (e.g.errorffuncs
), or a special syntax for passing function names to the existingfuncs
flag. Other ideas?The two things are independent of each other. For the first step I already sent https://golang.org/cl/340409 (which actually led me into creating this issue).
The text was updated successfully, but these errors were encountered: