-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
x/tools/go/analysis/passes/lostcancel: add new context functions and make it configurable #70185
Comments
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
CC @golang/tools-team |
Thanks for the report. Adding the three new context functions is trivial; I'll do that presently. Making it configurable is more work, and in our experience no-one ever configures analyzers, so they need smart defaults or heuristics to infer the relevant patterns. A good heuristic here might be "returns a context.CancelFunc". A generalization of this might be "returns any func value named |
Change https://go.dev/cl/625115 mentions this issue: |
Thanks for quick fix for first issue. In my case I need to check my multicontext type, which has the same With* functions as context, so ability to add new import path would be enough for me. We already use configuration option provided by printf analyzer to check if our logging printf-like functions are used correctly. I would like to do something similar with lostcancel. |
The *Cause variants behave in a similar manner. Updates golang/go#70185 Change-Id: Ia7eea7a5be8878930505b63fa8222060fef47079 Reviewed-on: https://go-review.googlesource.com/c/tools/+/625115 Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Alan Donovan <adonovan@google.com>
Interestingly, the printf analyzer is able to infer whether a function is printf-like based on whether it delegates (perhaps indirectly) to fmt.Sprintf; this is an example of how it is simpler and more robust to get the tool to recognize the pattern than to use an allowlist. (I wonder why your logging printf-like functions need the configuration option. Is it because dynamic calls are involved, so the analyzer can't observe the delegation? See #58340.) |
Yes, we have logger interface like in linked issue. |
Thoughts:
|
Go version
go version go1.23.2 linux/amd64
Output of
go env
in your module/workspace:What did you do?
run nogo
What did you see happen?
Analyzer
lostcancel
does not check ifcancel
returned by newcontext
functions (WithCancelCause
,WithDeadlineCause
,WithTimeoutCause
) is called. It is also not possible to add new import paths to check to the analyzer,What did you expect to see?
Analyzer lostcancel checks for these new functions too. It also has configuration flag to specify additional import paths for
context
-like packages. You may also add another flag to specify more functions to check but this is more tricky in general, as cancel function may not be returned as a second value as forcontext
functions.The text was updated successfully, but these errors were encountered: