You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
proposal: cmd/compile: provide 'evaluated but not used' error when function returned from another function is not used (called or assigned) #29428
The call defaultErrorHandler(p) returned a function that was then never evaluated. But the compiler doesn't warn, and the underlying logic is not executed.
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. func foo() (int, func())?
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.
changed the title [-]Give 'evaluated but not used' error when function returned from another function is not called[/-][+]proposal: cmd/compile: provide 'evaluated but not used' error when function returned from another function is not called[/+]on Dec 27, 2018
changed the title [-]proposal: cmd/compile: provide 'evaluated but not used' error when function returned from another function is not called[/-][+]proposal: cmd/compile: provide 'evaluated but not used' error when function returned from another function is not used (called or assigned)[/+]on Dec 27, 2018
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:
var err error = ...
fmt.Printf("failed: %s\n", err.Error)
It's printing a closure pointer, not the error text. It should be:
var err error = ...
fmt.Printf("failed: %s\n", err.Error())
(Just err would also work.)
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 fmt.Printf.
The compiler can't make this an error - that would violate the language spec.
But vet could warn about something like this if it were essentially always an error.
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.
Activity
HaraldNordgren commentedon Dec 26, 2018
This bit me in the butt recently when writing code like this:
The call
defaultErrorHandler(p)returned a function that was then never evaluated. But the compiler doesn't warn, and the underlying logic is not executed.odeke-em commentedon Dec 27, 2018
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.
func foo() (int, func())?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
[-]Give 'evaluated but not used' error when function returned from another function is not called[/-][+]proposal: cmd/compile: provide 'evaluated but not used' error when function returned from another function is not called[/+][-]proposal: cmd/compile: provide 'evaluated but not used' error when function returned from another function is not called[/-][+]proposal: cmd/compile: provide 'evaluated but not used' error when function returned from another function is not used (called or assigned)[/+]griesemer commentedon Dec 27, 2018
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.
randall77 commentedon Dec 28, 2018
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
errwould also work.)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
fmt.Printf.rsc commentedon Jan 9, 2019
The compiler can't make this an error - that would violate the language spec.
But vet could warn about something like this if it were essentially always an error.
And in fact vet should already be warning about @randall77's example.
(It does: https://play.golang.org/p/oDdHnjwc1Ey)
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.