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
proposal: cmd/compile: provide 'evaluated but not used' error when function returned from another function is not used (called or assigned) #29428
Comments
This bit me in the butt recently when writing code like this: func defaultErrorHandler(p *httputil.ReverseProxy) func(rw http.ResponseWriter, req *http.Request, err error) {
return func(rw http.ResponseWriter, req *http.Request, err error) {
rw.WriteHeader(http.StatusBadGateway)
}
}
func CustomizedErrorHandlerOption(p *httputil.ReverseProxy) {
p.ErrorHandler = func(rw http.ResponseWriter, req *http.Request, err error) {
if someConditionFulfilled(err) {
rw.WriteHeader(http.StatusBadRequest)
return
}
// Should have been 'defaultErrorHandler(p)(rw, req, err)'
defaultErrorHandler(p)
}
} The call |
Hello @HaraldNordgren, thank you for filing this issue and welcome to the Go project! The goal of the compiler's "mark-used" pass is to find and report unused variables/expressions to check the validity of the Go program -- if all the expressions are evaluated and used, that makes for a correct Go program. Any other extra passes would perhaps become too intrusive and less expressive, which would open up the flood gates and make complicated cases. For example you might want to invoke f1 just for the side effect but without using its runtime return value. What happens when we return a closure that's dynamically created? What happens if the return signature has more than a function e.g. Also the Go compiler doesn't emit warnings. In addition, this would also mean that we'd break the Go1 compatibility whereby programs that compiled before this change(if it were accepted) would be broken. Perhaps this could be a special case for go vet but even that I think might be complex. I'll page some experts for some more ideas @griesemer @josharian @ianlancetaylor |
This is an interesting, if rare, special case. Reporting an error in this case might prevent coding errors that are easily missed. But I'm not sure how intrusive it would be and whether it would meet the usual vet criteria. An experiment (implement the vet check and apply it) might be necessary to make an educated decision. |
I have seen a similar error a bunch of times inside Google:
It's printing a closure pointer, not the error text. It should be:
(Just In this case, it isn't a function returning a function that isn't called, but a method expression that's never called. The extra challenge here is deciding whether the method is called inside |
The compiler can't make this an error - that would violate the language spec. And in fact vet should already be warning about @randall77's example. The defaultErrorHandler(p) is less clearly erroneous. We call functions and ignore their results all the time. Maybe that returned a cleanup function that is unnecessary in this context. I'm not sure there's much we can do to fix your particular problem. If you want to try making vet more aggressive as an experiment and come back with a more precise extra condition to check, that's fine, but probably it is not good enough. See cmd/vet/README for criteria. Feel free to file a new issue if you do find a clearer criteria. |
What version of Go are you using (
go version
)?What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://play.golang.org/p/NjLj1WrbUuF
The text was updated successfully, but these errors were encountered: